Need some help regarding some very elementary code.

Hey, everyone! I'm taking a beginner's C++ course and am having trouble with my first assignment. I read the textbook and worked on practice programs but I get the errors 'expected ';' before any variable and that I didn't declare any variables in the scope. If you guys see what's wrong can you point me in the right direction? Thanks!

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


	int main()
{
	int triangle height, wall height, triangle width; 
	
	cout << "Hey, user. Can you do me a favor and give me some measurements to calculate an area? "; 
	
	cout << "To start, what's the wall height? ";
	cin >> wall height;
	
	cout << "Alright cool, thanks! Now, what's the triangle width? "; 
	cin >> triangle width; 
	
	cout << "Awesome, just one more. Lastly, what's the triangle height? ";
	cin >> triangle height; 
	
	area = triangle height * triangle width / 2 + wall height * triangle width; 

	cout << "The area of the A-frame house is " << area << ".\n"
	return 0;
}
you haven't defined a wall or a triangle.

, wall height //what is a wall?
Last edited on
You are putting spaces in your variable names, remove the spaces. If you want to use more than one word names for your variables use an underscore or camel case.

Camel case just capitalize the first letter in each word but do not use spaces. thisIsCamelCase.
Do what @MrGoat said. I usually prefer to use underscore (_). Here is what the code should look like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std; 


	int main()
{
	int triangle_height, wall_height, triangle_width; 
	
	cout << "Hey, user. Can you do me a favor and give me some measurements to calculate an area? "; 
	
	cout << "To start, what's the wall height? ";
	cin >> wall_height;
	
	cout << "Alright cool, thanks! Now, what's the triangle width? "; 
	cin >> triangle_width; 
	
	cout << "Awesome, just one more. Lastly, what's the triangle height? ";
	cin >> triangle_height; 
	
	area = triangle_height * triangle_width / 2 + wall_height * triangle_width; 

	cout << "The area of the A-frame house is " << area << ".\n"
	return 0;
}
Last edited on
It ran! Thanks, everyone!

My professor also asked for it to be printed in square feet and to the second decimal place and I'm not sure how to do that.. any tips?
You can't do that with a INT value, so you may have to change at least the total value to a double...

post your current code so we have a better idea what to suggest.
In order to print decimal value you need float or double . For a fixed number of decimal points you'll need setprecision() which is in #include<iomanip>
The way to use that is the following:
1
2
3
4
5
6
7
8
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
double q;
cin>>q;
cout<<fixed<<setprecision(2)<<q; //fixed, with a precision of 2 decimal points.
}

Input:
 2.325435 

Output:
 2.33 

Try it.

http://www.cplusplus.com/reference/iomanip/setprecision/
Last edited on
In order to find that using square feet, you need to use a variable where you convert square meters to square feet. One square meter is 10.764 square feet.
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
#include <iostream>
using namespace std; 


	int main()
{
	int area, triangle_height, wall_height, triangle_width; 
	
	// This first line establishes what needs to be done.//
	cout << "Hey, user. I need some measurements to calculate an area. Can you help me out? You can?! Thanks!\n"; 
	
	//This one asks for the height of the wall on the house, specifying that the user need not enter the height including the triangle.//
	cout << "Okay, to start, what's the height of the wall on the A-frame house? JUST the wall, not the whole house.";
	cin >> wall_height;
	
	//Same here, only thing needed is the triangle width (aka the wall width since it lines up with the triangle perfectly.)//
	cout << "Alright cool, thanks! Now, what's the triangle width?"; 
	cin >> triangle_width; 
	
	//Last line of input simply asks for the triangle height.//
	cout << "Awesome, just one more. Lastly, what's the triangle height? ";
	cin >> triangle_height; 
	
	//Once the system knows the integer values for all three variables, it does the following equation.//
	area = triangle_height * triangle_width / 2 + wall_height * triangle_width; 
	
	//This simply calculates the 1/2bh area of the triangle and adds it to the length * width of the rectangle i.e. the triangle width, since they're interchangeable.//
	cout << "Thanks for your help! The area of the A-frame house is approximately " << area << " square feet.\n";
	
	return 0;
}


Here's the final program that ran and that I'm going to turn in. I just couldn't get the final area to print with a couple decimal places, if the calculation turned out to be, say, 47.5, it would only print 47. But we've only learned 'float' so far, we haven't learned setprecision and were told specifically to only use iostream.
Declare every variable with float: float area, triangle_height, wall_height, triangle_width; and then turn it in. I believe it would be more correct. As for the 2 decimal points count, I don't know any way to do it without using setprecision()
As for the 2 decimal points count, I don't know any way to do it without using setprecision()

See member function precision()
http://www.cplusplus.com/reference/ios/ios_base/precision/
See member function precision() .


That is indeed iostream, but from what I understand, they were told to make their own algorithm for doing that task.
Topic archived. No new replies allowed.