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 11 months ago.
Improve this question
Hi I'm trying to use a cin input but on my second while loop, the inputs are not running though correctly. How would I go about this?
Here's what I have:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using std::endl;
using std::cout;
using std::cin;
using namespace std;
int a, A;
int main() {
int choice, num;
cout << "State Search" << endl;
cout << "" << endl;
cout << "1. Enter the first letter of your desired state" << endl;
cout << "2. Press 2 to Quit" << endl;
cout << "" << endl;
bool done = false;
while(!done) {
//user inputs desired choice
cin >> choice;
if(choice == a || choice == A)
{
cout << "Which State? (enter a number)" << endl;
cout << "1. Alabama" << endl;
cout << "2. Alaska" << endl;
cout << "3. Arizona" << endl;
cout << "" << endl;
while (!(cin >> num))
{
if(num == 1)
{
cout << "test output 1" << endl;
}
else if (num == 2)
{
cout << "test output 2" << endl;
}
else if (num == 3)
{
cout << "test output 3" << endl;
}
} break;
} break;
}
return 0;
}
Once the user chooses one of the above states using a number I want to display the chosen output.
Here is a cleaned up version of what you are trying to do with some inline comment where you were going wrong.
// You only need to include iostream in your example program
// Only include what you need
#include <iostream>
#include <limits>
// You do not need to include all of the std namespace in the local scope
// if you are using the things you want.
using std::endl;
using std::cout;
using std::cin;
using std::streamsize;
int main()
{
cout << "State Search\n\n";
cout << "1. Enter the first letter of your desired state\n";
cout << "2. Press 2 to Quit\n" << endl;
// Removed all the while(!done) / break; as it is just noise in the current iteration of your program.
// if you use an int for the choice variable, you will actually try to parse the input
// as an integer and return 0 if the user inputs 'a'
// you need to use a char for what you want to do
char choice;
cin >> choice;
// Also your a and A int value do not make sense as they are init to 0
// (global initialization), I guess what you want to do is compare to the char 'a' or 'A'. You could also transform the input to lowercase and compare only to 'a'.
if (choice == 'a' || choice == 'A')
{
// Use \n not std::endl as it will flush the output buffer
// all the time. std::endl is a false friend, use it only
// when you want to present your text to the user.
cout << "Which State? (enter a number)\n";
cout << "1. Alabama\n";
cout << "2. Alaska\n";
cout << "3. Arizona\n" << endl;
// We want to wait until we get a correct input and _then_
// do something with our input.
int num;
while (!(cin >> num))
{
// You might want to say something to your user if he doesn't input a correct number here.
// This will succeed if the user inputs 4, so you might also want to handle that case differently and ask the user to retry too in that case.
// The stream is now in error, you need to reset the state of the stream by clearing the error and emptying the buffer.
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
// Just showing another way of doing the if / else if dance in C/C++.
switch(num)
{
case 1: cout << "You chose Alabama" << endl; break;
case 2: cout << "You chose Alaska" << endl; break;
case 3: cout << "You chose Arizona" << endl; break;
default: cout << "I do not recognize the State" << endl; break;
}
}
// if we didn't receive 'a' or 'A' we just quit the program.
return 0;
}
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 8 months ago.
Improve this question
So in this small project for my school I needed to build a word guessing game everything works fine except the string university which always gives output incorrect, I tried changing it to universit and it works but university doesn't work. I cant figure out what the problem is.
Also are there any other alternatives to strcmp()?
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{ // at first we are basically going to give an intro to the gameseco
cout << " HANGMAN : GUESS THE WORD AND NOT DIE!!\n\n\n";
cout << " Welcome to the game player!!\n\n";
cout << " Rules: Guess the word by typing a letter based on given hints.\n";
cout << " Multiple incorrect guesses will HANG YOU.\n\n";
// here we add different words in the game for the guessing system using a 2 dimensional array
char word[20][20] = {
"higher",
"secondary",
"first",
"year",
"cotton",
"university",
"computer",
"science",
"project",
"hangman",
};
char newword[20], guess[10];
bool again = 1;
while (again == 1)
{
again = 0;
srand(time(0));
int x = 0, wrong = 0, z = (rand() % 10) + 0;
strcpy(newword, word[z]); // so we used strcpy command to copy an element of the 2D array of randomly generated index to a new string
cout << "Enter your guess. \nHINT: Word Length(indicated by underscores) = "; // hint no. one the gives us the exact lenth of word
for (x = 0; newword[x] != '\0'; x++)
{
cout << "-";
}
cout << "\nWord starts with: '" << newword[0] << "'"; // hint no. two which gives the player an idea what the word might be
cout << "\n\n";
for (wrong = 1; wrong <= 6; wrong++) // the loop here checks whether the input given by the user is correct or not
{
cin >> guess; // the input is taken here
if (strcmp(guess, newword) == 0) // the input is compared with the word to be guessed
{ // using the strcmp() function from the string.h library
cout << "Correct!\n"; // if the guess is correct the program will print correct and correct and end
break; // correct guess will terminate the loop
}
else if (wrong == 1)
{
cout << "Opps! Wrong guess!\n\n"; // if player inputs wrong word a poll will appear for hanging the person
cout << "I=-=\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 2)
{
cout << "Opps! Wrong guess again!\n\n"; // each wrong word will result in the appearance of of a hanging person
cout << "I=-=\n";
cout << "| |\n";
cout << "|\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 3)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "|\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 4)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "|\n";
cout << "I\n";
}
else if (wrong == 5)
{
cout << "Opps! Wrong guess again!\n\n";
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n";
cout << "I--|--\n";
cout << "| o\n";
cout << "I\n";
}
else if (wrong == 6)
{
cout << "Opps! Wrong guess again!\nLast Chance!!\n\n"; // unfortunately the player couldn't guess the word
cout << "I=-=\n";
cout << "| |\n";
cout << "| O\n"; // the hanging person compeletely appears and the program ends
cout << "|--|--\n";
cout << "| o\n";
cout << "I | |\n";
cout << "YOU ARE DEAD HAHAHA!!!";
}
}
cout << "Do you want to play again?\nPress 1 for yes, 0 for no."; // here it is asked if we want to play again
cin >> again;
}
return 0;
}
The problem seems to be this declaration:
char guess[10];
Your array declaration should always cater for a \0 character at the end of string. The length of "university" is 10 character, to hold the entire string you required an array of 11 characters.
So please change the length of guess array to be longest string length plus 1.
char guess[11];
As a side note:
Since you are using C++, you should fully utilize the facilities offers by the language. In C++, you should use std::string to store array of characters, use std::vector to store a collection of objects etc.
This question already has answers here:
How to test whether stringstream operator>> has parsed a bad type and skip it
(5 answers)
How to handle wrong data type input
(4 answers)
Closed 1 year ago.
I have a switch statement (copied in below) with cases and a default function, but if I enter a string, the code loops endlessly. How can I prevent this? Do I need to move something outside of the do loop? Use a different loop? I'm new to c++ and don't know how I can fix this, despite having tried moving things out of the loop.
void menu() //Opens new function, called menu, which will contain all code for the menu screen.
{
cout << setw(60); //Sets the screen width to 60 in order to centre all outputted text on the menu screen.
std::string text; //Declares the variable "text" as a string.
int choice{}; //Declares an integer "choice" that will contain the menu input.
int choicex{}; //Declares an integer "choicex" that will contain the input that will determine virus activation.
bool valid_input = false;
cout << setw(60);
cout << "ARES" << endl;
cout << setw(61);
std::cout << "MENU\n";
cout << setw(78);
std::cout << "Select one of the following options:\n";
cout << setw(67);
std::cout << "1. Activate Virus\n";
cout << setw(70);
std::cout << "2. Program Information\n";
cout << setw(62);
std::cout << "3. Exit\n";
do
{
std::cin >> choice;
switch (choice)
{
case 1:
system("CLS");
cout << setw(84);
std::cout << "Are you sure? Press 1 to continue, or 2 to go back."; // Prints the spaces
std::cin >> choicex;
if (choicex == 1)
{
system("CLS");
Ares();
}
if (choicex == 2)
{
system("CLS");
menu();
}
if (choicex != 1 and choicex != 2)
{
system("CLS");
cout << setw(80);
std::cout << "Invalid Input. Redirecting to menu.\n";
cout << setw(90);
system("pause");
system("CLS");
menu();
}
case 2:
ProgramInfo();
case 3:
system("CLS");
exit(EXIT_FAILURE);
default:
system("CLS");
cout << setw(60);
std::cout << "Invalid input.\n";
cout << setw(66);
std::cout << "Redirecting to menu.\n";
cout << setw(70);
std::cout << "Press any key to continue.\n";
std::cin.ignore();
system("CLS");
menu();
}
}
while (choice < 1 or choice > 3);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm having trouble with this do-while loop menu for a program I'm working on for school. I've checked, and as far as I'm concerned I have written the code correctly. However, when testing, if I type 'y' or 'n' the result is the same: the menu streaming down 100's of times non stop until I exit the program. Any idea on what I'm doing wrong and how I can get it to display the menu properly every time? Thanks in advance.
#include <iostream>
#include <iomanip>
#include <string>
#include "CashRegister.h"
#include "InventoryItem.h"
using namespace std;
int main()
{
// Variables
int selection, numUnits, cont;
double price;
// Use the first constructor for the first item
InventoryItem item1;
item1.setCost(5.0);
item1.setDescription("Adjustable Wrench");
item1.setUnits(10);
// Use the second constructor for the second item
InventoryItem item2("Screwdriver");
item2.setCost(3.0);
item2.setUnits(20);
// Use the third constructor for the remaining items
InventoryItem item3("Pliers", 7.0, 35);
InventoryItem item4("Ratchet", 10.0, 10);
InventoryItem item5("Socket Wrench", 15.0, 7);
do
{
cout << "#\t" << "Item\t\t\t" << "qty on Hand" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "1\t" << item1.getDescription() << "\t" << setw(3) << item1.getUnits() << endl;
cout << "2\t" << item2.getDescription() << "\t\t" << setw(3) << item2.getUnits() << endl;
cout << "3\t" << item3.getDescription() << "\t\t\t" << setw(3) << item3.getUnits() << endl;
cout << "4\t" << item4.getDescription() << "\t\t\t" << setw(3) << item4.getUnits() << endl;
cout << "5\t" << item5.getDescription() << "\t\t" << setw(3) << item5.getUnits() << endl;
cout << "Which item above is being purchased? ";
cin >> selection;
// Validate the selection
while (selection < 1 || selection > 5)
{
cout << "Error, please make a valid item selection: ";
cin >> selection;
}
cout << "How many units? ";
cin >> numUnits;
// Validate the quantity of units to make sure it isn't a negative value
while (numUnits < 0)
{
cout << "Error, please enter a valid quantity: ";
cin >> numUnits;
}
// Use a switch statement to figure out which cost to pull
switch (selection)
{
case 1: {price = item1.getCost();
item1.changeUnits(numUnits); }
break;
case 2: {price = item2.getCost();
item2.changeUnits(numUnits); }
break;
case 3: {price = item3.getCost();
item3.changeUnits(numUnits); }
break;
case 4: {price = item4.getCost();
item4.changeUnits(numUnits); }
break;
case 5: {price = item5.getCost();
item5.changeUnits(numUnits); }
break;
}
// Create a CashRegister object for this particular selection
CashRegister transaction(price, numUnits);
// Display the totals
cout << fixed << showpoint << setprecision(2);
cout << "Subtotal: $" << transaction.getSubtotal() << endl;
cout << "Sales Tax: $" << transaction.getSalesTax() << endl;
cout << "Total: $" << transaction.getPurchaseTotal() << endl;
// Find out if the user wants to purchase another item
cout << "Do you want to purchase another item? Enter y/n: ";
cin >> cont;
} while (cont != 'n' && cont != 'N');
system("pause");
return 0;
}
Your loop will never break unless you explicitly enter 110 which is the 'n' char in ASCII Codes or 78 which is the 'N'. So change your cont declaration from int cont; to char cont; and then you won't get the infinite loop anymore, and its condition will be valid to possibly break by then unless you have another hidden logical error which will require you to debug it.
I recently created a program that will create a math problem based on the user input. By entering either 1-4 the program can generate a problem or the user can quit by entering 5. The only problem I am having is that when I enter a character the program goes into an infinite loop. What function could I use to check if the input is not a number so I can display an error message?
//CIS180 Assignment #4
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//Declare variables.
int num1, num2, menuNum;
int addInput, subInput, multInput, divInput;
int addAnswer, subAnswer, multAnswer, divAnswer;
int addSolution, subSolution, multSolution, divSolution;
srand(time(0));
//Display menu.
cout << "Menu" << endl;
cout << "1. Addition problem" << endl;
cout << "2. Subtraction problem" << endl;
cout << "3. Multiplication problem" << endl;
cout << "4. Division problem" << endl;
cout << "5. Quit this program" << endl << endl;
cout << "Enter your choice (1-5): " << endl;
cin >> menuNum;
//Loop that will provide math problems when user inputs number.
while(menuNum != 5)
{
//Check if the input is valid.
while((menuNum < 1) || (menuNum >5))
{
cout << "The valid choices are 1, 2, 3 , 4, and 5. Please choose: " << endl;
cin >> menuNum;
}
//Generate two random numbers for addition and display output.
if(menuNum == 1)
{
num1 = rand()%500 + 1;
num2 = rand()%500 + 1;
addSolution = num1 + num2;
cout << setw(5) << right << num1 << endl;
cout << setw(2) << left << "+" << setw(3) << right << num2 << endl;
cout << setw(5) << fixed << "-----" << endl;
cin >> addAnswer;
//Check if the addition answer input is correct.
if(addAnswer != addSolution)
cout << "Sorry, the correct answer is " << addSolution << "." << endl;
else if(addAnswer == addSolution)
cout << "Congratulations! That's right." << endl << endl;
}
.
.
.
First off, you should detect whether your input attempt was successful: always check after reading that the read attempt was successful. Next, when you identify that you couldn't read a value you'll need to reset the stream to a good state using clear() and you'll need to get rid of any bad characters, e.g., using ignore(). Given that the characters were typically entered, i.e., the user had to hit return before the characters were used it is generally reaonable to get of the entire line. For example:
for (choice = -1; !(1 <= choice && choice <= 5); ) {
if (!(std::cin >> choice)) {
std::cout << "invalid character was added (ignoring the line)\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
The use of std::numeric_limits<std::streamsize>::max() is the way to obtain the magic number which makes ignore() as many characters as necessary until a character with the value of its second argument is found.
Read a single character
If this character is a digit, use it.
If this character wasn't a digit, goto 1.
Actually, forget about step 2. Just check whether it was one of the digit characters you actually want ('1', '2', '3', '4', '5'):
char choice;
while(cin.get(choice)){
if(choice == '5')
break;
switch(choice){
default: printWrongCharacterMessage(); break;
case '1': do1Stuff(); break;
case '2': do2Stuff(); break;
case '3': do3Stuff(); break;
case '4': do4Stuff(); break;
}
}
You can use isdigit from ctype.h
i need to prevent the junk left in the buffer as entering a value for a switch case menu from being used in a function called by the menu where their is user input.
menu code
void menu()
{
bool done = false;
string input;
while(!done)
{
cout << "Welcome to the DVD database." << endl;
cout << "1. Add A DVD" << endl;
cout << "2. Delete a DVD." << endl;
cout << "3. Edit a DVD." << endl;
cout << "4. List By Category." << endl;
cout << "5. Retrieve by a DVD by Title." << endl;
cout << "6. Display collection by year" << endl;
cout << "7. Display collection by title" << endl;
cout << "-999. Exit program" << endl;
cout << "Please choose an option by entering the corresponding number" << endl;
cin >> input;
int value = atoi(input.c_str());
switch(value)
{
case 1:addDVD(); break;
case 2:deleteDVD(); break;
// case 3:editDVD(); break;
case 4:listByCategory();break;
case 6:displayByYear();break;
case 7:displayByTitle();break;
case -999: writeToFile(); exit(0); break;
default : cout <<"Invalid entry"<< endl; break;
}
}
}
void retrieveByTitle()
{
string search;
int size = database.size();
int index = 0;
bool found = false;
cin.ignore();
cout << "Please enter the title of the DVD you would like to retrieve: " << endl;
getline(cin,search);
cout << search;
while(!found && index<size)
{
if(database.at(index)->getTitle().compare(search)==0)
{
cout << database.at(index)->toString();
break;
}
}
cout << endl;
}
if 5 is entered in the menu, the program skips the user input in the method
This code works, but it has the same problem you describe if you eliminate the 'cin.ignore()', which removes the extra delimiters ignored by the cin >> operator:
#include <iostream>
#include <climits>
using namespace std;
int main() {
string a, b;
while (true) {
cout << "write 'x' to exit: " << endl;
cin >> a;
if (a == "x") {
break;
}
cout << "read '" << a << "'" << endl;
cout << "now write a line: " << endl;
cin.clear(); // clears cin status
cin.ignore(INT_MAX); // clears existing, unprocessed input
getline(cin, a);
cout << "read '" << a << "'" << endl;
}
return 0;
}
When dealing with interactive user input you should use std::getline()
The std::cin is flushed to the application every time you hit <enter>. So this is the logical junks you should read data from the user in.
std::string answer;
std::cout << "Question:\n";
std::getline(std::cin, answer);
This gets you everything the user provided in response to the previous question.
Once you have the input you should get the value you think is on the input. Once you have this you should check if there is any other junk on the input (if there is then abort and re-try) otherwise validate the data you expected.
If you were expected an integer;
std::stringstream linestream(answer);
int value;
std::string junk;
if ((answer >> value)) && (!(answer >> junk)))
{
// If you got data
// and there was no junk on the line you are now good to go
}
In your specific example there is already a simple way to do this:
std::getline(std::cin, input);
int value = boost::lexical_cast<int>(input); // throws an exception if there is not
// an int on the input (with no junk)