Limit input when a number is entered...

I've been away from programming for a loong time, so I have a basic question regarding a program I'm writing for class.

How do I end input if the user types -999 in the following program (the bold part):

#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
using namespace std;


struct TraderInfo
{
int traderNumber;
string traderName;
double traderProfit;

};

int main()
{
TraderInfo trader[10];
int index = 0;



while (index < 10)

{
cout << "Enter trader " << index + 1 << " number (or -999): ";
cin >> trader[index].traderNumber;
cout << "\n";
cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
cout << "Enter trader " << index +1 << " name: ";
getline(cin, trader[index].traderName);
cout << "Enter trader " << index + 1 << " profit: ";
cin >> trader[index].traderProfit;
cout << endl;
index++;
}

index = 0;
while (index < 10)
{
cout << "Trader " << index + 1 << " Number: " << trader[index].traderNumber << endl;
cout << "Trader " << index + 1 << " Name: " << trader[index].traderName << endl;
cout << "Trader " << index + 1 << " Profit: " << trader[index].traderProfit << endl;
cout << endl;
index++;
}

system ("PAUSE");
return 0;
}
Last edited on
1
2
cout << "Enter trader " << index + 1 << " number (or -999): ";
cin >> trader[index].traderNumber;


Use an if statement after these lines above, to check if the user entered -999, then break; from the loop.
Topic archived. No new replies allowed.