Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

c++ - How can use specific Void functions in an if statement

#include <iostream>
using namespace std;

class Vehicles
{

public:

void wheels () {
cout << "Enter number of wheels: ";
int wh;
cin >> wh;

if (wh == 2) {
    cout << "You chose a Motorcycle!
";
} else if (wh == 3) {
    cout << "You chose a Tricycle!
";
} else if (wh == 4) {
    cout << "You chose a Car!
";
}
}
} ; //number of wheels chosen

class brandMotorcycle : public Vehicles
{
public:

void brandM () {
cout << "Enter brand: 
 1: Yamaha 
 2: Ducati 
 3: Honda 
 4: Kawasaki 
 5: Suzuki 
";
int br;
cin >> br;

if ( br == 1) {
    cout << "You chose Yamaha! 
";
} else if (br == 2) {
    cout << "You chose Ducati! 
";
} else if (br == 3) {
    cout << "You chose Honda! 
";
} else if (br == 4) {
    cout << "You chose Kawasaki! 
";
} else if (br == 5) {
    cout << "You chose Suzuki! 
";
}
}
} ; //brand of motorcycle in case the user will chose 2 wheels

class brandTricycle : public Vehicles
{
public:

void brandT () {
cout << "Enter brand: 
 1: Classic 
 2: CATs 
 3: KYTO 
 4: Prime Green 
 5: Sun Etrike 
";
int br;
cin >> br;

if ( br == 1) {
    cout << "You chose Classic! 
";
} else if (br == 2) {
    cout << "You chose CATs! 
";
} else if (br == 3) {
    cout << "You chose KYTO! 
";
} else if (br == 4) {
    cout << "You chose Prime Green! 
";
} else if (br == 5) {
    cout << "You chose Sun Etrike! 
";
}
}
} ; //brand of tricycle in case the user chose 3 wheels

class brandCar : public Vehicles
{
public:

void brandC () {
cout << "Enter brand: 
 1: Mazda 
 2: Honda 
 3: Toyota 
 4: Kia 
 5: Volkswagen 
";
int br;
cin >> br;

if ( br == 1) {
    cout << "You chose Mazda! 
";
} else if (br == 2) {
    cout << "You chose Honda! 
";
} else if (br == 3) {
    cout << "You chose Toyota! 
";
} else if (br == 4) {
    cout << "You chose Kia! 
";
} else if (br == 5) {
    cout << "You chose Volkswagen! 
";
}
}
} ; //brand of cars in case the user chose 4 wheels

int main () {

Vehicles number;
brandMotorcycle brandM;
brandTricycle brandT;
brandCar brandC;
number.wheels();

    if (wheels == 2) {
brandM.brandM();
} else if (wheels == 3) {
brandT.brandT();
}  else if (wheels == 4) {
brandC.brandC();
} // this part is my problem

return 0;
}

I made an Inheritance where if the user chose 2 wheels he will get a motorcycle and he gets to chose what brand it is. And if he chose 3 wheels he gets tricycle and chose the brand and same with the 4 wheels. But I am currently struggling to input the final if statement because I don't know which code I should use.

question from:https://stackoverflow.com/questions/65838498/how-can-use-specific-void-functions-in-an-if-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are many ways to interact with variables between different classes and different functions. As @TheUndeadFish mentioned, return values are a good place to start. If you have your heart set on using void functions one option would be to use a public variable within your Vehicles class that you set in your wheels function.

So your Vehicles class would become:

class Vehicles
{

public:
    int wh;

    void wheels() {
        cout << "Enter number of wheels: ";
        cin >> wh;

        if (wh == 2) {
            cout << "You chose a Motorcycle!
";
        }
        else if (wh == 3) {
            cout << "You chose a Tricycle!
";
        }
        else if (wh == 4) {
            cout << "You chose a Car!
";
        }
    }
}; //number of wheels chosen

And then your main could be:

int main() {

    Vehicles number;
    brandMotorcycle brandM;
    brandTricycle brandT;
    brandCar brandC;
    number.wheels();

    int wheels = number.wh;
    if (wheels == 2) {
        brandM.brandM();
    }
    else if (wheels == 3) {
        brandT.brandT();
    }
    else if (wheels == 4) {
        brandC.brandC();
    } // this part is my problem

    return 0;
}

EDIT: notice that if you comment out the call to number.wheels() you will get an uninitialized variable error. In practice you'd want it initialized (possibly to -1) in the Vehicles class constructor to avoid the possibility of this error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...