473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5.  
  6. {
  7. int number1;
  8. int number2;
  9.  
  10. std::cout<<"Enter number to be squared: ";
  11. std::cin>>number1;
  12.  
  13. std::cout<<"Guess the square: ";
  14. std::cin>>number2;
  15.  
  16. If (number2==number1*number1);
  17. std::cout<<"Good Guess"<<endl;
  18.  
  19. If (number2!=number1*number1);
  20. std::cout<<"Not Correct."<<number1<<"squared is"<<number1*number1<<endl;
  21.  
  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
3 1724
donbock
2,426 Expert 2GB
Your program says "If" instead of "if". That is, the 'i' should be lower-case.
Oct 29 '08 #2
Banfa
9,065 Expert Mod 8TB
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
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

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

Similar topics

4
by: cplusplus | last post by:
Hello, I have newbie question. I'm stuck on this current assignment. Write a program that prompts the user for two integer values, passes the values to a function where they are multiplied...
4
by: winnerpl | last post by:
Hey guys I'm trying to get a magic square but I'm stuck with it printing out only 1's and 0's in random places. Hope you experts can can provide some input on what I'm doing wrong. #include...
1
by: shaveta | last post by:
pls guide me to write a program in c to generate a magic square of size n*n , where n is odd A magic square is an n*n matrix of integer from 1 to n^2, where n is odd, such that the sum of every...
5
by: punkybrewster | last post by:
My task is to write a program that reads in from the user the size of the sides of a square and prints a hollow square of that size in asterisks and spaces. A nested loop should be done. An example...
5
by: magic man | last post by:
I need help ... I have very rudimentary VB skills. I am working on a topographical model of a magic square. I consider each cell in the square to be a solid structure to the height specified by the...
1
by: magic man | last post by:
I am 50 years old ...and am working physical models of the math structure called a magic square .. for my own interest. My present problem is this. I have a topograhical model for the square...
3
by: JWest46088 | last post by:
Hello everybody. I'm having a little trouble using recursion. I need to accept input from the user and display the input in a square. It has to be done recursively. I would be able to do it...
5
by: WanHongbin | last post by:
#include <stdio.h> double square(); /*without declare main() { double s; s = square(2); printf("%g\n", s); }
6
by: Blue sky | last post by:
Hi ,I think the follow program is right in logical But why the compiler output :"square:declared identifier" #include<stdio.h> #include<math.h> int main() { double x1;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.