getline() Problem

I'm having a bit of a problem with getline(). In line 66, the "cin" will not allow any spaces (ex: White Wolf), but when I change it to getline(), it works, however, it changes the program and ends up doing something I don't want it to. What can I do?


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void printHeader();
void printErrorMessage(int num);
double getPrice(string prompt);
bool isBidPriceGood(double bidPrice, double reservePrice);
void printWinner(string bidder, string lotName, double bid);
void calcWinner(string bidder1, string bidder2, string lotName,
double bid1, double bid2, double reservePrice);
string getName(string prompt);


int main()
{
string lotName;
double reservePrice;
cout << "Enter lot name: ";
getline(cin, lotName);
reservePrice = getPrice("Reserve price: $ ");

if(reservePrice < 0)
printErrorMessage(5);

else
{
string bidder1;
cout << endl;
bidder1 = getName("Bidder 1 ID: ");
if (bidder1 == "") // fix this, ask question about this...
printErrorMessage(3);

else
{
double bid1;
bid1 = getPrice("Bidder1 price: $ ");

if(bid1 < 0)
printErrorMessage(2);

else if(reservePrice > bid1)
printErrorMessage(1);

else
{
cout << endl;
cout << bidder1;
cout << " is high bidder, current price = $";
cout << reservePrice;
}

}
}
return 0;
}

string getName(string prompt)
{
string bidder;
cout << prompt;
cin >> bidder;
return bidder;
}

double getPrice(string prompt)
{
double price;
cout << prompt;
cin >> price;
return price;
}

void printHeader()
{
cout << "----------------------------------------" << endl
<<" bCreek" << endl
<<" Fine Cereal Sales" << endl
<<"----------------------------------------" << endl << endl;
cout << fixed << showpoint;
cout << setprecision(2);
}

void printErrorMessage(int num)
{
if (num == 1) {
cout << endl
<< " ERROR: Reserve not met, bid rejected" << endl << endl;
} else if (num == 2) {
cout << endl
<< " ERROR: Negative price, bid rejected" << endl << endl;
} else if (num == 3) {
cout << endl
<< " ERROR: Blank bidder ID, no bid allowed" << endl << endl;
} else if (num == 4) {
cout << endl
<< "ERROR: Neither bidder met Reserve, auction canceled" << endl << endl;
} else if (num == 5) {
cout << endl
<< "ERROR: Reserve is not positive, auction canceled" << endl << endl;
} else {
cout << " This should never print" << endl << endl;
}
}
[/code]
Last edited on
but when I change it to getline(), it works, however, it changes the program and ends up doing something I don't want it to.

Can you get a bit more vague?
If I change line 66 to getline(), it doesn't let me enter a string for line 34 and simply displays the error message in line 36. the error message in line 36 is only suppose to execute when nothing is put into line 66, but it doesn't allow me to put anything into it in the first place.
Last edited on
Input taken with the >> operator stops at whitespace so if you want the "line" (all string info up to CR) you will need to use cin.getline() if the input goes into a cstring. You could also use std::getline() if you want to read from cin into a string. If you want to mix using getline() and the >> operator you need to know that there may be leftovers in the buffer that will trip you up. You can read up about it here: http://augustcouncil.com/~tgibson/tutorial/iotips.html

The operator >> skip all leading whitespace characters before extracting the desired value but does not discard the end of line character left in the buffer. On line 74, a double value is extracted and stored into price. The marker position in the input stream is left on '\n'.

getline() does not skip the leading whitespace characters and will extract any character until it finds a delimiter character which is, by default, the new line character '\n'.

Therefore, when statement 66 is executed, getline() read the input buffer and finds the new line character immediately. bidder1 ends up with an empty string.
Please note that getline(), after having extracted the desired characters, will discard the delimiter characters.

You can verify this by ignoring the first character left ('\n') in cin buffer after line 74:

1
2
3
4
5
6
7
8
double getPrice(string prompt)
{	
	double price;
	cout << prompt;
	cin >> price;
        cin.ignore();
	return price;
}


Hope it helps!



References:
The reading Marker and the Newline Character - Programming and Problem Solving with C++ by Nell Dale,Chip Weems: http://books.google.be/books?id=bSy1hBCLNl8C&pg=PA137&lpg=PA137&ots=mmMgc6_ruN&dq=cin+extract+operator&hl=nl#v=onepage&q=cin%20extract%20operator&f=false
getline() - cplusplus.com: http://www.cplusplus.com/reference/string/getline/
ignore() - cplusplus.com: http://www.cplusplus.com/reference/iostream/istream/ignore/
Last edited on
Topic archived. No new replies allowed.