What is wrong with my program?

No matter what I do, I keep getting errors and I don't know what is wrong with it. I've been working on this for a day and it's just not happening. about to quit CS altogether here ;_;

//prerequisites being
//constructor that accepts year model & make arguments. constructor should //assign 0 to speed member variables.
//accessor functions to get values stored in yearModel, make, and speed
//brake should subtract 5 from speed each time it's called
//acceleration should add 5 from speed each time it's called


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

class Car
{
public:
int carmodel();
string carmake();
int carspeed();
void brake();
void accelerate();
void displayMenu();
Car(int, string, int);
private:
int yearModel;
string make;
int speed;
};


Car::Car(int year, string makeby, int sp) //sp being speed
{
yearModel = year;
make = makeby;
speed = sp;
}
//get year
int Car::carmodel()
{
return yearModel;
}

//get make
string Car::carmake()
{
return make;
}

//get speed
int Car::carspeed()
{
speed = 0;
return speed;
}

void Car::accelerate()
{
speed = speed + 5;
}
void Car::brake()
{
speed = speed - 5;
}

void displayMenu()
{
cout << "Please choose A or B\n";
cout << "A. Accelerate\n";
cout << "B. Brake\n";
}

int main()
{
int speed = 0;
char selection;

cout << "The speed of the car is: " << speed << endl;
Car one(2007, "Honda", speed);

do
{
displayMenu();
cin >> selection;
while(selection < 'A' || selection > 'B')
{
cout << "Please make a valid selection. A or B.";
cin >> selection;
}


switch (selection)
{
case 'a':
case 'A': cout << "You're accelerating.";
cout << one.accelerate() << endl;
break;

case 'b':
case 'A': cout << "You pressed the brake.";
cout << one.brake() >> endl;
break;
}

return 0;
}
you had a few problems in there.

First you were trying to return a value from a void function.

this one here cout << one.accelerate() << endl;

you tried to return a value from this void function also as well as the cout symbols were the wrong direction cout << one.brake() >> endl;

I changed both of those functions from void to int.

It also looks like you started a do while loop but never completed the thought.

I removed the do and started an infinite loop using while (true){}

To exit the program I used Return 1; when somebody selected the letter C.

you have a few unused functions in the program at the moment but i'm assuming you intend to do something with them once you got the basic program working so here you go :)

You see how I added a new selection you can follow that basic idea to output make and model also.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

class Car
{
public:
    int carmodel();
    string carmake();
    int carspeed();
    int brake();
    int accelerate();
    void displayMenu();
    Car(int, string, int);
private:
    int yearModel;
    int speed;
    string make;
};

//sp being speed
Car::Car(int year, string makeby, int sp){
    yearModel = year;
    make = makeby;
    speed = sp;
    }

//get year
int Car::carmodel(){return yearModel;}

//get make
string Car::carmake(){return make;}

//return current car speed
int Car::carspeed(){return speed;}

//increase car speed
int Car::accelerate(){return (speed = speed + 5);}

//decrease Car Speed
int Car::brake(){return (speed = speed - 5);}

void displayMenu()
{
cout << "Please choose A or B\n";
cout << "A. Accelerate\n";
cout << "B. Brake\n";
cout << "C. Exit Program\n";
}

int main()
{
    int speed = 0;
    char selection;

    cout << "The speed of the car is: " << speed << endl;
    Car one(2007, "Honda", speed);

    while (true){
    displayMenu();

    selection = 'z'; // giving it a value so it enters loop
        while(selection < 'A' || selection > 'C'){
            cin >> selection;
            switch (selection){
                case 'a':
                case 'A': cout << "You're accelerating.\n";
                cout << "Your Current Speed Is: " << one.accelerate() << endl;
                break;

                case 'b':
                case 'B': cout << "You pressed the brake.\n";
                cout << "Your Current Speed Is: " << one.brake() << endl;
                break;

                case 'c':
                case 'C':cout << "You chose to exit.\n";
                cout << "Your Final Speed Was: " << one.carspeed();
                return 1;// return 1 exits the program
                }//case switch bracker
        }//selection loop bracket
    }//infinite loop bracker
}//main bracket
Topic archived. No new replies allowed.