cin.cin.getline() can run into problems when used with cin >> var.#include<iostream>#include<iomanip>iostream and you can't go wrong#include<fstream>cin.int i; float fl; std::cin >> fl; std::cin >> i;
3.14<return>42<return>
fl . The carriage return (newline) following the 3.14 is still sitting on the input buffer.std::cin >> i . Then the integer 42 is read into i and the second return is left on the input buffer.std::cin.getline() can run into problems when used with std::cin >> var.getline can be provided a third argument--a "stop" character. This character ends getline's input. The character is eaten and the string is terminated. Example:std::cin.getline(str, 100, '|')std::cin.getline() is not provided a "stop" character as a third argument, it will stop when it reaches a newline.float fl; std::cin >> fl; char str[101] std::cin.getline(str, 101);
3.14<return>fl . The newline following the 3.14 is still sitting on the input buffer.std::cin.getline(str, 101) immediately processes the newline that is still on the input buffer. str becomes an empty string.std::cin.getline() statement.The solution is to add std::cin.ignore(); immediately after the first std::cin statement. This will grab a character off of the input buffer (in this case, newline) and discard it.
std::cin.ignore() can be called three different ways:
std::cin.ignore(); //discard 1 character std::cin.ignore(33); //discard 33 characters std::cin.ignore(26, '\n'); //ignore 26 characters or to a newline, whichever comes first std::cin.clear()!std::cin)std::cin.clear() to take the stream out of the "fail" state.std::cin.ignore(...)
#include<limits> //for numeric_limits
float fl;
int bad_input;
do{
bad_input=0;
std::cin >> fl;
if(!std::cin)
{
bad_input=1;
std::cin.clear();
std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
}
}while(bad_input);
#include<limits> //for numeric_limits
float fl;
while(!(std::cin >> fl))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
}
A note on limits. If your compiler doesn't support std::numeric_limits<streamsize>::max(), an alternative is to use the c-style method for determining the maximum integer allowed:
#include<climits> ... std::cin.ignore(INT_MAX, '\n');
#include <cstdlib> ... int i; float fl; char temp[100]; std::cin.getline(temp, 100); fl=strtof(temp,0); std::cin.getline(temp, 100); i=strtol(temp,0,10);
std::ifstream someVarName("data.txt");
float fl;
char temp[100];
someVarName.getline(temp, 100);
fl=strtof(temp);
int i;
someVarName >> i;
std::ifstream inf("data.txt");
char temp[100];
while(!inf.getline(temp, 100).eof())
{
//process the line
}
char temp[100]; std::cin.getline(temp, 100, '|');
John|83|52.2 swimming|Jefferson Jane|26|10.09 sprinting|San MarinProcess using:
std::ifstream inf("data.txt");
char name[30];
while(!inf.getline(name, 30, '|').eof())
{
Athlete* ap;
char jersey_number[10];
char best_time[10];
char sport[40];
char high_school[40];
inf.getline(jersey_number, 10, '|'); #read thru pipe
inf.getline(best_time, 10); #read thru newline
inf.getline(sport, 40, '|'); #read thru pipe
inf.getline(high_school, 40); #read thru newline
ap = new Athlete(name, strtod(number), strtof(best_time), sport, high_school);
//do something with ap
}
All of the previous examples have assumed that C-style strings (null-terminated character arrays) were being used. C++ provides a string class that, when combined with a particular "getline" function, can dynamically resize to accommodate user input. In general, C++ strings are preferred over C strings.
Here is the same code shown above, this time using C++ strings:
#include <string>
std::ifstream inf("data.txt");
string name;
while(!std::getline(inf, name, '|').eof())
{
Athlete* ap;
std::string jersey_number;
std::string best_time;
std::string sport;
string high_school;
std::getline(inf, jersey_number, '|'); #read thru pipe
std::getline(inf, best_time); #read thru newline
std::getline(inf, sport, '|'); #read thru pipe
std::getline(inf, high_school); #read thru newline
ap = new Athlete(name, strtod(number.c_str()),
strtof(best_time.c_str()), sport, high_school);
//do something with ap
}
std::cout.setf(std::ios::fixed, std::ios::floatfield); std::cout.setf(std::ios::showpoint); std::cout.precision(2);
int one=4, two=44;
std::cout << one << std::endl.;
//output: "4"
std::cout << setw(2) << one << std::endl.;
//output: " 4"
std::cout.fill('X');
std::cout << setw(2) << one << std::endl.;
//output: "X4"
std::cout.fill('X');
std::cout << setw(2) << two << std::endl.;
//output: "44"
| 2012 April 18 | Added std:: to ios flags |
| 2007 March 1 | Corrected spelling error |
| 2010 February 18 | Corrected HTML error |