beginner question

This is a discussion on beginner question within the C++ Programming forums, part of the General Programming Boards category; hi, im trying to make this thing that asks you a maths question and you have to type the correct ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    28

    beginner question

    hi, im trying to make this thing that asks you a maths question and you have to type the correct answer, if u type it write it will say correct, if u type it wrong it will say incorrect(or you are dumb lol)
    heres the code:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int correct = 0;
    	int a =5;
    	int b=10; 
    	int c=25;
        cout <<"What is 5 + 10 + 25?= ";
        cin >> correct;
        
    
    	cout << "You said the answer is  "  << correct<<endl;
    	
        if int.correct=40
        {
        cout <<"You are Correct!"<<endl;
    }
    else
    cout <<"lol u r dumb!";	
    		
    
    	system("pause");
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    1. Generally speaking, it ........es the hell out of people to see threads named like "question" or "hElP pLz !!11!1!". Avoid.

    2. Though the errors here are fairly obvious, always post the errors you get when you compile.

    3. Always state what you want the program to do, what you expect it to do in these lines and what it does, it helps a lot to find bugs.

    4. You have 3 variables a, b and c which you never use. You can get rid of them.

    5. "if int.correct=40" contains at least 5 errors. An if() statement starts with an opening parenthesis and ends with a closing parenthesis; moreover, the equality operator is == whereas = is the assignment operator. The difference is simple. The == operator tests for equality and the = operator takes the value to the right and put it in the variable to the left.
    Therefore... "if(correct == 40)" would be correct.

    6. You name a variable 'correct' but it does not mean anything to someone who reads your code. A name like 'answer' would suit the purpose much better.

    7. When you have only one instruction in an if() or 'else' block, you don't need to put brackets around your code; however, if you decide to put brackets for the if() block, it's logical to follow the same rule and put brackets for the 'else' block. Consistency is the rule here.

    8. As we have already told you, 'system("pause");' isn't correct to use. We have already explained the reasons to you. It's up to you now to decide to follow our advices or not but generally speaking I think we know much better what's better coding and you should probably stick to our advices.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    28
    thanks so much but theres one problem, when i use
    Code:
    std::cout.get();
    it will close the program when i press enter, on the other hand if i put
    Code:
    system("pause")
    it wont close it

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    I think you mean the opposite. Just hit enter.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    That is not an excuse.

    You could just run it in a command shell as intended.

    The only time the program really stops is either when you feed it input or it when it runs out of stuff to do closes. Our use of cin.get() is to pause to keep the window open long enough to read the output. That's all. Once we get input, it doesn't matter.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    28
    sorry i mean this code closes it when i write a number then press enter:
    Code:
    std::cin.get();

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Ah, when you hit enter you left a newline in the input buffer.
    Just get in the habit of typing
    Code:
    std::cin.ignore();
    after input.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    I think it would do the job too if you added a flush before each user input.
    Code:
    std::cout << "whatever" << std::flush;
    std::cin.get(); // this will wait
    
    std::cout << "whatever again...";
    std::cin.get(); // this may/will not wait

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    28
    i know i ask too many question(and is annoying) but here is my final code and it works, but how would i make it if the user answers the question right it will show another question similer to the first question?
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int answer = 0;
        cout <<"What is 5 + 10 + 25?= ";
        cin >> answer;
        std::cin.ignore();
    
    	cout << "You said the answer is  "  << answer<<endl;
    	
    	
        if(answer == 40)
        {
        cout <<"You are Correct!"<<endl;
        std::cin.ignore();
    }
    else
    {
    cout <<"lol u r dumb!"<<endl;
    cout <<"The correct answer is 40"<<endl;
    std::cin.ignore();
    }		
    return 0;
    }

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    7
    I can't answer that last question (yet), but from one noob to another, I've got a suggestion and a question. Since you already have a "using" statement (using namespace std), you don't need to put "std::" before your cin.ignore and cin.get.

    My question is: I believe it works correctly without putting "int answer = 0", meaning you could just put "int answer." Is there a reason for putting in the '= 0'?

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    >Is there a reason for putting in the '= 0'?
    Safety. Initializing your variables is good programming practice because you know that the memory you set aside is clear.

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>how would i make it if the user answers the question right it
    will show another question similer to the first question?

    Well naturally you could code out a bunch of questions and it
    could cycle through them, or more adventurously, you could
    randomly generate questions. It would be fairly easy to code
    it so it just asks you to add a set number of numbers together,
    a little more complex to make the length of each question variable
    each time, and harder still to make the operations random (add,
    subtract, multiply and divide). All of these can be performed by
    using a function called rand(). I would not suggest doing it
    just yet though because you appear to be just after starting quite
    recently, but here's some info on how to use rand()

    >>Is there a reason for putting in the '= 0'?

    Yes, it's a good practice to initialise variables because uninitialised
    variables can cause problems that you wouldn't expect - thats
    because when variables are created, they are not initialised
    to 0 as you might expect.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    7
    Quote Originally Posted by citizen
    >Is there a reason for putting in the '= 0'?
    Safety. Initializing your variables is good programming practice because you know that the memory you set aside is clear.
    Quote Originally Posted by Richie T
    Yes, it's a good practice to initialise variables because uninitialised
    variables can cause problems that you wouldn't expect - thats
    because when variables are created, they are not initialised
    to 0 as you might expect.
    Good to know. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21