ok I have added the curly bracket. so what should I do next? I have tried to figure out how to read the ID as the main part of each line. wow I noticed I get so many errors with the latest code I have
strange, when I try to build my code, I get this
seems I didn't have to include two function and that's what brought up this code. I so what do I do now to proceed? here is the latest codeCode:// cstdio standard header #pragma once #ifndef _CSTDIO_ #define _CSTDIO_ #include <yvals.h> #ifdef _STD_USING #undef _STD_USING #include <stdio.h> #define _STD_USING #else /* _STD_USING */ #include <stdio.h> #endif /* _STD_USING */ #define _HAS_CONVENTIONAL_CLIB 1 #define _IOBASE _base #define _IOPTR _ptr #define _IOCNT _cnt #ifndef RC_INVOKED #if _GLOBAL_USING _STD_BEGIN using ::size_t; using ::fpos_t; using ::FILE; using ::clearerr; using ::fclose; using ::feof; using ::ferror; using ::fflush; using ::fgetc; using ::fgetpos; using ::fgets; using ::fopen; using ::fprintf; using ::fputc; using ::fputs; using ::fread; using ::freopen; using ::fscanf; using ::fseek; using ::fsetpos; using ::ftell; using ::fwrite; using ::getc; using ::getchar; using ::gets; using ::perror; using ::putc; using ::putchar; using ::printf; using ::puts; using ::remove; using ::rename; using ::rewind; using ::scanf; using ::setbuf; using ::setvbuf; using ::sprintf; using ::sscanf; using ::tmpfile; using ::tmpnam; using ::ungetc; using ::vfprintf; using ::vprintf; using ::vsprintf; _STD_END #endif /* _GLOBAL_USING */ #endif /* RC_INVOKED */ #ifndef _Filet #define _Filet FILE #endif /* _Filet */ #ifndef _FPOSOFF #define _FPOSOFF(fp) ((long)(fp)) #endif /* _FPOSOFF */ #endif /* _CSTDIO_ */ /* * Copyright (c) 1992-2005 by P.J. Plauger. ALL RIGHTS RESERVED. * Consult your license regarding permissions and restrictions. V4.05:0009 */
Code:#include <stdio.h> #include <string.h> #include <sys/types.h> #include <stdio.h> struct item { int ID; char name[64]; int price; }; int main(int argc, char *argv[]) FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [50] if ((fp=fopen("C://shoppinglist.txt","r"); printf("Error: can't open file.\n"); exit(1); } while (retstr, BUFSIZ, fp)!= NULL) { { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if(cnt == max_cnt) break; f1=strtok(retstr, " "); f2=strtok(NULL, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); cnt++; } for(i=0; i < cnt; i++) printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price); fclose(fp); exit(0); } printf( "1 - view the complete list of goods in the shop, with their prices" ); printf( "2 – list the cheapest item on offer" ); printf( "3 – “discount” option" ); printf( "4 – choose items for purchase" ); printf( "5 – total price for items" ); printf( "6 - Exit"); switch ( option ) { case 1: case 2: break; case 3: case 4: printf( "choose item for purchase\n" ); break; case 5: printf( "total price for items\n" ); break; case 6: printf( "Exit\n" ); } return 0;
Last edited by Hitsugaya_KK; 08-19-2009 at 03:39 AM.
Again I see some missing braces, so this code will not compile properly. You should be trying to at least compile your code without errors every few lines even if it does not produce output or anything.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
this is the latest:
finding it hard to get read of the errors :/Code:#include <stdio.h> #include <string.h> #include <sys/types.h> #include <stdio.h> struct item { int ID; char name[64]; int price; }; int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [50]; if ((fp=fopen("C://shoppinglist.txt","r"); } printf("Error: can't open file.\n"); { exit(1); } while (retstr, BUFSIZ, fp)!= NULL) { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if(cnt == max_cnt) break; f1=strtok(retstr, " "); f2=strtok(NULL, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); cnt++; } for(i=0; i < cnt; i++) printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price); fclose(fp); exit(0); } printf( "1 - view the complete list of goods in the shop, with their prices" ); printf( "2 – list the cheapest item on offer" ); printf( "3 – “discount” option" ); printf( "4 – choose items for purchase" ); printf( "5 – total price for items" ); printf( "6 - Exit"); switch ( option ) { case 1: case 2: break; case 3: case 4: printf( "choose item for purchase\n" ); break; case 5: printf( "total price for items\n" ); break; case 6: printf( "Exit\n" ); } return 0;
You need to indent your code properly. For example:Originally Posted by Hitsugaya_KK
It is now obvious that you have code that should be in a function, presumably the main function, hanging in the middle of nowhere.Code:#include <stdio.h> #include <string.h> #include <sys/types.h> #include <stdio.h> struct item { int ID; char name[64]; int price; }; int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [50]; if ((fp=fopen("C://shoppinglist.txt","r"); } printf("Error: can't open file.\n"); { exit(1); } while (retstr, BUFSIZ, fp)!= NULL) { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if(cnt == max_cnt) break; f1=strtok(retstr, " "); f2=strtok(NULL, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); cnt++; } for(i=0; i < cnt; i++) printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price); fclose(fp); exit(0); } printf( "1 - view the complete list of goods in the shop, with their prices" ); printf( "2 – list the cheapest item on offer" ); printf( "3 – “discount” option" ); printf( "4 – choose items for purchase" ); printf( "5 – total price for items" ); printf( "6 - Exit"); switch ( option ) { case 1: case 2: break; case 3: case 4: printf( "choose item for purchase\n" ); break; case 5: printf( "total price for items\n" ); break; case 6: printf( "Exit\n" ); } return 0;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
this is what I have done so far:
please can you help me point out the code?Code:#include <stdio.h> #include <string.h> #include <sys/types.h> #include <stdio.h> struct item { int ID; char name[64]; int price; }; int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [10]; if (fp=fopen("C://shoppinglist.txt","r")); } printf("Error: can't open file.\n"); { exit(1); } while (retstr, BUFSIZ, fp)!= NULL) { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if(cnt == max_cnt) break; f1=strtok(retstr, " "); f2=strtok(NULL, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); cnt++; } for(i=0; i < cnt; i++) printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price); fclose(fp); exit(0); } printf( "1 - view the complete list of goods in the shop, with their prices" ); printf( "2 – list the cheapest item on offer" ); printf( "3 – “discount” option" ); printf( "4 – choose items for purchase" ); printf( "5 – total price for items" ); printf( "6 - Exit"); switch ( option ) { case 1: case 2: break; case 3: case 4: printf( "choose item for purchase\n" ); break; case 5: printf( "total price for items\n" ); break; case 6: printf( "Exit\n" ); } return 0; }
Last edited by Hitsugaya_KK; 08-19-2009 at 04:46 AM.
Yes, I can point out mistakes in your code, but I refuse to do so until you indent it properly. I have already given you an example. If you ignore my advice, why should I give you more advice?Originally Posted by Hitsugaya_KK
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok I edited the code. will it do? I am sorry . ok I made final adjustments. I hope someone can help me in identifying my errors
Last edited by Hitsugaya_KK; 08-19-2009 at 04:43 AM.
You're messing up your bracketing, which messes up program flow and your indenting. When it comes to indenting you might not care but the program flow matters. You want to ensure proper, top-down execution by opening and closing all your statement blocks. So like people have been saying you need to fix that. It's a good idea for beginners to follow a simple rule - match your brackets, then fill in the statements.
First you type:
Let's fill in that if condition.Code:if() { }
Add a while loop.Code:if( fp != NULL ) { }
Code:if( fp != NULL ) { char retstr[BUFSIZ]; while( ) { } fclose(fp); }Savvy? Get to work. Start over.Code:if( fp != NULL ) { char retstr[BUFSIZ]; while( fgets(retstr, BUFSIZ, fp) != NULL ) { /* strtok stuff.... */ } fclose(fp); }
Last edited by whiteflags; 08-19-2009 at 06:01 AM.
done what I can . I have 1 error in my program. And the error is "Error 10 error C2059: syntax error : 'if'". how do I go about it? it is referring to this "if( fp != NULL )".Code:#include <stdio.h> #include <string.h> #include <sys/types.h> #include <stdio.h> struct item { int ID; char name[64]; int price; }; int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [10]; if (fp=fopen("C://shoppinglist.txt","r")); { printf("Error: can't open file.\n"); } exit(1); } if( fp != NULL ) { char retstr, [BUFSIZ]; while( fgets(retstr, BUFSIZ, fp) != NULL ) { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if(cnt == max_cnt) break; f1=strtok(retstr, " "); f2=strtok(NULL, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); cnt++; } for(i=0; i < cnt; i++) { printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price); } fclose(fp); exit(0); }
This is your main function:Originally Posted by Hitsugaya_KK
The line that the compiler is complaining about is outside of the main function, where it should not be. Notice that I indented all the code within the main function. As such, it is easy for me to see when the main function ends, thus I can confidentally tell you what I just told you.Code:int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [10]; if (fp=fopen("C://shoppinglist.txt","r")); { printf("Error: can't open file.\n"); } exit(1); }
By the way, you have an extra semi-colon on this line:
Code:if (fp=fopen("C://shoppinglist.txt","r"));
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
laserlight, is this what you said I should do?
I don't see an extra simicolon, just an extra bracketCode:int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [10]; if (fp=fopen("C://shoppinglist.txt","r")); { printf("Error: can't open file.\n"); } if( fp != NULL ) { char retstr, [BUFSIZ]; while( fgets(retstr, BUFSIZ, fp) != NULL ) { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if(cnt == max_cnt) break; f1=strtok(retstr, " "); f2=strtok(NULL, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); cnt++; } exit(1); }
Last edited by Hitsugaya_KK; 08-19-2009 at 07:45 AM.
You are getting closer, but you should be more consistent, including within the function:
So now, based on the fact that the last closing brace is not aligned with the first closing brace, you get a visual cue that the main function is not complete.Code:int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; char *f1, *f2, *f3, *p; int max_cnt=15; int i, cnt=0; struct item record [10]; if (fp = fopen("C://shoppinglist.txt","r")); { printf("Error: can't open file.\n"); } if (fp != NULL) { char retstr, [BUFSIZ]; while( fgets(retstr, BUFSIZ, fp) != NULL ) { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if (cnt == max_cnt) break; f1=strtok(retstr, " "); f2=strtok(NULL, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); cnt++; } exit(1); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am now getting an error based on "char retstr, [BUFSIZ];" . It is saying Error 2 error C2059: syntax error : '[' . No need error anymore, I realised I had to get rid of "char retstr, [BUFSIZ];" since it had already been initialized. I had to rebuild the program so now there are no errors.
I made a some changes to the program. So now I need to do the Menu part :/.Code:#include <stdio.h> #include <string.h> #include <sys/types.h> #include <stdio.h> struct item { int ID; char name[64]; int price; int quantity; }; int main(int argc, char *argv[]) { FILE *fp; char retstr[BUFSIZ]; int *f1; char *f2; int *f3; int *f4; char *p; int max_cnt=15; int i, cnt=0; struct item record [10]; if (fp=fopen("C://shoppinglist.txt","r")); { printf("Error: can't open file.\n"); } if( fp != NULL ) { while( fgets(retstr, BUFSIZ, fp) != NULL ) { if ((p = strchr(retstr, '\n')) != NULL) *p = '\0'; if(cnt == max_cnt) break; f1=strtok(,NULL " "); f2=strtok(retstr, " "); f3=strtok(NULL, " "); strcpy(record[cnt].ID, f1); strcpy(record[cnt].name, f2); strcpy(record[cnt].price, f3); strcpy(record[cnt].quantity, f4); cnt++; } exit(1); } for(i=0; i < cnt; i++) { printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price); } fclose(fp); exit(0); }
Code:{ Int option; int a; /*it lists all the items*/ int b; /* it looks for the item with the lowest price*/ int c; /* it discounts 20% of the price from all items only once*/ int d:/* it allows the user to select the items he wants*/ printf( "1 - view the complete list of goods in the shop, with their prices" ); printf( "2 – list the cheapest item on offer" ); printf( "3 – “discount” option" ); printf( "4 – choose items for purchase" ); printf( "5 – total price for items" ); printf( "6 - Exit"); switch ( option ) { case 1: case 2: break; case 3: case 4: printf( "choose item for purchase\n" ); break; case 5: printf( "total price for items\n" ); break; case 6: printf( "Exit\n" ); } return 0;
Last edited by Hitsugaya_KK; 08-19-2009 at 08:30 AM.