Guess the square program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bravoss
    New Member
    • Oct 2008
    • 3

    Guess the square program

    First of all helo to everyone. I'm new to this forum and I'm also new to C++. I'm in first year of university and we have programming lectures from C++. We had a test today and I couldn't design one simple program, and I still can't find out what I'm doing wrong.

    So the task of the program is:

    Design and implement a program that asks the user to enter a number and then guess at the square of the first number. If the user guesses correctly a congratulatory message is displayed, otherwise a commiseration message is displayed along with the correct answer.

    This is how I tried to design a program:

    C++ Code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    
    {
    int number1;
    int number2;
    
    std::cout<<"Enter number to be squared: ";
    std::cin>>number1;
    
    std::cout<<"Guess the square: ";
    std::cin>>number2;
    
    If (number2==number1*number1);
    std::cout<<"Good Guess"<<endl;
    
    If (number2!=number1*number1);
    std::cout<<"Not Correct."<<number1<<"squared is"<<number1*number1<<endl;
    
    system("pause");
    return 0;
    }
    These are the errors: Line 16: 'if' undeclared(firs t use this function) (each undeclared identifier is reported only once for each function it appears in.) Thanks in advance.
    Last edited by JosAH; Oct 29 '08, 05:15 PM. Reason: added [code] ... [/code] tags
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Your program says "If" instead of "if". That is, the 'i' should be lower-case.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      I have added &#91;code] &#91;/code] tags round your code, it makes it easier to read.

      Is this an exact copy of your code?

      You have failed to say what problems you are having, it is important in the field of programming to be able to express your problems in detail and fully so you will be able to communicate properly with your colleagues.

      If you are getting compiler errors then list them and say why you do not understand them, most compiler errors are actually quite clear in the error they express. For instance "missing ; before return" means exactly that. What you have to what out for is sometimes the line the error is reported on is not the line the error is actually on.

      The line number given in the compiler diagnostic message is normally one of
      • The line of code the error is on.
      • The line of code after the line of code the error is on.
      • The last line of code of the function that the line of code with the error is in.
      • The last line of code of the file that the line of code with the error is in.


      You need to learn to differentiate these positions (especially the first 2), my example error message is of the second type, that is reported on the line of code after the line of code the error is on.

      You have single character errors on lines 15, 18 and 21.

      Comment

      • bravoss
        New Member
        • Oct 2008
        • 3

        #4
        It works!

        Damn, I totally forgot that commands are case sensitive.

        Also in if statements I've put a semicolon before cout. It seemed illogical but I've still put because I've seen the guy sitting next to me did it too and his program worked(obviousl y totally different program).

        I was also bit confused when in the last line(return 0) it said I forgot to put a semicolon before return 0. I was wondering why the hell do you have to put it before, and didn't realise that it actually means I forgot to put it at the end of the previous line(system("pa use"))

        It's all clear now. The program works perfectly, but this is just a beginning. Thanks a lot for the support!

        Comment

        Working...