My first C++ Prog.

I am new to C++. I really like it, and started a program as a test of what I have been reading in the book I purchased. My first goal is to get used to cout, cin, switch, and if.

It seems as though my IF statements will not work. I am getting the following error. ( I am at line 45 please ignore the rest as I need to change it once I know how):

Error: incompatible types in assignment of 'const char [5]' to 'char [20]. Do I need a string? I have not used one yet haha

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
113
114
115
116
117
118
119
120
121
122
  #include <iostream>
#include <string>

using namespace std;

int main() {

int grade;
char grade2[20];

cout << "My favorite drinks are 1.coke 2.fanta 3.snake blood 4.cake tea 5. limsip. \n";
cout << " You need to select the drinks by entering 1-5. \n";
cin >> grade;


switch(grade) {

case 1:

cout << "You a coke fan eh? \n";

case 2:

cout << "You drink of the fanta, hmm? \n";

case 3:

cout << "WTF? WEirdo! \n";

case 4:

cout << "That exsists O.o? \n";

case 5:

cout << "Erm..fair play \n";

}


cout << "Now enter the name of your favorite drink \n";
cout << "Your options are coke, fanta, snake blood, cake tea, and limsip. \n";
cin >> grade2;

if(grade2 = "coke") {

cout << "You have been sent a bottle of coke \n";
}

else if (grade = fanta) {

cout << "You have been sent a fanta \n";
}

else if (grade = snake blood) {

cout << "You have been sent a bible \n";
}

else if (grade = cake tea) {

cout << " You have been sent some cake and a cup with tea in it..enjoy O.O \n";

}

else if (grade = limpsip) {

cout << "You have been sent a lemon \n";

}

else{

cout << "Read the instructions.pfft \n";

}


cout << " Well done, did that change your life? \n";
cout << " Yes or No? \n";

cin >> grade;

if ( grade = "Yes" || grade = "yes") {

cout << " Well you are very very sad, we have sent you a life \n";

}

else if ( grade = "No" || grade = "no") {

cout << " Well..soooooorry for wasting your time sir \n";

else

cout << "Fail. \n";

}

cout << "For no reason at all! Here is calculator. \n";

int Operator, num1, num2, sum;

sum = num1 + operator + num2;

cout << " Please select the operator. \n";
cout << " ENTER: \n";
cout << " / = divide \n * = multiply \n + = plus \n - = minus \n";
cin >> operator;

cout << "Now enter your first number. \n";
cin >> num1;

cout << " Now enter your second number. \n";
cin >> num2;

cout << "The sum is ";
cout << sum << endl;

system ("pause")
return 0;
}
Now, your if statements (as you surmised) are wrong. You are using the wrong operator: '=' means assignment, set one to the other, while you want '==', or test if equivalent. Anyway, due to various reasons which might become apparent as you learn more about C++, comparing to C-strings like that wont work. As such, I recommend that you use std::string, its not hard (and actually easier).

Also, you have forgotten the 'break' statements after each case in your switch statement, as it is the program will run on to each successive one.

Here is the first bit of your program rewritten with std::string:
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
#include <iostream>
#include <string>
using namespace std;

int main() {
    int grade;
    string grade2;

    // ... 

    switch (grade) {
        case 1:
            cout << "...\n";
        break;

        case 2:
            cout << "...\n";
        break;

        // ...
    }

    // ...
    cin >> grade2;

    if (grade2 == "coke") {
        // ...
    } else if (grade2 == "fanta") {
        // ...
    }

    // you get the idea
}
in assignment

Assignment in conditional? Perhaps your intention is to compare equality rather than assign.

This is FAQ; usually the compiler does not even warn the users about suspicious assignment, so consider yourself lucky.
= is assignment.
== tests equality.

However, pointer is equal to another pointer only when both contain the same address. Therefore, you would not compare two arrays in the way you expect. There is a function in <cstring> for comparing C-strings.

Yes, std::string is much more convenient. It even has operator==.
Thank you very much. Oops can't believe I forgot the breaks!

Well now I have learnt more about strings which is a bonus, however I have been playing around with my code, and the last if always outputs the else statement. I have entered "Yes" and "yes" which means I have done something wrong here.

Can you help? btw you will probably see allot of me cause I really want to learn this :P Thank you for your time.

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>

using namespace std;
std::string test_string1 = "My favourite drinks are 1.coke 2.fanta 3.snake blood 4.cake tea 5. limsip. \n";
std::string test_string2 = " You need to select the drinks by entering 1-5. \n";
std::string test_string3 = test_string1 + test_string2;

int main() {


int grade;
char grade2[20];

cout << test_string3;

cin >> grade;


switch(grade) {

case 1:

cout << "You a coke fan eh? \n";
break;

case 2:

cout << "You drink of the fanta, hmm? \n";
break;

case 3:

cout << "WTF? WEirdo! \n";
break;

case 4:
;
cout << "That's a thing O.o? \n";
break;

case 5:
;
cout << "Erm..fair play \n";
break;

}


cout << "Now enter the name of your favourite drink \n";
cout << "Your options are coke, fanta, snake blood, cake tea, and limsip. \n";
cin >> grade2;

if(grade2 == "coke") {

cout << "You have been sent a bottle of coke \n";
}

else if (grade2 == "fanta") {

cout << "You have been sent a fanta \n";
}

else if (grade2 == "snake blood") {

cout << "You have been sent a bible \n";
}

else if (grade2 == "cake tea") {

cout << " You have been sent some cake and a cup with tea in it..enjoy O.O \n";

}

else if (grade2 == "limpsip") {

cout << "You have been sent a lemon \n";

}

else{

cout << "Read the instructions.pfft \n";

}

char grade3[3];
cout << " Well done, did that change your life? \n";
cout << " Yes or No? \n";

cin >> grade3;

if ( grade3 == "Yes" || grade3 == "yes") {

cout << " Well you are very very sad, we have sent you a life \n";

}

else if ( grade3 == "No" || grade3 == "no") {

cout << " Well..soooooorry for wasting your time sir \n";
}
else{


cout << "Fail. \n";

}

cin.get();
return 0;
}
Last edited on
In fact from the first if to the last it is now saying:

Warning: comparison with string literal results in unspecified behaniour [-waddress]

ill keep looking at it until I get a reply:

Update:
Seems to be a codeblocks error "cannot open output file bin/debug/firstprog.exe permission denied."

Update:

Solved it. Changed build > change target to release, and it works. I still need help with last IF though haha :P
Last edited on
Line 87. Use std::string instead of an array.
Thank you keskiverto. I have just started and can see you have already explained it to me before. I just didn't fully understand what an array was haha but now I do.

First program is now working :P
Topic archived. No new replies allowed.