Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

can anyone help me with my program?

Hi everyone. I'm trying to write a program for a college course I'm taking. It is supposed to simulate a driving a car (it's VERY basic). Well, when I compile my program I get two errors...

prog20.cc: In function `int main()':
prog20.cc:86: parse error before `{' token
prog20.cc: At global scope:
prog20.cc:100: parse error before `while'

in this function...

void displayMenu(int &choice)
{ //This is line 86
do
{
cout << "--- CAR SIMULATOR 1.0 ---" << endl;
cout << " 1 --> Drive Car" << endl;
cout << " 2 --> Refuel Car" << endl;
cout << " 3 --> Crash Car" << endl;
cout << " 4 --> Buy a New Car" << endl;
cout << " 5 --> Paint Car" << endl;
cout << " 0 --> Quit..." << endl;
cout << endl << "Enter the number of an option: ";
cin >> choice;
if(choice < 0 || choice > 5)
cout << "unknown command..." << endl;
}while(choice < 0 || choice > 5); // This is line 100
}

... and I cant figure them out. Probably something really basic that I just keep overlooking... Can anyone help? If you need more of the code just ask and I'll post it. Thanks
[1468 byte] By [nick53182] at [2007-11-11 7:25:47]
# 1 Re: can anyone help me with my program?
The first line of the output is "In function int main()", is it possible that you forgot to close } in the main function? C++ doesn't allow nested procedures.
evlich at 2007-11-11 21:02:19 >
# 2 Re: can anyone help me with my program?
Does your variable "choice" have a value when you pass it to displayMenu()?
nspils at 2007-11-11 21:03:19 >
# 3 Re: can anyone help me with my program?
I am going with evlich here -- you most likely have a {} alignment mistake above the complaint
jonnin at 2007-11-11 21:04:18 >
# 4 Re: can anyone help me with my program?
In short show us how your main() looks like. The problem lies there, somewhere.
Danny at 2007-11-11 21:05:24 >
# 5 Re: can anyone help me with my program?
Here ya go... my program up to the errors.

#include <iostream>
#include <string>
using namespace std;

struct Vehicle
{
int year;
string model;
string color;
int miles;
float mpg;
float gas;
float tank;
};

void displayMenu(int &);

void printVehicle(Vehicle &);
void initVehicle(Vehicle &);

void driveVehicle(Vehicle &);
void refuelVehicle(Vehicle &);
void crashVehicle(Vehicle &);
void readVehicle(Vehicle &);
void paintVehicle(Vehicle &);
bool quit();

int main()
{
int choice;
cout << endl;
Vehicle v1;
initVehicle(v1);

bool done = false;

while(!done)
{
printVehicle(v1);

displayMenu(choice);

if(choice == 1)
{
driveVehicle(v1);
}

if(choice == 2)
{
refuelVehicle(v1);
}

if(choice == 3)
{
crashVehicle(v1);
}

if(choice == 4)
{
readVehicle(v1);
}

if(choice == 5)
{
paintVehicle(v1);
}

if(choice == 0)
{
done = quit();
}

return 0;
}

void displayMenu(int &choice)
{ //THIS IS LINE 86
do
{
cout << "--- CAR SIMULATOR 1.0 ---" << endl;
cout << " 1 --> Drive Car" << endl;
cout << " 2 --> Refuel Car" << endl;
cout << " 3 --> Crash Car" << endl;
cout << " 4 --> Buy a New Car" << endl;
cout << " 5 --> Paint Car" << endl;
cout << " 0 --> Quit..." << endl;
cout << endl << "Enter the number of an option: ";
cin >> choice;
if(choice < 0 || choice > 5)
cout << "unknown command..." << endl;
}while(choice < 0 || choice > 5); //THIS IS LINE 100
}

...(there is more to the program but its left out for space)
nick53182 at 2007-11-11 21:06:23 >
# 6 Re: can anyone help me with my program?
It's probably a bad habit, but I like to simplify main as much as possible and do all my work in functions... I don't know why...
nick53182 at 2007-11-11 21:07:27 >
# 7 Re: can anyone help me with my program?
yep, looks like the while loop is missing a bracket.

{
int choice;
cout << endl;
Vehicle v1;
initVehicle(v1);

bool done = false;

while(!done)
{
... bunch of matched if pairs
if(choice == 0)
{
done = quit();
}
-------there is no end while here!!
return 0;
}
jonnin at 2007-11-11 21:08:26 >
# 8 Re: can anyone help me with my program?
oh... haha. i knew i was that simple. Thanks
nick53182 at 2007-11-11 21:09:24 >