char your_name, your_lastname, your_best_friend, your_gb_friend, your_car, a;
You've declared all these to be "char", that is a single character, like 'D' or 'x'. So all you can ever get into or out of them is a single character. That's why your program works for single characters.
Normally, I would suggest you use a string, so you don't have to worry about the length of the name, but your code is so old-timey (iostream.h) I'm not sure how strings work in your implementation.
Maybe you should follow what Raffay said above, and use c-style (yuk):
1 2 3 4
|
#define MAX_LEN (256)
#define ANS_LEN (4)
char your_name[MAX_LEN], your_lastname[MAX_LEN], your_best_friend[MAX_LEN], your_gb_friend[MAX_LEN], your_car[MAX_LEN];
char yes[ANS_LEN], Yes[ANS_LEN], yep[ANS_LEN], Yep[ANS_LEN], a[ANS_LEN];
|
You would also have to initialise yes, Yes, yep, Yep, probably with strcpy,
or at declaration time, like char *yes = "yes", etc.
or just not bother with those variables and compare the stirngs directly: if (strcmp("yes", a))
And yeah, instead of a==yes, use strcmp().
All this would be much easier if you just use std::string.
This is getting way too messy, here's a less old-time way - not very good mind you, but still better than going back to C
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
|
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
static inline char lower_case(char x) // let us re-invent the wheel, just to make a point
{
if (x >= 'A' && x <= 'Z')
{
x += ('a' - 'A');
}
return x;
}
int main()
{
string your_name, your_lastname, your_best_friend, your_gb_friend, your_car, a;
string yes("yes"), yep("yep");
cout << "What's your name? ";
cin >> your_name;
cout << "What's your lastname? ";
cin >> your_lastname;
cout << "What's your best pal's name? ";
cin >> your_best_friend;
cout << "And your dream car? ";
cin >> your_car;
cout<<"Do you have a girl friend or boy friend? Say yes or no ";
cin >> a;
transform(a.begin(), a.end(), a.begin(), lower_case); // change a to all lower case
if (a == yes || a == yep)
{
cout << "what's her or his name? ";
cin >> your_gb_friend;
cout << "You can't believe! Guess what, I saw "
<< your_best_friend
<< " giving flowers to "
<< your_gb_friend
<< " in your "
<< your_car
<< " car! What you wanna do "
<< your_name
<< " "
<< your_lastname
<< "?"
<< endl;
}
else
{
cout <<"I saw your "
<< your_car
<< " in a garage, I thought it was you inside of it. I said \"Hey! how are you! "
<< your_name
<< "\", but I figured out he's not you!, the driver was "
<< your_best_friend
<< "! and what would you do if your friend had the car? would you ask your friend to drive it "
<< your_lastname
<<"?"
<< endl;
}
cin.get();
return 0;
}
|