First Program, Review Please 20~ lines.

First,
Hello everyone!! After 1 day reading thru ~2 chapters of Stroustrups book I have written the following mini program. My question is with the answering of questions in the app. If i enter anything other than 'y' or 'n' it crashes. How can i encompass someone answering yes/no questions with the various forms of yes/no(ie. Yes,yes,Y,y,ya,etc) while still using the same (char Yes). Do I have to define each possible answer and give each its own name? Any help is appreciated as well as any glaring mistakes in the below..thanks!

-Justin


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
  #include "..\..\std_lib_facilities.h"

int main()
{
string firstName;
cout << "What is your name child?\n";
cin >> firstName;
cout << "Dear " << firstName << ",\n"
<< "     How have you been lately? I worry that your workouts are too strenous.\n Are they?";
string workOutFeeling;
cin >> workOutFeeling;
cout << workOutFeeling << ", that is interesting. I do not understand feeling.\n Can I ask you a Question?\n";
char Yes = 'y';
char No = 'n';
char ansQues;
cin >> ansQues;
if (ansQues=='y')
{
	string friendName;
	cout << "What is your friends name?\n";
	cin >> friendName;
	char friendSex;
	char Male = 'm';
	char Female = 'f';

	cout << "Male or Female?, please enter: M or F\n";
	cin >> friendSex;
	if (friendSex == 'm')
		cout << "Tell him to call me please.\n";

	if (friendSex == 'f')
		cout << "Tell her DONOT call me please lol.\n";
	cout << "How old are you?\n";
	int age;
	cin >> age;
	cout << "No way, you are " << age << " years old?? That's like " << age * 12 << "Months\n old or " << age * 12 * 30 << " days old..woow. Im 2hours old.\n "
		"So next year you will be " << age + 1 << " years old ya?...Cool Cool.\n Hopefully I'm still around hehe.\n";
}

if (ansQues == 'n')
{
	cout << "Sorry you don't feeling like talking to a lonely computer....hope\n u catch a virus with no antivirus protection...\n";
}



	keep_window_open();
	return 0;
}
if i take out line 1 and put in the relevant includes, and remove line 47 it does not crash for me.

while still using the same (char Yes)


i wouldn't not keep it as a char. use a std::string, even if the user types a single character. then you can test for all expected inputs (Yes,yes,Y,y,ya).

why define
char Yes = 'y';
on line 13, then you dont even bother using this variable on libe 17? In fact none of your if statement should contain hard-coded variables (like 'm', 'f', 'y' and 'n').
Last edited on

why define
char Yes = 'y';

because I need to use it in:
if (ansQues=='y')
line 17
If this is incorrect, how could i of better used this?

-Justin
Last edited on
It doesn't actually crash when the user enters something other than y or n, it simply terminates normally.

One way to deal with that is to use a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    char Yes = 'y';
    char No = 'n';
    char ansQues;
    do 
    {
        cin >> ansQues;
        if (ansQues == Yes)
        {
            // etc.
        } 
        else if (ansQues == No)
        {
            // etc.
        }
        else
        {
            cout << "Sorry, I don't understand, could you just put 'y' or 'n' please " << endl;
        }
    } while (ansQues != Yes && ansQues != No);
Note, in your code, the variables Yes and No are defined but your code makes no use of them. (I did use them in the example above).

If you want to allow for both upper and lower case responses, one way is to test for either possibility:
 
    if (ansQues == 'y' || ansQues == 'Y')

another way is to convert the input to either upper or lower case before testing its value:
1
2
3
    cin >> ansQues;
    ansQues = tolower(ansQues);
    if (ansQues=='y')

In order to allow for all possible variations, such as 'y', 'Y', "YES", "yes", "Yes", "yeah" .. and so on, change ansQues to be of type string instead of char and test for each possibility, separated by the logical or operator || as i showed in the 'y' 'Y' example.
You missed mutexe's point.

You don't use Yes on line 17, so why bother with the definition at line 13?

Thanks Chervil. You understand my point to a 'T'.

AbstractionAnon: I did seem to miss his point, but I understand now that I don't use the 'y', I use the "Yes" as Chervil pointed out. But thank you though.
Topic archived. No new replies allowed.