I cant figure out why this does not work.

Hi there,

Im new to c++, and have been working on a program that talks to you and asks you questions. Anyway, the section just after you say pie in a fave food choice, it just terminates. No nothing. here is the code ( It is quite long and some of the lines are references to my friend if you don't get them):


#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
cout << "MegaTron program v1.0! Codename: Goldie" << endl;
int first;
cout << "Enter your name!" << endl; // these lines indroduce the user.
string Info_2 = "Null";
cin >> Info_2;
cout << "Hello " << Info_2 << " IS GAY!!!! LOLROFLLMAO " << endl;

cout << "Did you know that goldie wilson is Techkid100's dad?" << endl;

cout << " So, " << Info_2 << ", Where do you live?" << endl;
string Info_3 = "Null";
cin >> Info_3;
cout << Info_3 << ", Ive heard its nice around there. is it?" << endl; //asking about home
cout << " ((Please answer with a simple yes or no answer.)) " << endl;



int second;

char IsItNice[] = "yes";

cin >> IsItNice;

if(!strcmp(IsItNice, "yes"))
{
cout << "Thats nice" << endl;
}
else if(!strcmp(IsItNice, "no"))
{
cout << "Oh. I was told that it was nice." << endl;
}
else if(strcmp(IsItNice, "yes") || strcmp(IsItNice, "no"))
{
cout << "Sorry, I didn't get that." << endl;
}

int third;

cout << " So, " << Info_2 << ", what do you like more, pizza or pie?" << endl;

char IsItNice2[] = "pizza";

cin >> IsItNice2;

if(!strcmp(IsItNice2, "pizza"))
{
cout << "cool me too!" << endl;

int Sub_1;

cout << "what's your fave topping? Cheese or Ham?" << endl;

char IsItNice3[] = "cheese";

cin >> IsItNice3;

if (!strcmp (IsItNice3, "cheese"))
{
cout << "Awesome! You and me are very similiar!" <<endl;
}
else if (!strcmp(IsItNice3, "ham"))
{
cout << "I like ham, but not as much as cheese." << endl;
}
else if (strcmp (IsItNice3, "cheese") || strcmp (IsItNice3, "ham" ))
{
cout << "Sorry didnt get that." << endl;



}
else if(!strcmp(IsItNice2, "pie")) <-----------HERE IS THE PROBLEM
{
cout << "I like pie too, but not as much as pizza." << endl;

int Sub_2;

cout << "what is your fave type? apple or steak?" << endl;

char IsItNice4 [] = "apple";

cin >> IsItNice4;

if (!strcmp (IsItNice4, "apple"))
{
cout << "Cool Thats mine too, but I still like pizza more." <<endl;
}
else if (!strcmp (IsItNice4, "steak"))
{
cout << "God, you have bad taste. Pie? Steak? Idiot!" << endl;
}
else if (strcmp (IsItNice4, "apple") || strcmp (IsItNice4, "steak"))
{
cout << "Sorry I didnt get that." << endl;
}


}
else if(strcmp(IsItNice2, "pizza") || strcmp(IsItNice2, "pie"))
{
cout << "Sorry, I didn't get that." << endl;
}










system("pause");
return 0;
}


}


If some one can help, I would be very grateful!
Last edited on
Please repost your code using the code tags (under Format). This will preserve some formatting and also it adds line numbers.

You really can't do what you're doing with your character arrays. When you initialize a character array with a string, the size of the array is fixed at the length of the string plus one byte for the terminating null. Rewrite this using the string class (http://www.cplusplus.com/reference/string/string/).

If you formatted your code properly, you'd be able to see where the closing brace for this is:
1
2
3
if(!strcmp(IsItNice2, "pizza"))
{
cout << "cool me too!" << endl;

and you'd be able to tell why the program did what it does.
stacking else if seems to look messy. what if there was a way to write this in a better more readable format?
1
2
3
4
5
6
7
8
9
10
     switch (<#expression#>) {
            case <#constant#>:
                <#statements#>
                break;
                
            default:
                break;
        }
        
        
ui uiho wrote:
stacking else if seems to look messy. what if there was a way to write this in a better more readable format?

Unfortunately there isn't so we have to live with it.
The two comments previous made me laugh... out loud. Smile, its good for you. I guess you could do it with a char array but it would have to be multidimensional. I.E. char isItNice[][256] but use a string like they said cause strings are AWESOME (comparatively).
Last edited on
switch( std::distance( std::find(choices.begin(), choices.end(), item), choices.begin() ) ){
Last edited on
Thanks for all of you guys helping! I havent had a chance to try any of this out but i will later!

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
#include <iostream>
 #include <stdio.h>
 #include <string.h>
 
using namespace std;
 
int main() 
{
 cout << "MegaTron program v1.0! Codename: Goldie" << endl;
 int first;
 cout << "Enter your name!" << endl; // these lines indroduce the user.
 string Info_2 = "Null";
 cin >> Info_2;
 cout << "Hello " << Info_2 << " IS GAY!!!! LOLROFLLMAO " << endl;

 cout << "Did you know that goldie wilson is Techkid100's dad?" << endl;

 cout << " So, " << Info_2 << ", Where do you live?" << endl;
 string Info_3 = "Null";
 cin >> Info_3;
 cout << Info_3 << ", Ive heard its nice around there. is it?" << endl; //asking about home
 cout << " ((Please answer with a simple yes or no answer.)) " << endl;



 int second;

 char IsItNice[] = "yes";

 cin >> IsItNice;

 if(!strcmp(IsItNice, "yes"))
 {
 cout << "Thats nice" << endl;
 }
 else if(!strcmp(IsItNice, "no"))
 {
 cout << "Oh. I was told that it was nice." << endl;
 }
 else if(strcmp(IsItNice, "yes") || strcmp(IsItNice, "no"))
 {
 cout << "Sorry, I didn't get that." << endl;
 }

 int third;

 cout << " So, " << Info_2 << ", what do you like more, pizza or pie?" << endl;

 char IsItNice2[] = "pizza";

 cin >> IsItNice2;

 if(!strcmp(IsItNice2, "pizza"))
 {
 cout << "cool me too!" << endl;

 int Sub_1;

 cout << "what's your fave topping? Cheese or Ham?" << endl;

 char IsItNice3[] = "cheese";

 cin >> IsItNice3;

 if (!strcmp (IsItNice3, "cheese"))
 {
 cout << "Awesome! You and me are very similiar!" <<endl; 
}
 else if (!strcmp(IsItNice3, "ham"))
 {
 cout << "I like ham, but not as much as cheese." << endl;
 } 
else if (strcmp (IsItNice3, "cheese") || strcmp (IsItNice3, "ham" ))
 {
 cout << "Sorry didnt get that." << endl;



 }
 else if(!strcmp(IsItNice2, "pie")) 
 {
 cout << "I like pie too, but not as much as pizza." << endl;

 int Sub_2;

 cout << "what is your fave type? apple or steak?" << endl;

 char IsItNice4 [] = "apple";

 cin >> IsItNice4;

 if (!strcmp (IsItNice4, "apple"))
 { 
cout << "Cool Thats mine too, but I still like pizza more." <<endl;
 } 
else if (!strcmp (IsItNice4, "steak"))
 {
 cout << "God, you have bad taste. Pie? Steak? Idiot!" << endl;
 } 
else if (strcmp (IsItNice4, "apple") || strcmp (IsItNice4, "steak"))
 {
 cout << "Sorry I didnt get that." << endl;
 } 


}
 else if(strcmp(IsItNice2, "pizza") || strcmp(IsItNice2, "pie"))
 {
 cout << "Sorry, I didn't get that." << endl;
 }





 




system("pause");
 return 0; 
}
 

}
 
Topic archived. No new replies allowed.