CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Mar 2015
    Posts
    8

    Problem using Getline and IF operation.

    Hello everyone, I am new to C++ and I am just trying to get a code going for a mock test and to get use to the getline and IF operations, but it seems I have ran into an issue. http://ideone.com/FefAU2 is a link to the code I have written, and I can use getline to give a value to my variable, but it seems like it gets lost once I try to use the IF function. Am I not using the right variable type? Or is there just a whole other way to go about what I am trying to accomplish that is more efficient and proper? I thank you for any assistance or tips.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem using Getline and IF operation.

    Please attach your code to your post.
    See the Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2015
    Posts
    8

    Re: Problem using Getline and IF operation.

    PHP Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    int a,b,c,d,results;
    string enter;
     
    int main()
    {
        
    cout << "How are you doing today?\n";
        
    getline (cin,enter);
        
    cout << "I am glad you are doing " << enter << " today.\n";
        
    cout << "Would you like to take a test today?\n";
        
    getline (cin,enter);
        
    cout << "Good I promise it will be fun.\n" << "Lets get started..\n";    
        
    cout << "For the first question, what is 4x3 equal to? \n";
     
       
    getline (cin,enter);
     
       if(
    enter=12)
    {
     
    cout << "CORRECT!";
    }    
        else
    {
        
    cout << "Sorry, that was not correct.";
    }
        
    system("pause");
        return 
    0;
     } 

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem using Getline and IF operation.

    Quote Originally Posted by Goombagoo View Post
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    int a,b,c,d,results;
    string enter;
     
    int main()
    {
       ...
       getline (cin,enter);
     
       if(enter=12)
    {
     cout << "CORRECT!";
    }    
        else
    {
        cout << "Sorry, that was not correct.";
    }
        system("pause");
        return 0;
     }
    First, please, next time use code tags (#), not the php ones.
    Second, in the if statement you ar using the assignment (=) rather than the comparison (== or != or some other) operator.
    Third, your enter is of type std::string, so why do you try to "compare" it with an integer number?
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2015
    Posts
    8

    Re: Problem using Getline and IF operation.

    I will remember to use the code tags next time, thank you. Also I realized my mistake on using (=) instead of (==) as a comparison. I didn't realize that string could not be used for numerical values, but was suspecting that might be an issue. I was able to resolve it using "sstream" however. It is only my second day of learning C++ so I apologize for my mistakes, was hard to find a clear answer while browsing the web so I appreciate the help, thank you.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem using Getline and IF operation.

    WEll, string in not a number!
    However, it can contain characters used to write the digits: '0', '1', ...'9'.

    Perhaps, you meant to compare this string with the text "12"?
    Like
    Code:
    if(enter == "12")
    {
          cout << "CORRECT!";
    }    
    else
    {
          cout << "Sorry, that was not correct.";
    }
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2015
    Posts
    8

    Re: Problem using Getline and IF operation.

    I just wanted to have the user input a number and then have the code see if the answer was right or wrong, and depending whether it is right or wrong to display a respond message. I was using string, cause I wasn't sure of another way to have the user set a numerical value to a variable I was trying to use later in the code.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem using Getline and IF operation.

    Did you try something like
    Code:
    ...
    int userInput;
    ...
    cin > userInput;
    if(userInput == 12)
    {
         cout << "CORRECT!";
    }    
    else
    {
          cout << "Sorry, that was not correct.";
    }
    Victor Nijegorodov

  9. #9
    Join Date
    Mar 2015
    Posts
    8

    Re: Problem using Getline and IF operation.

    I really don't know why I did not, but I just did and yes it works perfectly. I felt like it would be more complicated than that and that is why I got stuck using the "getline" operation. I appreciate the time you have spent assisting me though, thank you so much. Now I will see what else I can add to my test to help me better understand how C++ works. Thank you again.

  10. #10
    Join Date
    Mar 2015
    Posts
    8

    Re: Problem using Getline and IF operation.

    I am sorry, last question if I may, but I want to know if there is anyway to set the output for an IF function and to reuse it throughout a code without having to fully type it out. Like I can keep
    if(userInput == 12)
    {
    cout << "Correct!";
    }
    else
    {
    cout << "Sorry, that was not correct.";
    }
    and I would make that the default output without having to re type it for question number 2. Is there a way to do that at all?

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem using Getline and IF operation.

    You are welcome!
    Victor Nijegorodov

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem using Getline and IF operation.

    Quote Originally Posted by Goombagoo View Post
    I am sorry, last question if I may, but I want to know if there is anyway to set the output for an IF function and to reuse it throughout a code without having to fully type it out. Like I can keep
    if(userInput == 12)
    {
    cout << "Correct!";
    }
    else
    {
    cout << "Sorry, that was not correct.";
    }
    and I would make that the default output without having to re type it for question number 2. Is there a way to do that at all?
    Do you mean a function? See http://www.learncpp.com/cpp-tutorial...-at-functions/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Mar 2015
    Posts
    14

    Re: Problem using Getline and IF operation.

    I was just going to say write a function for that code and call it when you want to use it, but I was kind of beat to it.

  14. #14
    Join Date
    Mar 2015
    Posts
    8

    Re: Problem using Getline and IF operation.

    Yes, I think that is what I want to do at least. I just want it to keep asking the user the question until they get the question answered right, aka the statement becomes true and not false. I have to look up how to call a function thought, cause I have no idea how to do so. Thank you all again for the support though.

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem using Getline and IF operation.

    In that case you need a loop which continues while a condition is true. There are for, while and do loops. See http://www.learncpp.com/cpp-tutorial...-introduction/ and subsequent.

    Consider
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    bool getAnswer(const string& str, int ans)
    {
    	const int tries = 3;
    	int userInput;
    
    	cout << endl << str << endl;
    	for (int cnt = 1; (cout << "Please enter your answer: ") && ((!(cin >> userInput) && (cnt < tries)) || (cin && ((userInput != ans) && (cnt < tries)))); cnt++) {
    		if (!cin) {
    			cin.clear();
    			cin.ignore(1000, '\n');
    			cout << "Invalid answer" << endl;
    		}
    		else
    			cout << "Incorrect answer" << endl;
    	}
    
    	if (userInput != ans)
    		cout << "You have had " << tries << " tries." << endl << "Correct answer is " << ans << "." << endl;
    	else
    		cout << "Correct!" << endl;
    
    	return (userInput == ans);
    }
    
    int main()
    {
    int cnt = 0;
    
    	cnt += getAnswer("What is 3 + 4?", 7);
    	cnt += getAnswer("What is 5 + 6?", 11);
    
    	cout << endl << "You got " << cnt << " correct" << endl;
    
    	return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured