Thread: Hello! New to C++ need help on a certain code.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    9

    Hello! New to C++ need help on a certain code.

    Im not sure how to explain this, so I will do it the only way I know how..

    Here is the code:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {int answer;
    cout<<"what is 4+4:";
    cin>>answer;
    cin.ignore();
    if( answer<8)  {
           cout<<"you are wrong please try again!\n";
           }
           else if (answer>8) {
                cout<<"You are wrong please try again!\n";}
                else if(answer=8){
                     cout<<"You are right great job!\n";}
                     cin.get();}
    What I want it to do is if you answer the wrong answer you get the question over again so as to answer it again....

    As I said I'm new to C++ as in I started last night 11/14/06 at 11PM.

    Please go easy on me

    Thanks
    -Fabrik8

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    what you're looking for is a loop, there's a while, do while, and for loop. i'll show you how to make it a while loop.

    and, != means not equal to, instead of checking for less than and greater than.
    Code:
    cout<<"what is 4+4: ";
    cin<<answer;
    
    while(answer!=8)
    {
    	cout<<"no that's wrong, try again: ";
    	cin>>answer;
    }
    cout<<"yay you got the right answer\n";

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    It's worth nothing that = does not check for equality between two terms. It is an assignment operator; it assigns values to variables. What you want is == which checks if both terms are equal. Always remember that, it could cause you many headaches.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    Quote Originally Posted by pik_d
    what you're looking for is a loop, there's a while, do while, and for loop. i'll show you how to make it a while loop.

    and, != means not equal to, instead of checking for less than and greater than.
    Code:
    HERE------------>cout<<"what is 4+4: ";
    cin<<answer;
    
    while(answer!=8)
    {
    	cout<<"no that's wrong, try again: ";
    	cin>>answer;
    }
    cout<<"yay you got the right answer\n";
    Im getting errors in this code...I marked where the error is occuring (line 1 @ cout)

    Using Dev-C++ Compiler.....

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    i ment for you to put that in your int main()

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    	int answer;
    
    	cout<<"what is 4+4: ";
    	cin<<answer;
    
    	while(answer!=8)
    	{
    		cout<<"no that's wrong, try again: ";
    		cin>>answer;
    	}
    	cout<<"yay you got the right answer\n";
    
    	return 1;
    }
    there's the entire code.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    Quote Originally Posted by pik_d
    i ment for you to put that in your int main()

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    	int answer;
    
    	cout<<"what is 4+4: ";
    	cin<<answer;
    
    	while(answer!=8)
    	{
    		cout<<"no that's wrong, try again: ";
    		cin>>answer;
    	}
    	cout<<"yay you got the right answer\n";
    
    	return 1;
    }
    there's the entire code.
    Yeah I got that, but it still doesnt work.....

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Code:
    cin<<answer;
    This one is probably a typo.

    Change it to:
    Code:
    cin>>answer;
    Also, you can add this line before "return 1" to pause.
    Code:
    cin.get();

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    Ok heres what I have, I moddified the code to do what i wanted it to do, but at the end result (the correct answer) where it is supposed to say "yay you got the right answer" Im getting nothing but program terminating and the window closing... Is there a way to stop this?

    Here is what I have so far...

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    	int answer;
    
    	cout<<"what is 4+4: ";
    	cin>>answer;
    
    	while(answer!=8)
    	{
    		cout<<"no that's wrong, try again:\n ";
    		cout<<"What is 4+4:"; cin>>answer;
    	}
    	cout<<"yay you got the right answer\n";
    	cout<<"Congratulations! Please press enter";
    
    	cin.get();return 1;
    }
    EDIT: Nevermind I figured it out :

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    	int answer;
    
    	cout<<"what is 4+4: ";
    	cin>>answer;
    
    	while(answer!=8)
    	{
    		cout<<"no that's wrong, try again:\n ";
    		cout<<"What is 4+4:"; cin>>answer;
    	}
    	cout<<"yay you got the right answer\n";
    	cout<<"Congratulations! Please press enter";cin>>answer;
    
    	cin.get();return 1;
    Last edited by Fabrik8; 11-15-2006 at 11:29 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In your original code you hade cin.ignore(); after the cin >> answer. That ignores the newline from when the user hits enter after typing an answer. You can use that technique again instead of your solution, since cin.ignore(); is a more common approach to that issue.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    Quote Originally Posted by Daved
    In your original code you hade cin.ignore(); after the cin >> answer. That ignores the newline from when the user hits enter after typing an answer. You can use that technique again instead of your solution, since cin.ignore(); is a more common approach to that issue.

    Great tip I love this (and I know this is remedial crap but im having fun learning!)

    Can I start over with a new question after the sin.ignore()?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. Assuming you use it after cin >>, that will just ignore the newline and you can continue your program. In fact, sometimes it is wise to always use cin.ignore() after cin >> if you are going to be using functions like get() or getline() that will be tripped up by the leftover newline.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    This is the simplest method ever! :-
    Try it!
    Code:
    #include <iostream.h>
    void main()
    {
     int answer;
     cout<<"what is 4+4:";
     cin>>answer;
     if(answer==8)                                        //"==" is there to check if answer is equal to 8!
    	 cout<<"The answer is Correct!";
     else
    	 cout<<"The answer is Wrong!";
    }

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >#include <iostream.h>
    >void main()

    Dispite both of the above in your example being dated. Main returns an int and drop the .h in iostream header declarations

    I also find it useful to place braces around if statements
    Double Helix STL

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    Ok I wanted to say thank you to everyone who has replied, thanks for all the support...

    Next i would like to get your oppinion on my code and let me know if its sloppy or ok?

    Would be greatly appreciated....

    PS. This is just an excercise for me I dont mean to be stuck on the same thing but ive only been writting code for 3 days now and "you gotta start somewhere" right?

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    	int answer_1;
    	
    
    	cout<<"What is 1+1:" ;
     
        
        cin>>answer_1;
    cin.ignore();
    	while(answer_1!=2)
    	{
    		cout<<"No that's wrong, try again:\n ";
    		cout<<"What is 1+1:"; cin>>answer_1;
    	}
    	cout<<"Yes you got the right answer\n";
    	cout<<"What is 1+2:";
        int answer_2;
        cin>>answer_2;
        cin.ignore();
        while(answer_2!=3)
        
                         {cout<<"No that is wrong, try again:\n";
                         cout<<"What is 1+2:";cin>>answer_2;cin.ignore();}
                         
        cout<<"Yes you got the right answer!\n";
        cout<<"What is 1+3:";
        int answer_3;
        cin>>answer_3;
        cin.ignore();
        while(answer_3!=4)     
        
                               {cout<<"No that is wrong, try again:\n";
                               cout<<"What is 1+3:";cin>>answer_3;cin.ignore();}
                               
                               cout<<"congratulations you suck at math!";cin.ignore();
                             
                         
                         
                        
        
     cin.get();return 3;}
    Thanks....
    -Fabrik8

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> let me know if its sloppy or ok?

    The code itself is ok. You can do other things to minimize the amount of repeated code. You can eventually learn functions and return top this code and re-do it with a function or two.

    The formatting is sloppy. Part of that is because you are mixing tabs and spaces, which don't work well when posted on the forum (your IDE probably uses 4 spaces per tab, but the forum uses 8). Try using only tabs, or changing your IDE options to convert tabs to spaces immediately.

    Another reason it is sloppy is the placement of your braces and short lines of code. Each statement should be on its own line, so this line is bad:
    Code:
    cout<<"What is 1+2:";cin>>answer_2;cin.ignore();}
    I would be consistent and make it look like this:
    Code:
        while(answer_2 != 3)
        {
            cout << "No that is wrong, try again:\n";
            cout << "What is 1+2:";
            cin >> answer_2;
            cin.ignore();
        }
    Much nicer and easier to read. You don't have to do it exactly that way, but that should give you an idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM