C++ Coursework Issues

Hi people.

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.

Thank you very much!
Tom Yallop
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?!


Thanks
Tom Yallop
Next time, use [code][/code] tags around your code, it makes it easier to read.

Anyway:
1
2
3
4
cout << “1. Circle” >> << cin >> menuChoice1 >> endl;
cout << “2. Triangle” << << cin >> menuChoice2 << endl;
cout << “3. Rectangle” << cin >> menuChoice3 << endl;
cout << “4. Quit” << << cin >> menuChoice4 << endl;

These will not work at all.
The way you are doing it is completely wrong, read this:
http://www.cplusplus.com/doc/tutorial/basic_io.html

You also only have one variable called "menuChoice", there are no variables called menuChoice1,2,3,4.

Read: http://www.cplusplus.com/doc/tutorial/variables.html

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 ^^)

Thanks for the help firedraco,

The downloads finished now, and I've started typing my code in.
I got as far as:

[code]
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()

{
int menuChoice1 = 0;
int menuChoice2 = 0;
int menuChoice3 = 0;
int menuChoice4 = 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;


// CIRCLE
if (menuChoice1 = 1);
{
int circumference = 0, area = 0, diameter = 0, pi = 0, menuChoiceOne1 = 0;
cout << "Please choose an option: " << endl;
cout << "1. Circumference" << cin << menuChoiceOne1 << endl;
cout << "2. Area" << cin << menuChoiceOne1 << endl;
}

}
[\code]

And I get the:
error C2059: syntax error : 'if' error.
Not quite sure what I'm doing here.

Thanks
Tom Yallop
Did you read the section on the iostreams? The problem is you are are mixing cout/cin, which you can't do.
Last edited on
Okay, I understand that now, thanks.

So, do you think this is okay?:

[code]
int menuChoice1;
int menuChoice2;
int menuChoice3;
int menuChoice4;

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;

// 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.

Is there anything wrong with my "if" statement?


Thanks
Tom Yallop
BTW, you need [/code] at the end, not [\code].

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?
You need to remove any semicolon ; at the end of each if line you have.
Your if statements are still wrong. Like firedraco said in an earlier post, == checks for comparisons and = sets variables, so your first if

 
if (menuChoice1 = 1);


does not make sense, it should be

 
if(menuChoice1 == 1)
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.

http://www.cplusplus.com/doc/tutorial/
Thanks for the help guys, it really means a lot, my coding has improved now.
Mainly thanks to those tutorials, they were very useful!

Thanks again,
Tom Yallop

Topic archived. No new replies allowed.