Square area

Write the program, square.cpp, to calculate and print the area of a square. Assume that the sides of the square are all positive whole numbers (use integers). I am getting a lot errors messages this due tomorrow and am completely lost and confused help ASAP.


Algorithm used:

Begin

define side as an integer
define area as an integer
write "Enter the size of a side of a square "
input side
set area to side * side
write "The area of the square is "
write area

End

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>;
#include <cmath>;
using namespace std;

// declaring variables
int side;
	
	cout << "Enter the size of a side of a square ";
    cout << endl;

int area;

	area = side * side;

	cout << "The area of the square is "
		cout << area

}



This is the error I am receiving:
c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\square\square\square.cpp(26): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?

there is no "StdAfx.h"'
Last edited on
First of all no idea what lines 1 - 13 mean so I'm gonna ignore them.

Line 31: I guess it should have been area = num * num since you just declare area before it.

Line 33, 34: Can become cout << "The area of the square is: " << area;

And why the variable value if you never use it??
Last edited on
the frist 13 lines were added without my prompt not sure if needed anyone else know how to write this I know I need cin and cout other then that I am not sure what I am doing wrong
Last edited on
When you create your project in MS VC++ 2010 you should select option Win32 Console application.
Last edited on
vlad your suggestion was hopefully but I am still having problems with this code so I started over
OK, so in VS you must include stdafx.h if you are using a precompiled header. So just do it.
Next,

You don't need semicolons after #includes (or any other preprocessor command)

The lines 5 through 18 need to be part of a function or maybe main()?

Line 18, the } does not have a corresponding {

Also,

At no point in this code are you requesting input.
You need cin >> side; //requests input from the user

If this code were to compile, it would crash on runtime.

You need a semicolon after lines 15 and 16.

The code area = side * side; does not do what you think it does.
Remember the order of operations!

Hope this helps.
Last edited on
Superdude,

here's what I have tried with your suggestions what command am I looking for when I am trying to multiply

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include "stdafx.h"
#include <iostream>
#include <cmath>


int _tmain(int argc, _TCHAR* argv[])
int main ()
	{

int side;
int area;

// declaring variables

   std::cout <<"Enter the size of a side of a square ";
   std::cin >> side; 
   
   //requests input from the user ;
	
	
   area = (side * side);

   std::cout <<"The area of the square is " n/;
   std::cout <<area;
//executing problem
   
   return 0;
   

}


still getting errors


1>------ Build started: Project: square, Configuration: Debug Win32 ------
1>  square.cpp
1>c:\users\amy\desktop\computer science\mircosoft visual c++ express 2010\codes for class\temp\square\square\square.cpp(4): fatal error C1083: Cannot open precompiled header file: 'Debug\square.pch': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on
Hi there.

I think you are forgetting the structure of a C++ program. A program first has to have #include s. Then every program must have a main function. An example of a fully working program can be found below:

1
2
3
4
5
6
7
8
#include <iostream> // Includes here (NOTE: No semicolons in #includes
#include "stdafx.h"

int main(void) { // main program that executes code.
	std::cout << "Hello World";
	std::cin.ignore();
	return(0);
}


I think you are forgetting to include int main(void). (In this case, _tmain is the main function).

So drop your main code (ln 17-35) into _tmain (without the extra curly braces and run it), and get rid of the semicolons in the includes and the cmath include (ln 14-15) (you aren't using the header for anything).

Reference: http://www.cplusplus.com/doc/tutorial/program_structure/
Last edited on
@usandfriends: You dont need a void in main(void), just a int main(). :)

But everything else is correct.

When you create a project in VS, sometimes it make a int _tmain(...) for you. Use this as your main, or delete it and use just int main(). But you must have only one entry point to your program like usandfriends explained.
...
Edit: Depending on your VS edition, you may have to change stdafx.h to StdAfx.h or similar. Can you give the full error? fatal error C10083: ???

Thanks
Last edited on
@Superdude:
int main(void) is not incorrect. It's a style, some coders like to have their code more thoroughly defined. If you really want to get upset about implicit vs. explicit declarations, there are a LOT better battles to fight.
I am not upset at all, just telling him he doesn't need it. No bad feelings, or anything of the sort.

To arl... take a look at this:
http://www.cplusplus.com/articles/1TUq5Di1/
Topic archived. No new replies allowed.