By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

Guess the square program

P: 3
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  4. int main ()
  6. {
  7. int number1;
  8. int number2;
  10. std::cout<<"Enter number to be squared: ";
  11. std::cin>>number1;
  13. std::cout<<"Guess the square: ";
  14. std::cin>>number2;
  16. If (number2==number1*number1);
  17. std::cout<<"Good Guess"<<endl;
  19. If (number2!=number1*number1);
  20. std::cout<<"Not Correct."<<number1<<"squared is"<<number1*number1<<endl;
  22. system("pause");
  23. return 0;
  24. }
These are the errors: Line 16: 'if' undeclared(first use this function) (each undeclared identifier is reported only once for each function it appears in.) Thanks in advance.
Oct 29 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 2,398
Your program says "If" instead of "if". That is, the 'i' should be lower-case.
Oct 29 '08 #2

Expert Mod 5K+
P: 8,916
I have added [code] [/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.
Oct 29 '08 #3

P: 3
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(obviously 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("pause"))

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

Post your reply

Sign in to post your reply or Sign up for a free account.