Thread: beginner in C, need some help stuck with code that has a bug and cant it figure out

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    2

    Question beginner in C, need some help stuck with code that has a bug and cant it figure out

    Hello I just joined up this is my first post,


    Let me give my background first, I actually just started, i'm real new to c/c++ programming, I was more decided to learn C++ (as projects i'm most interested in use C++ majority of the time including game programming) very recently but the only friends i have that are advanced experienced programmers that have C/C++ skills are C programmers, they sort of told me to try and start with C first for various reasons even if i end up just doing C++ anyways, like the issue that even if i wanted to just code C++ they said theres alot of code in the wild that are mixed with C anyways so knowing some C basics would be helpful for me to understand and it will also give me an idea of the difference of both languages form an opinion of them by myself,

    so i just started C++ anyways and thought to why not check out C, and well i cant talk to them all the time since they are busy senior developers and they are much older with families even, one of them is into compiler design even, anyways so i thought to join this forum for information and advice when i cannot talk to them

    the program i'm having issue with is actually a C program thats why i am posting on the C board, that i learned from a C++ program, I tried to translate from c++ to c, but cant figure out the problem the c program does something un-intentional theres supposed to be a loop to check user input with boolean value, to check if the user wants to quit or not, it never happens unlike in the c++ version which does, i found out that the boolean was just added in c99, i tried compiling for both c99 and c11 with including this line at the beginning even #include <stdbool.h>but im not sure im doing it right, its my suspicion that the problem is to do with boolean and how i'm implementing it, since the program compiles but just doesnt work the same as the c++ one

    any advice or a fix much appreciated, its just a basic program - guess my number game

    this is the C++ version
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main() {
        
        cout << "This is the Guess My Number Game!" << endl;
        cout << "Please type in your number guess below! " << endl; 
        
        int answer = 7;
        int guess;
        char yesOrNo;
        bool keepgoing = true;
        
        while(keepgoing) {
            
            cout << "Enter a Number: ";
            cin >> guess;
            cout << "You entered " << guess << endl;
            
            if (guess == answer) {
                
                cout << "Your Guess is 7, that's the correct number, you win!" << endl;
                
            }
            
            else if (guess < answer) {
                
                cout << "Your guess is lower than the mystery number" << endl;
                
            }
            
            else if( guess > answer) {
                
                cout << "Your guess is higher than the mystery number" << endl;
                
            }
            
            else {
                
                cout << "i'm lost" << endl;
                
            }
            
            cout << "Play Again? (type y for yes or n for no)" << endl; 
            cout << "y or n? " << endl;
            cin >> yesOrNo;
        
        switch(yesOrNo) {
            
            case 'Y':
            case 'y':
            keepgoing = true;
            break;
            
            case 'N':
            case 'n':
            keepgoing = false;
            break;
                    
                } 
                
                /* if else 
                
                if (yesOrNo == 'y') {
                
                keepgoing = true;        
                }
                else if(yesOrNo == 'n') {
                
                keepgoing = false;
                
                }
                else {
                
                break;
                
                */
                
                // (yesOrNo == 'y' || 'Y') ? keepgoing = true: keepgoing = false; // Ternary
                
        }
        
        return(0);
        
    }

    and the C version that doesnt seem to work the same way as the C++ one

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    
    int main(void) {
        
        printf("This is the Guess My Number Game!\n" );
        printf("Please type in your number guess below!\n" );
        
        int answer = 7;
        int guess;
        char yesOrNo;
        //bool keepgoing = true; //tried this, and _Bool no luck 
        _Bool keepgoing = 1;
        
        
        while(keepgoing == 1) {
            
            printf("Enter a Number: ");
            scanf("%d", &guess);
            printf("You entered: %d\n", guess);
            
            if (guess == answer) {
                
                printf("Your Guess is 7, that's the correct number, you win!\n");
                
            }
            
            else if (guess < answer) {
                
                printf("Your guess is lower than the mystery number\n");
    
    
            }
            
            else {
                
                printf("Your guess is higher than the mystery number\n");
                
            }
            
                printf("Play Again? 'y' or 'n'?\n");
                scanf("%c", &yesOrNo);
                // yesOrNo = getchar();
                
        //switch
        switch(yesOrNo) {    
            case 'Y':
            case 'y':
            keepgoing = 1; //tried values 1 and True
            break;
            
            case 'N':
            case 'n':
            keepgoing = 0; //tried values 0 and false 
            break;
            
        }
        
        /* if else
            
                if (yesOrNo == 'y') {
                
                keepgoing = 1;        
                
                }
                
                else if(yesOrNo == 'n') {
                
                keepgoing = 0;
                
                }
        
        */
                //(yesOrNo == 'y') ? keepgoing = 1 : keepgoing = 0; // ternary
                
        }
        
        return(0);
        
    }
    Last edited by albrecht; 05-21-2016 at 06:30 AM. Reason: typos and one on the title i wanted to correct but cant seem to do anyways

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First if you really want to learn C++ then study and learn C++ there is no need to learn C before you learn C++.

    The problem is that scanf() when using the "%c" specifier doesn't skip leading whitespace and when you enter the number the end of line character is left in the input buffer. The easiest way to solve this problem is to force scanf() to skip the leading whitespace when using the "%c" specifier. You do this by placing a space before the specifier " %c".

    Jim

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    2
    wow thanks jimblumberg, truly much appreciated your tip worked i was really stumped trying to figure out what the problem was, to think it was just a simple space that actually was the fix, about the C++ learning just learn C++ i read about it and was on the premise to do just that, but i just gave it a chance to just check C real quick with my buddies advice who are much experienced anyways, and i was pretty surprised that i was going through all the lessons i did in C++ and converting to C, and i got stumped on this one

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No matter which language you start with you should start with one language, don't try to learn both languages at the same time, you'll only confuse yourself. Trying to convert C++ programs to C will get more and more difficult as you progress in learning C++. While these languages are similar they are very very different so learn one language before you try to learn the other.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2014, 12:59 AM
  2. Replies: 9
    Last Post: 04-14-2012, 01:59 PM
  3. Replies: 17
    Last Post: 12-10-2011, 03:36 PM
  4. Im stuck and can't figure this out
    By jturner38 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2010, 04:51 PM
  5. Please help! Beginner stuck on table and array problem!
    By robsmith in forum C Programming
    Replies: 2
    Last Post: 03-10-2005, 11:42 AM

Tags for this Thread