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>;
usingnamespace 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?
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
#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 ==========
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).
@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: ???
@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.