Reputation: 19
I am doing a program for a class in school, and when I try to run the code I wrote below (only half of the project is done, but it's in a state where it should run anyways) the menu comes up fine, but then it jumps straight to the end of the program and wont let me input the important part..
When I remove the menu (which will be a necessity later when I finish the project) it works fine, but when it's there like I need it to be, it wont run properly
//Project 4 Written By Nate
#include <iostream>
#include <iomanip>
using namespace std;
int menuOption;
char stockName[21],symbol[10];
float openingPrice,closingPrice,numberShares,gain;
int main()
{
// MENU //
cout<<"Welcome to Project 4! Please select the program you would like to run:\n\n 1)Stock Program\n\nEnter your selection: ";
cin>>menuOption;
if(menuOption == 1) {
goto stockProgram;
} else
{
cout<<"Invalid response received. Program will now terminate";
return 0;
}
stockProgram:
cout<<"This program will ask you information about a stock you own.\n\n";
cout<<"Enter stock name: ";
cin.get(stockName,21);
cin.ignore(80,'\n');
cout<<"Symbol: ";
cin.get(symbol,10);
cin.ignore(80,'\n');
cout<<"Enter opening price: ";
cin>>openingPrice;
cout<<"Enter closing price: ";
cin>>closingPrice;
cout<<"Enter the number of shares: ";
cin>>numberShares;
cout<<"\n\n";
gain=(numberShares*closingPrice)-(numberShares*openingPrice);
cout<<setw(10)<<"Stock Name"<<setw(10)<<"Symbol"<<setw(10)<<"Opening"<<setw(10)<<"Closing"<<setw(10)<<"Shares"<<setw(11)<<"Gain\n";
cout<<setw(10)<<stockName<<setw(10)<<symbol<<setw(10)<<openingPrice<<setw(10)<<closingPrice<<setw(10)<<numberShares<<setw(10)<<gain<<"\n\n";
cout<<"=====================================================================\n";
cout<<" This gain could've been yours, too bad you are an anti-mac person.\n";
return 0;
}
Thanks..
Upvotes: 0
Views: 281
Reputation:
Adding cin.ignore()
after cin>>menuOption
- this will read the one int
that currently resides within the buffer & discard it since the EOF is the new line after input.
int main()
{
// MENU //
cout<<"Welcome to Project 4! Please select the program you would like to run:\n\n 1)Stock Program\n\nEnter your selection: ";
cin>>menuOption; cin.ignore();
if(menuOption != 1) {
cout<<"Invalid response received. Program will now terminate";
return 0;
}
//etc
}
Upvotes: 1
Reputation: 4000
You probably still have the a newline character or other characters after the 1 in the initial input. You've used cin.ignore on other inputs but not the first.
cout<<"Welcome to Project 4! Please select the program you would like to run:\n\n 1)Stock Program\n\nEnter your selection: ";
cin>>menuOption;
cin.ignore(80,'\n');
ignore will extract the delimiting \n
Also, whenever dealing with istream check that it was successful in getting the input into the correct type:
#include <limits>
#include <sstream>
int myVariable;
if( (cin >> myVariable).fail() )
{
// Error - input was not an integer
std::cerr << "Input was not an integer" << std::endl;
return -1;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Upvotes: 2