a "Char" question.

Hello.
I just wrote a program that gets your name,last name..... and then tells you a story. I have no idea why when I write a name with more than one character (for example "Dan") it won't work properly but when I write a character (fop example "D") it works better. Please help what should I do with this C++ script?

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
//in the name of GOD
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char your_name, your_lastname, your_best_friend, your_gb_friend, your_car, a;
char yes, Yes, yep, Yep;
cout<<"What's your name? ";
cin>>your_name;
cout<<"\n what's your lastname? ";
cin>>your_lastname;
cout<<"\n What's your best pal ?";
cin>>your_best_friend;
cout<<"\n Do you have a girl friend or boy friend? Say yes or no ";
cin>>a;
if ( (a==yes) || (a==Yes) || (a==yep) || (a==Yep) )
  {
  cout<<"what's her or his name?";
  cin>>your_gb_friend;
  cout<<"And your dream car?";
  cin>>your_car;
  cout<<"You can't believe! Guess what, I saw your "<<your_best_friend<<" 
giving followers to your "<< your_gb_friend<<" in your "<<your_car<<" car!, 
what you wanna do "<<your_name<<" "<<your_lastname<<"?";
  } else {
cout<<"And your dream car?";
cin>>your_car;
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 his 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<<"?";
}
getch();
}
Last edited on
you should use char your_name[15] or write what ever you think should be the right length for a name
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
char your_name[15], your_lastname[15], your_best_friend[15], your_gb_friend[15], your_car[15],a[15];
char yes, Yes, yep, Yep;
cout<<"What's your name? ";
cin>>your_name;
cout<<"\n what's your lastname? ";
cin>>your_lastname;
cout<<"\n What's your best pal ?";
cin>>your_best_friend;
cout<<"\n Do you have a girl friend or boy friend? Say yes or no ";
cin>>your_gb_friend;
if ( (a=="yes") || (a=="Yes") || (a=="yep") || (a=="Yep") )
{
cout<<"what's her or his name?";
cin>>your_gb_friend;
cout<<"And your dream car?";
cin>>your_car;
cout<<"You can't believe! Guess what, I saw your "<<your_best_friend<<
"\tgiving followers to your "<< your_gb_friend<<" in your "<<your_car<<
" \tcar!, what you wanna do "<<your_name<<" "<<your_lastname<<"?";
} else
{
cout<<"\tAnd your dream car?";
cin>>your_car;
cout<<"I saw your "<< your_car<< "in a garage, I thought it was you inside of it. I said"
<<"\tHey! how are you! "<<your_name<<"\tbut I figured out its not you!, the driver was "
<<your_best_friend<<!" \tand what would you do if your friend had the car? would you "
<<"\task your friend to drive it "<<your_lastname<<endl;
}
getch();
}
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;
}
Thank you guys for answering me in detail. Your helps were really good.
Last edited on
Topic archived. No new replies allowed.