Thread: Novice program, plz help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    Novice program, plz help

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <conio.h>
    
    
    #define MAX		100
    #define MIN		1
    
    int grades, number, count, ch;
    
    int main(void)
    {
     printf("\nDo you want to add some grades? (Y/N) ");
     ch = toupper((int)getch());
    
     if (ch == 'N') {
     printf("\nAlright have a nice day\n");
     system("pause");
     return 0; }
    
     else
    
     do {
     number = MIN;
     grades = 0;
     count=0;
    
    } while (number++ != (MAX));
    printf("Enter grade");
    scanf("%d",++ grades);
    ++ count;
    printf("%d / %d = ",grades,count);
    	printf("\nDo you want to enter another Grade? (Y/N) ");
            ch = toupper((int)getch());
    		printf("\n");
      } while (ch != 'N');
     }
     }
    Keep getting a parse error before the last while.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have three open-curly-braces and five close-curly-braces. The close-curly-brace right before the last while corresponds to the open-curly-brace of int main, so all that code is hanging outside of everything.

    If you mean that last while to be part of a do-while loop, well, then you need the do part somewhere.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use proper indentation!!!

    alt_F8 in VS will help
    Code:
    int main(void)
    {
    	printf("\nDo you want to add some grades? (Y/N) ");
    	ch = toupper((int)getch());
    
    	if (ch == 'N') 
    	{
    		printf("\nAlright have a nice day\n");
    		system("pause");
    		return 0; 
    	}
    	else
    		do 
    		{
    			number = MIN;
    			grades = 0;
    			count=0;
    		} while (number++ != (MAX));
    	printf("Enter grade");
    	scanf("&#37;d",++ grades);
    	++ count;
    	printf("%d / %d = ",grades,count);
    	printf("\nDo you want to enter another Grade? (Y/N) ");
    	ch = toupper((int)getch());
    	printf("\n");
    }
    
    while (ch != 'N');
    }
    }
    you will see that your while is place outside any function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    In addition, while it's correct to cast the argument to toupper(), you need to cast it to unsigned char, not int. Unless getch() returns unsigned char (which I doubt, but I can't be certain). So toupper() should be called something like this:
    Code:
    int ch = getch(); /* I assume getch() acts like getchar() */
    if(ch == EOF) ... /* oops */
    ch = toupper( (unsigned char)ch );
    Why that particular cast? Because C says so (and it says so in order to allow an implementation to provide ctype macros that do table lookups).

    You'll also want to make sure you pass the proper type to scanf(); ++grades has type int which is not what %d expects (it expects a pointer to int).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Shouldn't be using getch in the first place... Supposed to be using getchar().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Actually, when you use getchar() how do you get input as 1 character at a time from keyboard, if you don't know your tidbits? I think the main reason people use getch() is because it does that easily for them. And sometimes things that work are the things you tend to use, even if someone says for obscure reasons to you, not to.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed, but getch() is just more unportable which is why getchar() is suggested.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Do you think that most people posting here care about portability? You are just repeating to them things they barely understand yet. And i must say you are getting tiring sometimes.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, so we should ignore portability?
    That's what this forum is about - safe and portable code, among other things.
    There is a reason people suggest avoiding getch and cornio.h. I'm not the only one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    well first make sure they get good books and good teachers!
    they are just new to all this and will obviously do as the book or teachers explains!
    and if some one is so confused about, say, while loops, don't feel bad if he cant value portability!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is a fine line, of course. But switching getch() for getchar() isn't particularly hard so I think we can afford to suggest that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by xuftugulus View Post
    Do you think that most people posting here care about portability? You are just repeating to them things they barely understand yet. And i must say you are getting tiring sometimes.
    It is all about learning right habbits
    When people learn in the University they use the tools required by the teacher like TC 3.0

    When they came afterwards to work in some real envirounment they will be requested to use the compiler available in this place like VS6.0, VS2005, gcc, etc

    If they get a right habbits of writing portable code - they will have no need to unstudy what was learned and study the new ways of writing the same basic things
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It's also making it so that the people here can compile the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Plz help wit my program
    By psychaospath in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2006, 05:13 PM
  4. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM