Thread: Basic problem in C program im trying to run

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Rishon Le Siyon, Hamerkaz, Israel, Israel
    Posts
    13

    Post Basic problem in C program im trying to run

    Hello everyone!
    My name is Or Ellenbogen and im new to the C language (also my first language) and to this community

    I bought myself a book from the local bookstore to start learning the language and today I faced my first problem while created a very simple and basic program for practice.

    The target of the program is to ask the user to input a number > \ < 0 and tell him if it is positive or negative, and after doing so - ask him if to repeat the process.

    After running the program I noticed my loop is stuck (the while loop I guess) and can't get the getchar(); command right.

    Here's the code:

    Code:
    #include <stdio.h>
    void main()
    {
    int x;
    char ch;
    printf("Please enter your number: ");
    scanf("%d" , &x);
    if (x > 0) 
    	printf("Your number is positive!\n");
    
    
    else if (x < 0)
    	printf("Your number is negative!\n");
    
    
    
    
    
    
    while (x > 0 || x < 0) {
    	printf("Enter another number?\n");
    	ch = getchar();
    	getchar();
    }
    	if (ch='y') {
    		printf("Please enter your number: ");
    		scanf("%d" , &x);
    		if (x > 0) 
    	printf("Your number is positive!\n");
    
    
    else if (x < 0)
    	printf("Your number is negative!\n");
    	}
    	else 
    		printf("Thank u for using my software, good bye!\n");
    	flushall();
    	getchar();
    
    
    }
    I'll appreciate your help very much

    thank you !

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
        if (ch='y') {
    Look up the difference between "=" and "==".
    The first assigns a value; the second compares two values.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you should indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    
    void main()
    {
        int x;
        char ch;
        printf("Please enter your number: ");
        scanf("%d", &x);
        if (x > 0)
            printf("Your number is positive!\n");
        else if (x < 0)
            printf("Your number is negative!\n");
    
        while (x > 0 || x < 0) {
            printf("Enter another number?\n");
            ch = getchar();
            getchar();
        }
    
        if (ch = 'y') {
            printf("Please enter your number: ");
            scanf("%d", &x);
            if (x > 0)
                printf("Your number is positive!\n");
            else if (x < 0)
                printf("Your number is negative!\n");
        }
        else
            printf("Thank u for using my software, good bye!\n");
    
        flushall();
        getchar();
    }
    Notice that I indent the body of the function, each if/else block, and the while loop. I also use blank lines to separate parts of the code, but I don't use too many at one go.

    Next, notice that your while loop's condition depends on x, but x does not change in the body of the while loop. This explains why you get an infinite loop. Also, note that there is a difference between = and == that will matter in the code after the while loop.

    By the way, void main should be int main, and I don't recall flushall being in the standard library. You probably want fflush(stdout) instead.
    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
    Registered User
    Join Date
    May 2013
    Location
    Rishon Le Siyon, Hamerkaz, Israel, Israel
    Posts
    13

    Thumbs up

    Thank you both stahta and lasernight
    I understood what you both wrote to me and lasernight i'll bold it to myself to write "clean and smooth" as the community requiers thank for paying attentino to that.

    Still, after reading both of the comments - I couldn't fix my progrem. I succedded to re-enter a number but it's bugged all over. I can't figure out what i've done wrong.
    atm it looks like this:
    Code:
    #include <stdio.h>
    int main()
    {
        int x;
        char ch , ch1;
        printf("Please enter your number: ");
        scanf("%d" , &x);
        if (x > 0) 
            printf("Your number is positive!\n");
    
    
        else if (x < 0)
            printf("Your number is negative!\n");
    
    
        ch = getchar();
        
        while (ch)
        {
            printf("Enter another number?\n");
            getchar();
        
            if (ch=='y')
            {
                
                printf("Please enter your number: ");
                scanf("%d" , &x);
                if (x > 0) 
                    printf("Your number is positive!\n");
    
    
                else if (x < 0)
                    printf("Your number is negative!\n");
            }
    
    
        else 
            printf("Thank u for using my software, good bye!\n");
    }
            flushall();
            getchar();
    
    
    }
    can you tell me what's wrong ?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try
    Code:
        while (ch)
        {
            printf("Enter another number?\n");
            ch = getchar();
    Kurt

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try this slight change to your program, and note the changes and comments.

    Code:
    #include <stdio.h>
    int main(void)
    {
        int x;
        char ch;
        printf("Please enter your number: ");
        scanf("%d" , &x);
        if (x > 0)
            printf("Your number is positive!\n");
     
     
        else if (x < 0)
            printf("Your number is negative!\n");
     
        ch = getchar();
        printf("%d\n",ch); //here, ch will always equal 10 - a newline
        //that is left over from entering x. We can use it, however.
      
        while (ch)  //ch will always be true on the first loop
        {
            printf("Enter another number?\n");
            scanf(" %c", &ch); //note the space before the %c! That space causes scanf
            //to "eat" whitespace in the keyboard (stdin) buffer.
         
            if (ch=='y')  //so now ch is correct
            {
                printf("Please enter your number: ");
                scanf("%d" , &x);
                if (x > 0)
                    printf("Your number is positive!\n");
                else if (x < 0)
                    printf("Your number is negative!\n");
            }
            else
                break; //ch not equal to 'y' 
       }
       printf("Thank u for using my software, good bye!\n");
       getchar();
     
       return 0;
    }
    Your problem is a very common one. getchar() or scanf("%c"...) without a space before the %c, will ALWAYS take a waiting newline char from the stdin, and use it for a char input - because a newline is what it's looking for.

    Integers don't have that problem, since they have a different size than char's have.
    Last edited by Adak; 05-27-2013 at 01:46 AM.

  7. #7
    Registered User
    Join Date
    May 2013
    Location
    Rishon Le Siyon, Hamerkaz, Israel, Israel
    Posts
    13

    Arrow

    Quote Originally Posted by Adak View Post
    Try this slight change to your program, and note the changes and comments.

    Code:
    #include <stdio.h>
    int main(void)
    {
        int x;
        char ch;
        printf("Please enter your number: ");
        scanf("%d" , &x);
        if (x > 0)
            printf("Your number is positive!\n");
     
     
        else if (x < 0)
            printf("Your number is negative!\n");
     
        ch = getchar();
        printf("%d\n",ch); //here, ch will always equal 10 - a newline
        //that is left over from entering x. We can use it, however.
      
        while (ch)  //ch will always be true on the first loop
        {
            printf("Enter another number?\n");
            scanf(" %c", &ch); //note the space before the %c! That space causes scanf
            //to "eat" whitespace in the keyboard (stdin) buffer.
         
            if (ch=='y')  //so now ch is correct
            {
                printf("Please enter your number: ");
                scanf("%d" , &x);
                if (x > 0)
                    printf("Your number is positive!\n");
                else if (x < 0)
                    printf("Your number is negative!\n");
            }
            else
                break; //ch not equal to 'y' 
       }
       printf("Thank u for using my software, good bye!\n");
       getchar();
     
       return 0;
    }
    Your problem is a very common one. getchar() or scanf("%c"...) without a space before the %c, will ALWAYS take a waiting newline char from the stdin, and use it for a char input - because a newline is what it's looking for.

    Integers don't have that problem, since they have a different size than char's have.
    Thank you Adak!
    Finally I see the program running perfectly beside one thing...
    after the user inputs 'Y' to the ch variable the program prints 10, now, I do realize that it has something to do with the "leftover" u mantioned in the notes but I can't figure out what "left over" is... if you can give me deeper details about it i'll really appreciate it.
    Moreover, there are 3 more things that i'd like to know, if I may;
    1. Why did you used printf("%d\n", ch ); before starting the loop? Is it because you want it to be true on the first time the loop starts?
    2. I didn't understand the stdin buffer part, about "eating" white space (what's white space [newbie qotd ] in the program. if you can repeat on the explanation deeper i'll really appreciate it.
    3. Why did you added return 0; in the end of the program? Does it help in any way?

    Thanks Adak!
    Last edited by Or Ellenbogen; 05-27-2013 at 03:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with a really basic program
    By KarenC in forum C Programming
    Replies: 5
    Last Post: 03-31-2012, 09:27 AM
  2. basic linked list program problem
    By Hybodus in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2011, 08:35 PM
  3. Help needed (problem with basic C program)
    By EpicYuzer in forum C Programming
    Replies: 15
    Last Post: 11-11-2010, 05:38 PM
  4. a little problem in a basic "if" program
    By elton_fan in forum C Programming
    Replies: 5
    Last Post: 01-10-2007, 12:18 PM
  5. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 04:41 AM

Tags for this Thread