Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I am trying to make a Rock-Paper-Scissor game in C++ in Visual Studio.
I am trying to make the computer generated response, but when I make the if statements, it thinks it is an error on line 9:
#include <iostream>
using namespace std;
string rockPaperScissor;
int computerChoiceInteger = 1 + (rand() % 3);
if (computerChoiceInteger == 1) {
cout << "1";
}
if (computerChoiceInteger == 2) {
cout << "2";
}
if (computerChoiceInteger == 3) {
cout << "3";
}
int main() {
cout << "Rock, paper or scissor?\n";
cout << "R=Rock P=Paper S=Scissor\n";
cin >> rockPaperScissor;
if (rockPaperScissor == "r" || "R") {
cout << "correct";
}
if (rockPaperScissor == "p" || "P") {
cout << "correct";
}
if (rockPaperScissor == "s" || "S") {
cout << "correct";
}
}
I have tried to comment out the first if statement, but then it says there is an error on line 13.
For your main error, you cannot have logical statements (if, while, etc.) outside of a function body. You will need to move all of your if statements into your main function body.
C++ uses the main function as the "entry point" for your program, which means once all global variables, class definitions, and more, are settled, the main function will be what your program begins to execute. All the logic for your program should always be inside of a function.
Also, as others in the comments have pointed out, you cannot choose between two values in an or statement like you are currently doing. You need to explicitly make two comparisons. I've moved your conditional logic as well as changed your or statements here, as well as added some more newlines:
#include <iostream>
using namespace std;
int computerChoiceInteger = 1 + (rand() % 3);
int main() {
if (computerChoiceInteger == 1) {
cout << "1\n";
}
if (computerChoiceInteger == 2) {
cout << "2\n";
}
if (computerChoiceInteger == 3) {
cout << "3\n";
}
cout << "Rock, paper or scissor?\n";
cout << "R=Rock P=Paper S=Scissor\n";
string rockPaperScissor;
cin >> rockPaperScissor;
if (rockPaperScissor == "r" || rockPaperScissor == "R") {
cout << "correct\n";
}
if (rockPaperScissor == "p" || rockPaperScissor == "P") {
cout << "correct\n";
}
if (rockPaperScissor == "s" || rockPaperScissor == "S") {
cout << "correct\n";
}
return 0;
}
As further reading, you may be interested in Generate random number between 1 and 3 in C++ to generate a more truly random choice by the computer.
Your program is structured all wrong.
you are missing #include <stdlib.h> or #include <cstdlib> to declare rand(). It is possible that <iostream> is already including one of them internally for you, but you should not rely on that behavior. Be explicit in the headers you want to use.
global scope can only have declarations, not statements. Statements can only appear in function scope.
your use of the || operator is incorrect. You can't compare a variable to multiple values the way you are trying to. You need a separate comparison for each value.
Try something more like this instead:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int computerChoiceInteger = 1 + (rand() % 3);
cout << computerChoiceInteger << endl;
cout << "Rock, paper or scissor?\n";
cout << "R=Rock P=Paper S=Scissor\n";
string rockPaperScissor;
cin >> rockPaperScissor;
if (rockPaperScissor == "r" || rockPaperScissor == "R") {
cout << "correct";
}
else if (rockPaperScissor == "p" || rockPaperScissor == "P") {
cout << "correct";
}
else if (rockPaperScissor == "s" || rockPaperScissor == "S") {
cout << "correct";
}
return 0;
}
Now, the code should at least compile, so you can carry on with finishing your game logic (since what you have shown is not a complete Rock-Paper-Scissor game yet).
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
Recently I have been making a text-based game to imitate a game from the 90's for my friend, and it went all well, but the Yes/No didn't work because I had an if statement then another if statement following first, so then I change it to an if statement with an else statement following the if statement, but get error's like "else without previous if" and "label referenced but not defined" which is really weird because a few lines below the reference in the defined label... I really need help with this, because I tried googling it but comes up with useless Microsoft threads that don't relate whatsoever.
My Code:
#include <iostream>
int main() //main function
{
std::cout << "NOTE: when you see [Y/N], it is a choice option, please capitalize letter of choice!!\n";
lab1:
char name = NULL;
int age = NULL;
char ans1 = NULL;
char strong = NULL;
char ans2 = NULL;
char ans3 = NULL;
char smart = NULL;
char ans4 = NULL;
char ans5 = NULL;
std::cout << "Well hello! What is your name?\n";
std::cin >> name;
std::cout << "And your age is?\n";
std::cin >> age;
std::cout << "Ok so your name is " << name << " and you are " << age << "?\n";
std::cout << "Is that correct? [Y/N]\n";
std::cin >> ans1;
if (ans1 == 'Y');
{
std::cout << "Well then, lets get started with your main ability!\n";
goto lab2;
}
else
{
std::cout << "Oh sorry, my bad lets enter it again shall we?\n";
goto lab1;
}
lab2:
lab4:
lab6:
std::cout << "Strong? [Y/N]\n";
std::cin >> strong;
if (strong == 'Y');
{
std::cout << "Are you sure? Only ONE main ability can be chosen! [Y/N]\n";
std::cin >> ans2;
if (ans2 == 'Y')
{
goto lab3;
}
else
{
goto lab4;
}
}
else
{
std::cout << "Are you sure? Move to next ability? [Y/N]\n";
std::cin >> ans3;
if (ans3 == 'Y');
{
goto lab5;
}
else
{
goto lab6;
}
}
lab5:
lab8:
lab10:
std::cout << "Smart? [Y/N]\n";
std::cin >> smart;
if (smart == 'Y')
{
std::cout << "Are you sure? Only ONE main ability can be chosen! [Y/N]\n";
std::cin >> ans4;
if (ans4 == 'Y')
{
goto lab7;
}
else
{
goto lab8;
}
}
else
{
std::cout << "Are you sure? Move to next ability? [Y/N]\n";
std::cin >> ans5;
if (ans5 == 'Y')
{
goto lab9;
}
else
{
goto lab10;
}
}
lab9:
lab3:
lab7:
std::cout << "Done!";
}
I know this doesnt answer your question but I am going to give advice.
Code like this with that many labels is very fragile (meaning its hard to change as your project grows). I have worked on large c++ projects (100,000 lines +) and every goto and label had to be individually justified. Many probably had none. You should develop a dislike of typing the characters 'g' 'o' 't' 'o'.
So how do you do what you code is trying to do. The common idiom is
std::string ans;
while(true)
{
std::cout >> "Are you sure";
std::cin >> ans;
if(ans == "Y" || ans == "N")
break;
}
if (ans == "Y")
{
//do the yes thing
}
else
{
// do the no thing
}
This will keep looping , asking the user a Yes No question until they answer Y or N
Taking your example of Smart
bool isSmart = false;
while(true)
{
std::cout << "Smart? [Y/N]\n";
std::cin >> smart;
if (smart == 'Y')
{
std::cout << "Are you sure? Only ONE main ability can be chosen! ";
std::cin >> ans4;
if (ans4 == 'Y')
{
isSmart = true;
break;
}
}
}
EDIT, answer the original question
You have
if (ans3 == 'Y');
{
goto lab5;
}
else
{
goto lab6;
}
this is invalid syntax
you need
if (ans3 == 'Y') // <=== no ;
{
goto lab5;
}
else
{
goto lab6;
}
You have this same error in multiple places
So, I'm triying to learn c++ (coming from python), and I wanted to make a program just to see if i could do it with what i've learned, here's the code
#include <iostream>
using namespace std;
int response(string i) {
if (i == "yes" or i == "Yes") {
cout << "\nHello, sad, I'm dad\n";
return(0);
}
else if (i == "no" or i == "No") {
cout << "Good for you pal\n";
return(0);
}
else {
cout << "Answer properly you overgrown flatworm\n";
response(i);
};
};
int main() {
string i;
cout << "Are you sad?";
cin >> i;
response(i);
};
Pretty simple huh? No. For some reason, yes and no answers work fine, but when I try something different I get insulted infinitely and the program crashes from exceeding it's memory limit. How do I solve this?
(English is not my native language, so feel free to correct any ortography mistakes)
At no point do you request further input. For bad input 'i', the response routine prints out an insult, and then calls itself with exactly the same string.
The response routine prints out an insult, and then calls itself with exactly the same string.
The response routine prints out an insult, and then calls itself with exactly the same string.
…
You need to allow the user to enter a new string, and then (if you want to use recursion) make the recursive call to validate the new input.
But as mentioned in the comment, this is not really a problem that needs a recursive solution.
This can be solved by eliminating recursion ad it involves moving the input routine inside of a function that's more self-contained:
int getResponse(string i) {
for(;;) {
string i;
cout << "Are you sad?";
cin >> i;
if (i == "yes" or i == "Yes") {
cout << "\nHello, sad, I'm dad\n";
return(0);
}
else if (i == "no" or i == "No") {
cout << "Good for you pal\n";
return(0);
}
else {
cout << "Answer properly you overgrown flatworm\n";
}
}
}
You have 2 issues:
In the else case, you are not asking for new user input.
You need to return the result of calling response(i), otherwise the code invokes undefined behavior.
else {
cout << "Answer properly you overgrown flatworm\n";
cin >> i;
return response(i);
};
Alternatively, since you never use the return value from response, you can just remove all the return statements, and make it a void function.
If you insist on using recursion then move the input and the check in the same function response() - that function doesn't need to return int at all. In main you can just call response().
void response()
{
string i;
cout << "Are you sad?";
cin >> i;
if (i == "yes" or i == "Yes")
{
cout << "\nHello, sad, I'm dad\n";
}
else if (i == "no" or i == "No")
{
cout << "Good for you pal\n";
return;
}
else
{
cout << "Answer properly you overgrown flatworm\n";
response();
}
}
int main()
{
response();
}
This question already has answers here:
If statement runs through whether conditions are met or not
(4 answers)
Closed 2 years ago.
Hello I'm having a problem where when I call my function after the user enters "Y" to start the game, the cout gets read but it becomes an infinite loop. It works just fine when you enter "N" or something thats not supposed to be entered. I am using a header file called functions to well, put all the functions if that has anything to do with it. I am still in the very early learning stages of programming, and run into so many speed bumps and just not quite sure where to turn. Any help is appreciated. (P.S. I have not yet started on the gameStart() function just because of this problem. That's not whats it's going to be in the end.)
#ifndef FUNCTIONS_H;
#define FUNCTIONS_H
#include <iostream>
using namespace std;
void startScreen()
{
void gameStart();
char answer;
cout << "Welcome to __________\n\n";
cout << "This is my fisrt ever actual program I made out of my own free will lol.\n";
cout << "It is a Text-Based Adventure game. In this game you will make a character,\n";
cout << "and explore the land of Spelet, battling enemies, leveling up, getting loot,\n";
cout << "and learning skills! You do not need to capitalize anything but your character\n";
cout << "name. If a question has (something like this), those are the choices for that \n";
cout << "interaction! Thank you for trying out my terrible little game! :)\n";
cout << "I really hope y'all enjoy it!\n\n";
cout << "Would you like to play?\n";
cin >> answer;
do
{
if (answer == 'Y' || answer == 'y')
{
gameStart();
}
else if (answer == 'N' || answer == 'n')
{
cout << "Program will now close...\n";
system("pause");
exit(0);
}
else
{
cout << "Enter a Y for yes or an N for no.\n";
cout << "Would you like to play?\n";
cin >> answer;
}
}
while (answer != 'N', 'n' || 'Y', 'y');
}
void gameStart()
{
cout << "\n\"BOOM-BOOM-BOOM...\"\n\n" << endl;
}
#endif
maybe you need:
while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y')
The comma operator doesn't do what you think it does. It "discards the result," as my link says.
You want the && operator (AND operator) instead:
while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y');
This question already has answers here:
Can you use 2 or more OR conditions in an if statement? [duplicate]
(9 answers)
Closed 4 years ago.
Ok, so I was writing a simple interface for a programming I'm creating and i come across this issue, where it gives me the same response regardless.
#include <iostream>
using namespace std;
int main()
{
char v;
cout << "Binary or ASCII? "<<endl;
cin >> v;
if (v == 'B' || 'b')
{
cout << "Binary " << endl;
}
else if (v == 'A' || 'a')
{
cout << "ASCII " << endl;
}
else
{
cout << "ERROR: Invalid Option" << endl;
}
return 0;
}
The interface is supposed to output
Binary
if I type B or b
ASCII
if i type A or a
and
ERROR: Invalid Option
for everything else
Instead, I get
Binary
regardless of what I type
Where is my mistake? what am I doing wrong?
Let's take a look at what happens in your if:
if (v == 'B' || 'b')
First it checks if v == 'B'. Let's assume it doesn't for the sake of this walkthrough. Then it'll check (false || 'b'). Since 'b' always evaluates to true, this will be true!
You probably wanted:
if (v == 'B' || v == 'b')
This question already has answers here:
if statement not working right?
(5 answers)
Closed 7 years ago.
after a good amount of time trying to get my else if statement to work, it just doesn't. This program keeps returning the first one, no matter what I input. Please help.
#include <iostream>
#include <string>
using namespace std;
string arehap;
int main()
{
cout << "Are you happy?" << endl;
cin >> arehap;
if (arehap == "Yes" || "Y")
{
cout << "Good." << endl;
}
else if (arehap == "No" || "N")
{
cout << "Bad." << endl;
}
return 0;
}
You should use this:
if (arehap == "Yes" || arehap == "Y")
{
cout << "Good." << endl;
}
else if (arehap == "No" || arehap == "N")
{
cout << "Bad." << endl;
}
When you're using the || operator, you have to compare two boolean values. If arehap is equal to "Y", the following statement will be True: arehap == "Y". In that case your computer will "understand" this as if (True || False) { /* do smth */} and this will evaluate to True and the code you want to execute will be run.
Your problem lies in this line:
if (arehap == "Yes" || "Y")
C++ understands this as
if ((arehap == "Yes") || ("Y"))
and while the first check (arehap == "Yes") might be false, the second check -- which is just "Yes" is always true.
This happens, because the "Yes" gets understood as a char const* -- and this pointer must obviously not be NULL, but point to the character 'Y'!