+ 4

If the user inputs an invalid entry, how can I make it so that the program restarts and says something like "invalid entry try again"?

8th Aug 2016, 12:46 PM
jigoku_tenshi
28 Antworten
+ 5
I hope we can finish this 20 comment question once and for all, haha😁 This code works perfectly on both the code playground, and Visual Studio: #include <iostream> #include <cstdlib> #include <ctime> #include <string> using namespace std; string input() { string b; cin >> b; int bLength = b.length(); for (int i = 0; i<bLength; i++) { b.at(i) = tolower(b.at(i)); } if (b != "rock" && b!= "paper" && b!="scissors") { cout << "Invalid entry! Try again:\n"; input(); } return b; } int main() { cout << "Rock, Paper, or Scissors?\n"; string b = input(); srand(time(0)); int a = 1 + (rand() % 3); if (a == 1) cout << "Rock\n"; else if (a == 2) cout << "Paper\n"; else cout << "Scissors\n"; if (b == "rock") { if (a == 1) cout << "It's a tie!\n"; else if (a == 2) cout << "You lose\n"; else cout << "You win\n"; } if (b == "paper") { if (a == 1) cout << "You win\n"; else if (a == 2) cout << "It's a tie\n"; else cout << "You lose\n"; } if (b == "scissors") { if (a == 1) cout << "You lose\n"; else if (a == 2) cout << "You win\n"; else cout << "It's a tie\n"; } return 0; }
8th Aug 2016, 9:33 PM
Cohen Creber
Cohen Creber - avatar
+ 3
Use a recursive function. Example: #include <iostream> int input() { int num; std::cin >> num; if (!(num >= 1 && num <= 10)) { std::cout << "\nInvalid input! Try again: "; input(); } return num; } int main() { std::cout << "Input an integer between 1 and 10: "; int num = input(); std::cout << "\nYou entered " << num << "!"; } Note, that this won't work on the Code Playground, due to the awkward input system. But this would work on a proper IDE.
8th Aug 2016, 3:13 PM
Cohen Creber
Cohen Creber - avatar
+ 3
The code didn't work in codeblocks either
10th Aug 2016, 1:40 PM
jigoku_tenshi
+ 3
I FIXED IT! I have no idea whatever I just did means, but I DID IT!!!
10th Aug 2016, 7:29 PM
jigoku_tenshi
+ 3
static just means it keeps its condition after something like a function has ended. With a non static function, variables will be destroyed when the function ends. With a static variable, that variable will not be destroyed. However at the end of the function, we are returning the string from input() to set it as the string from main(), so I think static is unnecessary. As a beginner to C++ as well, I couldn't tell you what the purpose of the & is. I know it returns the memory address, but I would think that would be fairly unnecessary on a single function. It worked for me without any of these, but if it works for you then congrats, it's finally done😂
10th Aug 2016, 7:47 PM
Cohen Creber
Cohen Creber - avatar
+ 2
I wrote it but the recursive function just continues running even if I type a valid input
8th Aug 2016, 3:49 PM
jigoku_tenshi
+ 2
It shouldn't do. Are you using the code that I wrote, or your own?
8th Aug 2016, 3:52 PM
Cohen Creber
Cohen Creber - avatar
+ 2
My code, but I used yours as reference. Slightly different if statement and my input is a string
8th Aug 2016, 3:57 PM
jigoku_tenshi
+ 2
Post it here if you could😁
8th Aug 2016, 3:58 PM
Cohen Creber
Cohen Creber - avatar
+ 2
It's a rock, paper, scissors game btw string input( ) { string b; cin >> b; if ((b!="Rock"||b!="rock")||(b!="Paper"|| b!="paper) || (b!="Scissors || b!= "scissors")) { cout << "Invalid entry! Try again:\n"; input( ); } return b; } int main ( ) { cout<< "Rock, Paper, or Scissors?\n"; string b = input ( ); // rest of the code } If there's a way I can simplify the if statement, please let me know
8th Aug 2016, 4:19 PM
jigoku_tenshi
+ 2
A couple things: • As you know, strings are case sensitive, so I can see why you have made the if statement like that. You could build a for loop to convert the entire string to lower (or upper) case, to shorten that if statement: int bLength = b.length(); //put this after you have input b, of course for (int i = 0, i < bLength, i++) b.at(i) = tolower(b.at(i)); tolower() and toupper() only works with characters, hence the for loop, and not: tolower(b). • Use &&, not ||. Lets say the user input paper, which is a valid input. Because you used the OR operator, it's checking whether string b is not scissors or rock. And because they are not, the if statement will evaluate to true. Hence why you should use AND operator in this situation. Your final code: string input( ) { string b; cin >> b; int bLength = b.length(); for (int i = 0; i < bLength; b++) b.at(i) = tolower(b.at(i)); if (b != "rock" && b != "scissors" && b != "paper") { cout << "Invalid entry! Try again:\n"; input( ); } return b; } int main ( ) { cout<< "Rock, Paper, or Scissors?\n"; string b = input ( ); // rest of the code }
8th Aug 2016, 4:27 PM
Cohen Creber
Cohen Creber - avatar
+ 2
@Sainath keyword continue is used to skip to the next iteration of a loop. Won't be much help here.
8th Aug 2016, 4:55 PM
Cohen Creber
Cohen Creber - avatar
+ 2
You're awesome! I should refer to you every time I have a doubt. This fixed the continuous function problem, but now it created a problem with the other part of the code I didn't send you. The other part of the code is a few if statements and switch's that tells whether the user won or lost. After the recursive function finishes, it's like the code now skips all those if statements and immediately returns 0 without letting the user know whether he won or lost
8th Aug 2016, 5:04 PM
jigoku_tenshi
+ 2
Feel free to post that here too, can take a look☺
8th Aug 2016, 5:07 PM
Cohen Creber
Cohen Creber - avatar
+ 2
After the recursive function (what you wrote), i have a truly random function that prints rock paper or scissors randomly to compete against the user. That part works fine. Then I have the following: if (b=="Rock" || b== "rock"){ switch (a) { // a=the random function case 1:cout<<"It's a tie\n"; break; case 2:cout<<"You lose\n"; break; case 3:cour<<"You win\n"; break; } } And two more of those with paper and scissors. I believe the problem may be in the if condition
8th Aug 2016, 5:19 PM
jigoku_tenshi
+ 2
For the if statement, you should be able to just do if (b == "rock") Since we converted b to lowercase. Switch statements are awkward things with strings in C++. It would be better to do if else statements in this situation: if (b== "rock") { if (a == "rock") cout << "It's a tie\n"; else if (a == "paper") cout << "You lose\n"; else cout << "You win\n"; }
8th Aug 2016, 5:29 PM
Cohen Creber
Cohen Creber - avatar
+ 2
It still "skips" it...
8th Aug 2016, 5:45 PM
jigoku_tenshi
+ 2
Sorry for the late reply. As long as b is lowercase, like we did with the for loop, then that first if statement should evalutate to true. Now it is comparing string a. As long as a is is also lowercase, and is of course a string, I don't see why else it would be skipping those outputs. Post the whole code here that you have right now.
8th Aug 2016, 7:20 PM
Cohen Creber
Cohen Creber - avatar
+ 2
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; string input(){ string b; cin>>b; int bLength=b.length(); for (int i=0; i<bLength; i++){ b.at(i)=tolower(b.at(i)); } if (b!="rock && b!="paper"&& b!="scissors{ cout<<"Invalid entry! Try again:\n"; input(); } return b; } int main() { cout<<"Rock, Paper, or Scissors?/n"; string b=input(); srand(time(0)); int a = 1 + (rand()%3); if (a==1) cout<<"Rock\n"; else if (a==2) cout<<"Paper\n"; else cout<<"Scissors\n"; if (b=="rock"){ if (a==1) cout<<"It's a tie!"; else if (a==2) cout<<"You lose"; else cout<<"You win"; } if (b=="paper"){ if (a==1) cout<<"You win"; else if (a==2) cout<<"It's a tie"; else cout<<"You lose"; } if (b=="scissors"){ if (a==1) cout<<"You lose"; else if (a==2) cout<<"You win"; else cout<<"It's a tie"; } return 0; } I think it's worth mentioning that it only started skipping after I included the recursive function AND it only skips after typing an invalid entry and the user retypes a valid entry.
8th Aug 2016, 7:57 PM
jigoku_tenshi
+ 2
I run the code that you pasted on both the Code Playground and Visual Studio, and I have not been getting the problem you are getting. All I changed were compiler errors that I got, and some mistypes that you may have made. For example, you have missed some semi-colons and the closing bracket on the input() function. Other than that, I changed not much else. 1) You enter rock, paper or scissors, non-case sensitive. 2) Program returns random: rock, paper or scissors. 3) Decides if you win or not. If you are still getting problems, let me know. But I believe most of your problem is in that if statement.
8th Aug 2016, 8:20 PM
Cohen Creber
Cohen Creber - avatar