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
#include <iostream>
#include <string>
usingnamespace 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";
}
elseif (grade = fanta) {
cout << "You have been sent a fanta \n";
}
elseif (grade = snake blood) {
cout << "You have been sent a bible \n";
}
elseif (grade = cake tea) {
cout << " You have been sent some cake and a cup with tea in it..enjoy O.O \n";
}
elseif (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";
}
elseif ( 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:
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.
#include <iostream>
#include <string>
usingnamespace 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";
}
elseif (grade2 == "fanta") {
cout << "You have been sent a fanta \n";
}
elseif (grade2 == "snake blood") {
cout << "You have been sent a bible \n";
}
elseif (grade2 == "cake tea") {
cout << " You have been sent some cake and a cup with tea in it..enjoy O.O \n";
}
elseif (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";
}
elseif ( grade3 == "No" || grade3 == "no") {
cout << " Well..soooooorry for wasting your time sir \n";
}
else{
cout << "Fail. \n";
}
cin.get();
return 0;
}
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.