|
Hi David,
Many thanks for responding to my posting & yes I should of explained with more clarity in respect of my failure. This will be my first program written in "C" I usually write small batch programs in XP and/or sometimes in Win9x. I have found that CMD (although) is very good sometimes I could do with some assistance programs to ease XP's CMD. So I am trying to write small helper programs in "C" to facilitate this, just some background info which may or may not be relevant.
I have been to an online site which compiles my code as a check & a series of errors are reported which lead me to believe my code has errors & is not right I will display the output below from "https://www.onlinegdb.com/online_c_compiler"
The Compilation failed due to following error(s).
main.c:7:1: error: expected identifier or ‘(’ before ‘for’
for (i = 1; i < argc; i++) {
^~~
main.c:7:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
for (i = 1; i < argc; i++) {
^
main.c:7:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
for (i = 1; i < argc; i++) {
^~
main.c: In function ‘echo’:
main.c:29:28: error: ‘STDIN’ undeclared (first use in this function)
while(fgets(input, 80, STDIN)){
^~~~~
main.c:29:28: note: each undeclared identifier is reported only once for each function it appears in
main.c: At top level:
main.c:38:7: error: expected identifier or ‘(’ before ‘if’
if (*char == -x) {
^~
main.c:42:7: error: expected identifier or ‘(’ before ‘else’
else
^~~~
main.c:47:14: error: unterminated comment
I am unclear as to what these errors messages mean really. Hope this gives you a rough idea as to where my failure is. I am concerned also about the structure of my potential program as I am not sure if it is right? any guidance would be appreciated? Also just as a tag-on due to the fact I am writing very small programs could you advise as to a small .com .EXE builder that I could use Please?
Best Regards
modified 12-Jan-21 4:38am.
|
|
|
|
|
You cannot have lines of code outside of functions in that way. Line 7 is a for statement but it cannot exist stand-alone like that, it needs to be part of some function. If you look below at function main, you can see the correct structure. I suggest you get hold of a copy of The C Programming Language - Wikipedia[^] and learn from the beginning.
|
|
|
|
|
Hi Richard,
Reading between the lines I think you are saying my code is way off base & is fundamentally wrong. would the following correct the For section like so I have removed the int main() as I am assuming only one int main() is required. your advice is duly noted in respect of ground up code writing many thanks?
int main()
{
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'x':
printf("flag a was called\n");
break;
}
}
}
|
|
|
|
|
Yes, all code must be contained in a function. you can put it all in main for a simple program, but for more complex problems you should create other functions that can be called with different parameter values. The documentation explains it clearly.
|
|
|
|
|
Thanks Richard,
I have edited/amended my first post to reflect what you have said. Yes small programs are a good place to start
many thanks!
|
|
|
|
|
You have another function inside main, which is also wrong:
int echo(){
char input[80];
Each function must stand alone in the main part of the source file:
int echo()
{
}
int main()
{
}
And get rid of those goto statements, that is really bad practice in modern programming. As I suggested above, get a copy of K & R and spend time learning the language properly. Trying to learn by questions here will take ten times as long, and not be comprehensive.
|
|
|
|
|
Hi Richard,
Sorry for not being attentive but I have been trying to find the book that you referred to in respect of "C" programming & I have finally sourced a copy albeit in PDF format but I have none-the-less found it, so I've got it now. Also I have been viewing a u-tube video which I have downloaded from here:
https://www.youtube.com/watch?v=KJgsSFOSQv0[^]
Only because I am the type of person if I can see it visually I tend to retain the info a bit better as I am not the sharpest tool in the toolbox if you know what I mean.
The narrator in the video mentions code::blocks which comes with IDE text editor & compiler to run the programs I selected the portable version with mingw to get started I will install tommorrow & give test run I have also been listening to some of the examples on the utube video. I may be able to break my code sample down into say 3 stages and run the code in Code::Blocks to check if the code is somewhere near possibly (Test-running)?
When you refer to the goto statements as being very bad practice in modern programming the only reason I opted to use these was because batch scripting is familair ground to me & Batch uses goto :labels therein quite a lot. Also one of the web-sites stated that goto statements are bad because of the dangers of spaghetti code, however they went onto say that if used sparingly & in the case of flag usage e.g. & only confined to purely isolated cases (like flags -X) they were, (although frowned upon) were permissable.
If I could use them until I acquire the necessary alternative skillset would it be okay for now given my obvious limitations. I could then interchange the new code with the goto code at a later date or is there another reason which I am missing here that I should not be using them, like they will not compile etc?
I will also attempt to amend the 1st post code with amended curly brackets.
Best Regards
modified 12-Jan-21 16:11pm.
|
|
|
|
|
jackngill wrote: I have been trying to find the book Amazon has it The C Programming Language (2nd Edition): Amazon.co.uk: Kernighan, Brian, Ritchie, Dennis: 8601410794231: Books[^].
jackngill wrote: The narrator in the video mentions code::blocks I would recommend Visual Studio 2019 from Microsoft. It's free and runs natively, so does not require mingw.
jackngill wrote: If I could use them (goto) until I acquire the necessary alternative skillset If you do it properly in the first place you will find there is no need for them. Whilst they will always compile they really are best avoided, especially as a new boy.
|
|
|
|
|
Hi Richard
Thanks for sharing your experience & advice it has ironed out quite a few wrinkles- I will Kinda work last to the first of your quotes or in reverse order.
Quote: If you do it properly in the first place you will find there is no need for them. Whilst they will always compile they really are best avoided, especially as a new boy.
I see, what about if I used a "if" "else" statement like e.g. if the user types or stipulates the "-x" flag with "ECO" then "ECO" removes the CRLF/EOL, "else" the user fails to input "-x" flag then "ECO" just echo's one line of text to the screen in the same fashion the normal echo statement does? If this is a suitable alternative I will work on amending the code?
Quote: I would recommend Visual Studio 2019 from Microsoft. It's free and runs natively, so does not require mingw.
I have downloaded the community edition of Visual Studio 2019 it seems to be the free version, will I need additional software to convert code (ECO.h) to EXE/COM once I have corrected all my errors? and last but not least... (Edit) Oop's It does not seem I will be able to use the 2019 version as it is asking for Dot net vers 4.6 which is a bridge too far for winXPSP3 I think Vers 4.0 was the last for XP I will look for earlier vers compatable with XP.
Quote: Amazon has it The C Programming Language (2nd Edition): Amazon.co.uk: Kernighan, Brian, Ritchie, Dennis: 8601410794231: Books[^].
It's okay I have managed to get hold of a copy "The C Programming Language" & will inwardly digest & will be good for referencing study.
Best Regards
David
|
|
|
|
|
jackngill wrote: I see, what about if I used a "if" "else" statement Exactly right, and you can eliminate any requirement for goto by correct use of if/else , while , switch , etc. My point was (sort of) do it the right way now, and life becomes much easier. Writing code that you know is poor or less efficient with a mental note to fix it in the future is usually a path to problems in the future.
jackngill wrote: will I need additional software No, Visual Studio includes all the components necessary to build source code into executables. BTW .COM files were abandoned years ago. Just make sure you include the right components (Visual C++ package) for building C/C++ programs. The same integrated compiler will build code written in either language.
|
|
|
|
|
It seems that one of my previous replies/posts is under review for possible spam or at least at my end it is showing so. I do not understand why this is I have been reflecting over the content trying to think what may have triggered this, I'm not sure what would cause this reaction. Perhaps I need to contact a mod for clarification.
I think, I will need to check at further length Visual Studio 2017 works with XP but it depends on what vers of Dotnet is required I think Dot net framework Vers 4.0 was the last for XP
modified 13-Jan-21 11:43am.
|
|
|
|
|
Nothing showing here. When and if it does happen, just be patient. The automatic spam checker holds "suspect" messages in a queue. They are then reviewed by one of the many moderators here, and accepted or rejected as appropriate. Your messages may get picked up for spurious reasons, but it is a fairly intelligent system and will improve once it recognises you.
|
|
|
|
|
Richard,
Yes decided to leave the site alone for a while to allow the spam-bot thing do it's thing. I took the oppertunity to look into/do some research into Visual Studio & which version would be suitable with windows XP.
This info may help others who might find themselves here. The version you can install is dictated in the main by the Dot Net Framework version as Visual Studio requires the relevant dot net framework installed which in the case of Windows XP (last DotNet for XP) "DotNet framework version 4.0" it needs to be installed with your OS. Now I have trawled a few sites looking at what the latest version would be for Windows XPSP3 & it is "Visual Studio 2010" which is going back some when the current vers at the time of writing is 2019. I have found it quite difficult to grab a 2010 version as we are talking old School but I elected to download an offline iso version called OFFLINESOFTWARES.COM_VS2010Express.iso. Not content with just downloading it (which incidently is 693Mb) I have scanned the iso with 2 virus checkers to check for malware & viruses, I do not have any affiliation with the VStudio website it was just the first one which I was successful in downloading it from. I would post the link but I will need to check if it is permissable here first. I think fully installed needs 2.2Gb of your HDD space.
Anyway My little rant over where/which Visual Studio 2010 which may or may not help others now is concluded. I think I will need to clear some HDD space to install it as it needs a fair amount of space. Okay "if" "Else" it is then re not using goto statements
I have been undertaking some house keeping as I needed to clear some hard drive space to install Visual Studio Vers 2010. I have done so but it seems to be defaulting to C++ rather than "C" Will the editor accept "C" code rather than C++?
best Regards
|
|
|
|
|
Hi Richard
I think because I am working on a EOL OS (XP) I have been having lots of problems installing Visual Studio 2010 I think the problems exist because the OS & the development of VStudio 2010 have not made any progress since its conception & I think that is why it is not working.
I have alternatively installed Code::Blocks with some minor glitchs & needing some additional files to run under XP but it seems to be working correctly.
I went for the portable install so that if it is no good for some reason I can easily ditch. Code::Blocks seems to have continuing development for XP so it has the advantage of update progression.
I will keep you posted re developments
Best Regards
|
|
|
|
|
Microsoft Visual C++ 2019. Windows 10, using MFC
Is there a nice step by step example of code showing how to use the MRU in a standard SDI application with ribbon and without document/view architecture?
My used files (*.jpg, *.bmp) are well stored in the MRU file list of the main menu of the ribbon.
But when I click on one of them I have an error in .../MFC/appui.cpp showing that ENSURE_VALID(m_pDocManager) failed, which is normal because I did not choose doc/view support.
Is there any way out, unless completely rewriting a new project with pDocManager?
Pierre
|
|
|
|
|
|
|
You are welcome!
|
|
|
|
|
Hi
I want the array of data under items, using this code i am able to get only the value under the first structure of data under items. How can i loop to the second structure of data under items, and further more if present. I am doing this using boost in c++.
Can someone help on this.
items=root.get_child("Serial");
for (boost::property_tree::ptree::iterator itprop = items.begin(); itprop != items.end(); ++itprop)
{
for(boost::property_tree::ptree::iterator it1=itprop->second.begin();it1!=itprop->second.end();++it1)
{
if(it1->first=="Items")
{
boost::property_tree::ptree& rootschild=it1->second;
cout<<"Size is "<<rootschild.size()<<"\n";
for(boost::property_tree::ptree::iterator="" it2="rootschild.begin();it2!=rootschild.end();++it2)
" {
="" it3="it2-">second.begin();it3!=it2->second. end();++it3)
{
std::cout<<it3->first<<": "<<it3->second.get_value<string>()<<"\n";
}
}
}
{
"Header": {
"Name": "abcd",
"Email": "xyz"
},
"Serial": [{
"flavour": "Green",
"Color": "9",
"code": "103",
"Items": [{
"date": "2020/11",
"value": "3.5",
"serial": "01"
}, {
"date": "2020/10",
"value": "3.4",
"serial": "03"
}]
}]
}
With Regards
|
|
|
|
|
What about something like:
serial_items = root.get_child("Serial");
items = serial_items.get_child("Items");
for (boost::property_tree::ptree::iterator itprop = items.begin(); itprop != items.end(); ++itprop)
... I don't use boost, nor have I tried to compile it, so this may or may not work.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 10-Jan-21 15:04pm.
|
|
|
|
|
Hi,
Thank you for the reply, but the code is crashing in the below line.
David Crow wrote: items = serial_items.get_child("Items");
With Regards
|
|
|
|
|
Litu Sahoo wrote: ...but the code is crashing in the below line. What exactly does that mean?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Litu Sahoo wrote: Thank you for the reply, but the code is crashing in the below line.
David Crow wrote: items = serial_items.get_child("Items");
Did you debug your code? What is the value of
serial_items
before the "crash"?
|
|
|
|
|
hi
m_data is showing blank and m_children is showing an address.
|
|
|
|
|
Hi
I modified the program but still not able to get the whole array of data Under Items. Below is the sample code. Please let me know where it is wrong.
namespace pt=boost::property_tree;
pt::ptree root;
pt::ptree items;
pt::ptree obsrvtns;
pt::ptree obsrvtns1;
pt::read_json(filepath,root);
obsrvtns=root.get_child("Header");
cout<<"Header size is "<<obsrvtns.size()<<"\n";
for="" (boost::property_tree::ptree::iterator="" itprop="obsrvtns.begin();" !="obsrvtns.end();" ++itprop)
="" {
="" cout<<itprop-="">first<<": "<<itprop->second.get_value<string>()<<"\n";
}
items=root.get_child("Serial");
cout<<"Series Size is "<<items.size()<<"\n";
for(boost::property_tree::ptree::iterator="" it="items.begin();it!=items.end();++it)
" {
="" cout<<"second="" size="" is="" "<<it-="">second.size()<<"\n";
for(boost::property_tree::ptree::iterator it1=it->second.begin();it1!=it->second.end();++it1)
{
cout<<it1->first<<": "<<it1->second.get_value<string>()<<"\n";
if(it1->first=="Item")
{
/*obsrvtns1=it1->second.get_child("Items");
cout<<"check \n";*/
//BOOST_FOREACH(boost::property_tree::ptree::value_type &v,root.get_child("Header.Serial.Item"))
BOOST_FOREACH(boost::property_tree::ptree::value_type &v,root.get_child("Item"))
{
cout<
|
|
|
|