Why is control + P leading to infinite loop in C++ (Visual Studio)? - c++

I've been programming for a while (in Prolog, Scheme, and a little bit in C), but I recently decided to brush up on my C++ knowledge. I solved a problem that was meant to illustrate vectors. It was essentially a project to create a database that creates a vector to temporarily store the various games that the user inputs into it, and removes the ones they don't want. The code itself is running fine, not as pretty as scheme or Prolog could do it, but it works.
However, I accidentally typed "Control P" into the first prompt of the program and I got the strangest result: it started an infinite loop. I tried it again with "Control Z" and I got the same result. I haven't tried any other key combos, but I imagine that some others could be found. It's not a super worrying problem, but I am curious to know why it's doing this. Is it something about C++, or is it just Visual Studio? Anyway, here's the source:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "Welcome to the Cpp Games Database!";
int x = 0;
string game = "";
vector <string> games;
vector <string>::const_iterator iter;
while (x != 4){
cout<< "\n\nPlease choose from the list bellow to decide what you want to do:\n";
cout<< "1. Add Games to the Database.\n"
<< "2. Remove Games from the Database.\n"
<< "3. List all the Games.\n"
<< "4. Exit.\n"
<< "\n(Type the number of your choice and hit return)\n";
cin >> x;
switch (x){
case 1:
game = "";
do{
cout << "\nPlease Input a Game (type esc to exit): ";
cin >> game;
games.push_back(game);
} while (game != "esc");
games.pop_back();
break;
case 2:
game = "";
do{
cout << "\nPlease input the game you would like to remove(or type esc to exit): ";
cin >> game;
iter = find(games.begin(), games.end(), game);
if(iter != games.end())
games.erase(iter);
else cout << "\nGame not found, try again please.\n";
} while (game != "esc");
break;
case 3:
cout << "\nYour Games are:\n";
for (iter = games.begin(); iter != games.end(); iter++)
{
cout << endl << *iter << endl;
}
break;
default: break;
}
}
return 0;
}

Since you are not inputting valid data for cin it is stuck there waiting for the data to be reprocessed or discarded for new input. You need to check your input and only accept valid data. Essentially cin is keeping the original data it's given and continuously trying to process it.
Always verify your input and discard it if it's invalid.
Here is another answer on the same problem for some more insight (with source).
https://stackoverflow.com/a/17430697/1858323

Related

For Loops (C++)

Assignment:
The program should ask the user to enter a positive number and display all numbers from 1 to the input value. If the number is not positive, an error message should show up asking the user to re - enter the number.
My specific problem:
For my program, if the user enters an incorrect number and then re - enters a positive number, it does not display all the numbers from 1 to the input value. The program just ends.
#include <iostream>
using namespace std;
int main()
{
int userChoice;
int i = 1;
cout << "Enter a positive integer" << endl;
cin >> userChoice;
if (userChoice > 0)
{
for (i = 1; i <= userChoice; i++)
{
cout << "Loop 1:" << endl;
cout << i << endl;
}
}
else if (userChoice < 0)
cout << "Please re - enter" << endl;
cin >> userChoice;
system("pause");
return 0;
}
You need some sort of loop at the top of your program, that keeps asking for input until the user provides something valid. It looks like a homework assignment, so I will provide pseudo-code, not something exact:
std::cout << "Enter a number:\n";
std::cin >> choice;
while (choice wasn't valid) { // 1
tell the user something went wrong // 2
ask again for input in basically the same way as above // 3
}
// after this, go ahead with your for loop
It is actually possible to avoid the duplication here for step 3, but I worry that might be a little confusing for you, so one duplicated line really isn't such a big problem.
As an aside, you may wish to reconsider your use of what are often considered bad practices: using namespace std; and endl. (Disclaimer - these are opinions, not hard facts).

School project (c++ Inventory management program)

I recently started working on a project. The task is the following:
Write a program to keep a list of available items in a sports goods store. For each item, the following information must be stored: Name, Manufacturer, Price, Available items. The program should support the following features, as a text menu from which the user can choose:
• Enter a new item in the list
• Search by Manufacturer of an item
• Sort items in the list by Name
• Display the current content of the list
I was advised this on another topic:
1) For the menu, you would make a function that displays the information and returns the input after verifying that it's a valid input. You can use pass by reference. Don't bother about functions if you haven't learn it yet. Then just use switch case like you said.
2) For adding an item, (again make a function if you have learnt about it) make a temporary ITEMS object and ask the user for the various required inputs like price and then directly read the inputs into the object (eg. cin >> object.price). After getting all information, simply push the object into the vector we had.
3) For searching by Manufacturer's name you simple need to compare the given name with the manufacturer's name from each element of the vector.
4) Sort the elements of the vector based on their name parameter.
5) Simply print out all parameters of each element in the vector.
I will create a vector after learning more about them, and I will enter below the code which I have written so far. My questions here is should i follow the five tips on top or not. If you think I should not, please share your insights with different suggestions. I know this topic will probably be closed because I did read all the rules but still some information could be crucial. Thanks in advance and I'm sorry if this essay of mine has been time-consuming.
#include <iostream>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <vector>
using namespace std;
struct articles
{
char name[20];
char manufacturer[15];
double price;
char available;
};
int main()
{
int choice;
do
{
cout << ("\n ##################################################");
cout << ("\n # Menu #");
cout << ("\n # 1.Enter new article #");
cout << ("\n # 2.List of manufacturers of articles #");
cout << ("\n # 3.Sort articles by name #");
cout << ("\n # 4.Display the current content of the list #");
cout << ("\n # 5. End of program #");
cout <
< ("\n ##################################################") << endl;
cin >> choice;
switch (choice)
{
case 1:
articles newart;
cout << ("Enter a name: ");
cin >> (newart.name);
cout << ("Enter a manufacturer: ");
cin >> (newart.manufacturer);
cout << ("Enter a price: ");
cin >> (newart.price);
cout << ("Enter if its available (y/n): ");
cin >> (newart.available);
cout << ("The new article you've created is the following: \n");
cout << newart.name << endl;
cout << newart.manufacturer << endl;
cout << newart.price << endl;
cout << newart.available << endl;
break;
/* case 2:
//code
break;
case 3:
//code
break;
case 4:
//code
//extra information about the mode and the controller
break;
case 5:
cout << "End of Program.\n";
break;
*/
default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}
}while (choice != 5);
return 0;
}

Safe [Y/N]; [1/2/3/etc.] function

I tried to make a an introduction to a "game", and in its functions I made some Yes/No, 1/2/3, situations.
Im new to this however it wasn't that difficult, worked perfectly. The problem appeared when handling with invalid inputs. So this is what the code looks like by now:
#include "Introduction.h"
#include "GameConstants.h"
#include "PlayerCharacter.h"
#include <iostream>
#include <windows.h>
using namespace std;
Introduction::Introduction()
{
}
/////////Function N.1///////////
void Introduction::presentation()
{
char confirm;
string enteredName;
cout << constants.line() << "Welcome traveler! What is the name?" << endl;
getline(cin,enteredName);// Gets the WHOLE LINE as the name.
while (confirm != 'Y') //If the player doesn't confirm the name with 'Y' in will run again until it does.
{
cout << constants.xline() << "Your name is " << enteredName << " right? (Y/N)" << endl;
cin >> confirm; //The player's answer
cin.sync(); //Only takes the first character
confirm = toupper(confirm); //Turns player message into CAPS for easier detection in the "if" statements
if (confirm == 'N'){ //If not the correct name, gives another chance
cout << constants.xline() << "Please, tell me your name again..." << endl;
cin >> enteredName;
cin.sync();}
if ((confirm != 'Y')&&(confirm != 'N')){ //If an invalid input is entered, gives another chance. And insults you.
cout << constants.xline() << "Fool Go ahead, just enter your name again." << endl;
cin >> enteredName;
cin.sync();}
}
if (confirm == 'Y'){ //When the answer is yes ('Y') /* Uneeded line */
PC.setName(enteredName); //Saves the name
cout << constants.xline() << "Excellent! I have a few more questions for you " << PC.name() << "..." << endl;
}
}
//////////Function N.2///////////
void Introduction::difSelection(){
int selectedDif = 0; //Variable to store selected difficulty whitin this function.
Sleep(2500);
cout << constants.xline() << "What kind of adventure do you want to take part in?" << endl;
Sleep(2500); //Wait 2,5 s
cout << "\n1= Easy\n2= Normal\n3= Hard" << endl;
while(selectedDif != 1&&2&&3){ //Selected option must be 1/2/3 or will run again
cin >> selectedDif; //Sets the user selected difficulty
cin.sync(); //Gets only first character
if((selectedDif != 1||2||3)&&(!(selectedDif))){ //If the input isn't 1/2/3 AND is an invalid character, this will run. And it'll start again
cout << constants.xline() << "Criminal scum. Go again." << endl;
cin.clear();
cin.ignore();
}
if(selectedDif != 1&&2&&3){ //If selected option isn't 1/2/3, this will run and will loop again. However I know this conflicts with the previous statement since this will run anyways.
cout << constants.xline() << "Wrong input, please try again." << endl;
}
else if(selectedDif == 1){
constants.setDiff(1);
constants.setStatPoints(15);
} else if(selectedDif == 2){
constants.setDiff(2);
constants.setStatPoints(10);
} else if (selectedDif == 3){
constants.setDiff(3);
constants.setStatPoints(5);}
}
}
The first function works perfectly you can type "aaa" or "a a a" and will work. However I'd like to know if there's a simpler way to do it. (Understandable for a beginner, just started 3 days ago lol; if it includes some advanced or less known code prefer to stay like this by now).
Now, the second one, I really have no idea how to fix it. I need something that if the user's input was an invalid character type, throw certain message, and if it's an int type, but out of the range, another message. And of course, run again if it fails. Did a lot of search and couldn't find anything that meet this requirements.
To check if the user input is an int, you could use the good() function.
int val;
cin >> val;
if( cin.good() ) {
// user input was a valid int
} else {
// otherwise
}
As for the range check, the syntax is a bit different.
This returns true if the number is not equal to 1 nor 2 nor 3:
selectedDif != 1 && selectedDif != 2 && selectedDif != 3
Another shorter way would be to use:
selectedDif < 1 || selectedDif > 3
Another thing, in c++, there are two keywords break and continue which will allow to reduce the code in the loops.

Do-while loop issue: Try to develop a simple game

Well, I'm writing to make a dice game. I tried searching dice game here but none of it seems to answer my question. This isn't a problem about the dice roll thing anyway. It's about the do while loop. I am very new to this site, I just found out about this via Maximum PC Magazine so please bear with me. Also I am new to programming.
Here is my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(NULL));
int userRoll = rand() % 6 + 1 ;
int computerRoll = rand() % 6 + 1 ;
string yesOrNoChoice;
string commandToThrowDie;
do{
cout << "Please enter \"throw\" (lowercase) to roll the die: ";
cin >> commandToThrowDie;
} while(commandToThrowDie != "throw");
do{
cout << "You rolled: " << userRoll << endl
<< "The Computer rolled: " << computerRoll << endl;
if (userRoll < computerRoll){
cout << "You lose. Try again? [Yes/No]: ";
}
if (computerRoll < userRoll){
cout << "You win! Try again? [Yes/No]: ";
}
if (computerRoll == userRoll) {
cout << "It's a draw. Try again? [Yes/No]: ";
}
cin >> yesOrNoChoice;
} while(yesOrNoChoice != "Yes");
system ("pause");
return 0;
}
The problem is that after asking the user to enter a choice at the end of the do-while-loop the program exits loop no matter what I enter, instead of looping back to another throw of the die.
It ends up like this:
I copied your code and it compiled and ran perfectly. Doesn't make sense exactly, but no issues. I say it doesn't make sense since when "Yes" is entered that is what kills it. I believe what you want it while(yesOrNoChoice == "Yes"). Perhaps having it as != was making you think you were getting the wrong behavior? Also, you should be using if, else if, else statements, not just if.

else if looping quandry

I am in the second phase of a project where I need to extend my program into a menu driven application to query the database I have on a .txt file. So, my trouble is that I cannot get my loop to be perpetual. It always terminates when it initializes from one option to the next. Here is the snippet of my code that is my int main:
int main ()
{
char Q,q;
char S,s;
char task;
string pathname;
string z;
int count=0;
cout << "Welcome to Jason Rodriguez's Library Database." << endl;
cout << "Please enter the name of the backup file: ";
cin >> pathname;
ifstream inFile(pathname.c_str());
while(!inFile.eof())
{
getline(inFile,z);
count++;
}
while (task != 'Q' || task != 'q') {
cout << count << " records loaded successfully." << endl;
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
cin >> task;
if ((task == 'Q')||(task =='q'))
{
cout << "Program will now terminate";
break;
}
else if ((task == 'S')||(task =='s'))
{
showAll (loadData (pathname));
cout << endl;
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
cin >> task;
}
}
}
I need to add two more options into the loop on top of these two but I figured I should get the first two working correctly first. The other two should be plug & chug after that. Basically what I was trying to do is say if the user enters Q or q, terminate the program. Else, if user hits S or s, activate showall function and after ward, go back to the original query. It isn't working though. Assistance is welcome and appreciated.
Menus almost always require loops - especially ones that require the user to enter the correct choice input. The most applicable one in a case like this is the while loop - but essentially, any other loop variant can be used.
UPDATE:
int main ()
{
char task;//this is the only char needed. Your other chars were redundant
string pathname;
string temp;//I changed z to temp to better reflect its purpose
int count=0;
cout << "Welcome to Jason Rodriguez's Library Database." << endl;
cout << "Please enter the name of the backup file: ";
cin >> pathname;
ifstream inFile(pathname.c_str());//this is potentially a problem in that you aren't verifying that the pathname is a valid one
//you did not check to see that your file was open, otherwise there is no way to tell that you successfully opened the file
if (inFile.is_open()) {
//while(!inFile.eof()) is a character by character read and comparison
//made your life easier by shortening it down to this - which ensures
//that a line is read. (Much faster and more readable)
while(getline(inFile,temp))
{
count++;
}
inFile.close();//always close a file after you've used it
//At this point the entire file has been read. So, this is where this message SHOULD be
cout << count << " records loaded successfully." << endl;
}
else {
//if there was an error opening the file (i.e. wrong path, or it simply does not exist), this will be displayed
cout << "There was a problem opening your file" << endl;
exit(0);//and the program will terminate
}
while (task != 'Q' || task != 'q') {
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
cin >> task;
if ((task == 'Q')||(task =='q'))
{
cout << "Program will now terminate";
break;
}
else if ((task == 'S')||(task =='s'))
{
string author;
//showAll (loadData (pathname));
cout << endl;
cout << "Search an Author" << endl;
cin >> author;//get the author name to search from the user
//write code to search an author here
}
}
}
There are a number of issues with the code that you posted which I will forgo for the sake of brevity. Hence, note the following:
Your code was printing the same message per option (except for quit). Of course it would appear that it didn't work. Each option is a different task. Print what each task does (similar to what I did).
You wish to search the file for an author, but you have not stored it. Look into a way of storing it that appeases your instructor.
It would be ideal for you to use switch in this case, considering the increasing complexity of your code.
Try breaking down each task into functions, and call them to make your main function readable. In fact, it is a good programming practice for your main function to be as small as possible.
And, as juanchopanza quite rightly pointed out: you have some fundamental issues with C++. Try doing some more exercises and do more examples from a good C++ book.