How do I do this?

char answer[70];
while(answer == 'Yes'){
if (a== 136)
{
wichita.pedal();
}
else if (a == 236)
{
std::cout << "hello";
wichita.fly();
}std::cout<< "Would you like to go on? Yes or no"
std::cin>> answer;

}

return 0;
}

and then I get ISO C++ forbids comparison between pointer and integer.
What i'm trying to do is if they reply with a "Yes" then we will start this procedure over. I used a character array "answer[80]" for the cin. How do I fix this?
Last edited on
You should use the strcmp or strncmp functions from the <cstring> header for comparing char arrays or C-style strings.

It thinks you're trying to compare a pointer with an integer because you put single quotes around the 'Yes', making it a char.
thansk shack. can you give me an example how i could do this? just for future references? like, say, if i were to have options of "Yes" and "No" and to have them write in Yes or No and have the effects of the Yes and No.

Like, how do that with examples? My book doesnt talk about strcmp or strncmp at all.
if( strcmp(answer, "Yes") == 0 )

The above case would be true if the first three characters in "answer" are "Yes". And similarly for "No":

if( strcmp(answer, "No") == 0 )

Of course, using strings is easier because you can use the == operator between a string and a char* or a char[], plus there's the benefit of it doing its own dynamic memory for you.
Last edited on
Thanks Shack. But Shack I have a new problem. It doesn't let me type in for "answer". How could this be?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 char answer[70];
    do{
        std::cin>> a;
        if (a == 136)
        {
             wichita.pedal();


        }
            else if (a == 236)
            {
                std::cout << "hello";
                wichita.fly();
            }
            std::cout<< "Would you like to go on? Yes or no";
            std::cin >> answer;

            }while(strcmp (answer, "Yes") == 0);


    return 0;
}
Shack, when I do this this function, it asks me to repeat twice, and then when I type Yes for both, it just repeats nonstop. Here's my code if you want to see what I'm talking about.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string.h>

class Tricycle
{
public:
    Tricycle(int initialAge, int abc);
    ~Tricycle();
    int getSpeed();
    void setSpeed(int speed);
    void pedal();
    void brake();
    void fly();
    void land();
    int getHeight();
    void setHeight(int height);
private:
    int speed;
    int height;
};

// constructor for the object
Tricycle::Tricycle(int initialSpeed, int initialHeight)
{
    setSpeed(initialSpeed);
    setHeight(initialHeight);
}

// destructor for the object
Tricycle::~Tricycle()
{
    // do nothing
}

// get the trike's speed
int Tricycle::getHeight()
{
    return height;
}
int Tricycle::getSpeed()
{
    return speed;
}

// set the trike's speed
void Tricycle::setSpeed(int newSpeed)
{
    if (newSpeed >= 0)
    {
        speed = newSpeed;
    }
}
void Tricycle::setHeight(int newHeight)
{
    if (newHeight >= 0)
    {
        height = newHeight;
    }
}

// pedal the trike
void Tricycle::pedal()
{
    setSpeed(speed + 1);
    std::cout << "\nPedaling; tricycle speed " << getSpeed() << " mph\n";
}
void Tricycle::land()
{
    setHeight(height - 1);
    std::cout<< "\nLanding tricycle height " << getHeight() << " mph\n";
    }
void Tricycle::fly()
{
    setHeight(height + 1);
    std::cout << "\nFlying; tricycle height " << getHeight() << " mph\n";}
// apply the brake on the trike
void Tricycle::brake()
{
    setSpeed(speed - 1);
    std::cout << "\nBraking; tricycle speed " << getSpeed() << " mph\n";
}

// create a trike and ride it
int main()
{
    int a;
    Tricycle wichita(5,3);
    std::cout << "\nNoesw you have the tricycle wichita. Options type in: Fly, Land, Pedal, or Brake.\n";

    char answer[70];
    do
    {
        std::cin >> a;
        if (a == 136)
        {
             wichita.pedal();


        }
            else if (a == 236)
            {
                std::cout << "hello";
                wichita.fly();
            }
            std::cout<< "Would you like to go on? Yes or no";
            std::cin >> answer;

            }while(strcmp (answer, "Yes") == 0);


    return 0;
}
Actually, I figured the last problem out. Thanks Shack you were a big help
Topic archived. No new replies allowed.