Hi everyone, any idea why the first time you run through the loop where you enter the information for the product that it skips the first cin? very strange!
any help would be great.
royal1664
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void desc();
void error();
void select();
void desktop();
void laptop();
void tablet();
int deskOutput(int dSum);
int lapOutput(int lSum);
int tabOutput(int tSum);
return 0;
}
void select()
{
int nSelect;
cout << "Enter a number to select which product to enter information for:" << endl
<< "1. Desktop" << endl
<< "2. Laptop" << endl
<< "3. Tablet" << endl;
cin >> nSelect;
if (nSelect == 1)
{
desktop();
}
if (nSelect == 2)
{
laptop();
}
if (nSelect == 3)
{
tablet();
}
if (nSelect < 1)
{
error();
}
if (nSelect > 3)
{
error();
}
}
void desc()
{
cout << "This program allows you to enter information about a product.\n" << endl;
}
void error()
{
cout << "You've entered an incorrect number.\n";
select();
}
void desktop()
{
int amount;
cout << "\nHow many desktops would you like to enter?\n";
cin >> amount;
This code won't compile for me because some of your functions are supposed to return values but they don't.
if you are going to use statements like this cin >> a; to input data you should follow each statement with cin.ignore(); to remove the newline left in the input stream from the user pressing enter.
How does simply putting cin.ignore(); after a cin >> actually work?
If you enter "Hello, Hi", your line actully looks like: "Hello, Hi\n"
operator>> will leave whitespaces, so after first std::cin >> xx; your buffer will look like: " Hi\n" (note whitespace in the beginning). After second: "\n" (newline symbol). getline will tale all symbols unti it encounter a delimeter (newline by default). As it encounters it immidetly, it returns an empty string.
std::cin.ignore() will skip one symbol (usually a newline in your input)