I've been set some C++ coursework as part of my degree course, but I am new to C++ programming.
I have to write a program, which uses functions, that takes input about the length of sides of shapes and returns the area, perimeter etc.
I'm still downloading Visual Studio at the moment (legally, through the MSDN Academic Alliance Microsoft has with my uni), so I've written my code in MS Word, hence the random capital letters for the "int"s "cin"s and "cout"s.
My code is as follows:
#include "stdafx.h" #include <iostream> using namespace std;
Int main ()
{
Int menuChoice = 0
cout << “Do you require:-“ << endl;
cout << “1. Circle” >> << cin >> menuChoice1 >> endl;
cout << “2. Triangle” << << cin >> menuChoice2 << endl;
cout << “3. Rectangle” << cin >> menuChoice3 << endl;
cout << “4. Quit” << << cin >> menuChoice4 << endl;
If (menuChoice = 1)
{
//CIRCLE
Int circumference = 0
Int area = 0
Int diameter = 0
Int pi = 0
Int menuChoice1.1 = 0
cout << Please choose an option: “
cout << “1. Circumference” >> cin << menuChoice1.1 >> << endl;
cout << “2. Area” >>cin << menuChoice 1.1 >> << endl;
If (menuChoice1.1 = 1);
{
cout << “Please enter the diameter of the circle : “ >> endl;
cin << diameter >> endl;
cout << “Please enter the value of Pi you wish to use: “ >> endl;
cin << pi >> endl;
circumference = diameter*pi
cout <<”The circumference is: “
cout << circumference >> <<endl;
}
If (menuChoice1.1 = 2);
{
cout << “Please enter the diameter of the square: “ >> endl;
cin << diameter >> endl;
cout << “Please enter the value of Pi you wish to use: “ >> endl;
cin << pi >> endl;
area = pi*radius ^2
cout <<”The area is: “
Cout << area >> <<endl;
}
Else If (menuChoice = 2)
{
//TRIANGLE
Perimeter = sideA+sideB+sideC
Int perimeter = 0
Int area = 0
Int sideA = 0
Int sideB = 0
Int sideC = 0
Int height = 0
Int base = 0
Int menuChoice2.1 = 0
cout << Please choose an option: “
cout << “1. Perimeter” >> Cin << menuChoice2.1 >> << endl;
cout << “2. Area” >>Cin << menuChoice 2.1 >> << endl;
If (menuChoice2.1 = 1);
{
cout << “Please enter the length of side A: “ >> endl;
cin << sideA >> endl;
cout << “Please enter the length of side B: “ >> endl;
cin << sideB >> endl;
cout << “Please enter the length of side C: “ >> endl;
perimeter = sideA+sideB+sideC;
cout <<”The perimeter is: “
cout << perimeter >> <<endl;
}
If (menuChoice2.1 = 2);
cout << “Please enter the perpendicular height of the triangle " >>endl;
cin << height >> endl;
cout << “Please enter the length of the triangles base: “ >> endl;
cin << base >> endl;
area = height*base/2
cout <<”The area is: “
cout << area >> <<endl;
}
else if (menuChoice = 3)
{
//RECTANGLE
Int perimeter = 0
Int area = 0
Int height = 0
Int width = 0
Int menuChoice3.1 = 0
Cout << Please choose an option: “
Cout << “1. Perimeter” >> Cin << menuChoice3.1 >> << endl;
Cout << “2. Area” >>Cin << menuChoice 3.1 >> << endl;
If (menuChoice1 = 1);
Cout << “Please enter the height of the rectangle: “ >> endl;
Cin << height >> endl;
Cout << “Please enter the width of the rectangle: “ >> endl;
Cin << width >> endl;
perimeter = height + height + width + width;
Cout <<”The perimeter is: “
Cout << perimeter >> <<endl;
If (menuChoice = 2);
Cout << “Please enter the height of the rectangle: “ >> endl;
Cin << height >> endl;
Cout << “Please enter the width of the rectangle: “ >> endl;
Cin << width >> endl;
area = height*width;
Cout <<”The area is: “
Cout << area >> <<endl;
}
Else
{
Cout << “Goodbye” >> <<endl;
}
}
Now, I'm very new to C++, so new that I've never actually used it before today.
I'm really stuck on this coursework, and I would appreciate the help.
It's also saying that the very first part of my "if" statement is an error:
error C2059: syntax error : 'if' is what I get...any idea what that means?!
Almost all of your if's are screwed up somehow...
No ; at the end of ifs
use "==" to compare things, "=" sets the variable to the right hand value
Again, you only have ONE variable, you don't have stuff like menuChoice2, menuChoice1.1, etc...
Try to fix the above (and get the compilier first, error messages are REALLY helpful ^^)
// CIRCLE
if (menuChoice1 = 1);
{
int circumference, area, diameter, pi, menuChoiceOne1;
cout << "Please choose an option: " << endl;
cout << "1. Circumference";
cin << menuChoiceOne1 << endl;
cout << "2. Area";
cin << menuChoiceOne2 << endl;
if (menuChoiceOne1 = 1);
cout << "Please enter the diameter of the circle: ";
cin >> diameter >> endl;
cout << "Please enter the value of Pi you wish to use: ";
cin >> pi >> endl;
circumference = diameter*pi
cout << "The circumference is: ";
cout << circumference << endl;
}
[\code]
I've got this so far, and I'm still getting the
error C2059: syntax error : 'if'
error.
Most of your cins are still screwed up...they need to be cin >> stuff;
Also, your cins won't work like you think they will. Each time you use cin, the program will stop and wait until the user inputs something. What you want to use is one variable (menuChoice) and use cin on that one (only), then check it to decide what to do. Well, you have a ; at the end that is bad...did you read my entire first post and try to fix all the errors?
It also seems that you have so many syntax errors. My friend it is brave to jump-in and start coding right away but you need to be braver to learn in the process. I recommend "Teach Yourself C++ in 24 Hours" as the fastest (well, 24 hours seem an awesome deal to learn C++ basic programming). Check if you have it in your school library. Otherwise, I would start by the awesome tutorials section on this website.