Hi, I am somewhat a beginner at C++ and i have been making a text adventure game lately. I put in a fun little easter egg when you type in your name as a swear word, but I have been having problems with it. Here's the code:
/* I included these libraies:
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
#include<stdio.h>
#include<conio.h>*/
//I have declared all necessary variables.
cin >> name;
if (name == "fuck" || "ass" || "shit" || "damn"){
do{
cout << "One more time?" << endl;
cin >> name;
}while(name == "fuck" || "ass" || "shit" || "damn");
}
//the rest of the game is here
if you need any other info on the code let me know and ill give it to you!
As a general rule, if you find yourself copy/pasting things, you are probably doing something wrong. Duplicate code is bad.
In this case:
1 2 3 4 5 6
if(condition)
{
do{
...
}while(condition);
}
Could be trimmed to this, which is exactly the same, functionally:
1 2 3 4
while(condition)
{
...
}
Notice how we only have one condition, instead of having to duplicate it.
Furthermore, the || operator doesn't work like you seem to think. It evaluates expressions on each side of it. If either side of the || operator is nonzero, then || gives you nonzero (true).
example:
if(name == "fuck" || "ass")
The expressions here are name == "fuck" and "ass"
Notice that "ass" is nonzero. Therefore this will always result in 'true', and the if block will always execute.
What you probably wanted is something like this:
if(name == "fuck" || name == "ass" || name == "whatever" ...)