Thread: while and gethchar. alternative solution.

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    while and gethchar. alternative solution.

    hallo guys!
    i have been studying C programming. i was typing some codes and trying to find alternative solutions about the given problem.
    first of all the below code is not mine and it made me really confused.
    first please check the code. and i will ask my question after.
    Code:
    #include <stdio.h>
    int main(void){
    int grade;
    unsigned int acount=0;
    unsigned int bcount=0;
    unsigned int ccount=0;
    unsigned int dcount=0;
    unsigned int fcount=0;
    puts("enter the letter grades");
    puts("enter the E0F character to end input");
    
    while((grade=getchar()) != E0F){
    switch(grade){
    case 'A':
    case 'a':
    ++acount;
    break;
    case 'b':
    case 'B':
    ++bcount;
    break;
    case 'C':
    case 'c':
    ++ccount;
    break;
    case 'D':
    case 'd':
    ++dcount;
    break;
    case 'F':
    case 'f':
    ++fcount;
    break;
    default:
    printf("%s","incorrect letter grade entered,enter new one");
    break;
    } //end switch
    }// end while
    puts( "\nTotals for each letter grade are:" );
    printf( "A: %u\n", aCount ); // display number of A grades
    printf( "B: %u\n", bCount ); // display number of B grades
    printf( "C: %u\n", cCount ); // display number of C grades
    printf( "D: %u\n", dCount ); // display number of D grades
     printf( "F: %u\n", fCount ); // display number of F grade
    }//end main
    now you understand the code. there is code for counting letter grades. my question is this
    why should i need to use E0F character to stop while loop ? i mean, why i can not use simply -1 to exit this code ?
    and in order to execute E0F , i should press ctrl+z, otherwise it won't work. how can i turn off this feature.

    and about while and getchar, i understand , i should get an input from user, but without defining any char character how does it works ?
    i mean the right code according to me should be this
    char x; // i defiend a character
    then
    while(getchar() .... etc)
    did i use int grade as a character in this form ?
    thank you for your help and answers. and sorry i am a new bee

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can tell right away there is a typo in your code.
    Code:
    E0F is not the same as EOF.
    The slash is a clear indication of a zero, so you can tell the difference from O. EOF with an O is correct.

    now you understand the code. there is code for counting letter grades. my question is this
    why should i need to use E0F character to stop while loop ? i mean, why i can not use simply -1 to exit this code ?
    and in order to execute E0F , i should press ctrl+z, otherwise it won't work. how can i turn off this feature.

    There is no turning off. There are a couple of things wrong with your thinking. 1) EOF doesn't have to be -1, it just commonly is. All that the standard requires is a negative number outside of the range of a char. (More on that later) and 2) Even when EOF happens to be -1, you still should make entering it different from a normal input. Otherwise, it gets really confusing what should happen in places where "-1" is valid input to a program.

    and about while and getchar, i understand , i should get an input from user, but without defining any char character how does it works ?
    But you did define the variable. It's called grade in your program. It is defined as an int, because getchar() returns an int and as a consequence, EOF is an int.

    Read this please: FAQ > Definition of EOF and how to use it effectively - Cprogramming.com

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    All that the standard requires is a negative number outside of the range of a char.
    I think you meant unsigned char since char can be signed and the idea is that getchar would get the value as an unsigned char converted to int, but then you already wrote "negative" so that would be unnecessary to specify.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No, I was discussing EOF by itself. The inner workings of getchar(), or more specifically, the signedness of char should not matter; I thought that would distract from the point. EOF is a negative integer. If I am wrong about that then I will happily be corrected.
    Last edited by whiteflags; 03-24-2017 at 05:29 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    EOF is a negative integer. If I am wrong about that then I will happily be corrected.
    You are not wrong about that, but you wrote more than that: "All that the standard requires is a negative number outside of the range of a char." Suppose that char is signed. Then -1 is in the range of a char, hence according to you, defining EOF as -1 in such cases would not conform to the standard. But this not true: the standard does not require that EOF be a negative number outside of the range of a char; it requires that EOF be a negative integer, which we can infer from the type of EOF must be within the negative range of an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2017
    Posts
    2
    thank you for all of the answers guys. it's been late . sorry. i had a tough and intense exam weeks. one after another.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any alternative?
    By sCandal2 in forum Windows Programming
    Replies: 4
    Last Post: 01-11-2006, 04:30 PM
  2. gets() alternative
    By Mastadex in forum C Programming
    Replies: 5
    Last Post: 12-12-2004, 12:28 AM
  3. Question for Alternative Solution in Program
    By Beast() in forum C Programming
    Replies: 14
    Last Post: 06-22-2004, 06:52 PM
  4. alternative copy.com ;-)
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-04-2002, 05:58 PM
  5. alternative to cin
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 06-25-2002, 05:14 PM

Tags for this Thread