Skip to main content
added 2 characters in body
Source Link
Lightness Races in Orbit
  • 386.8k
  • 77
  • 670
  • 1.1k

Its because you have a '\n''\n' left lying on the input stream from a previous call.

cin >>> i; // This reads the number but the '\n' you hit after the number   // is still on the input. 

The easiest way to do interactive user input.
Is is to make sure each line is processed independently (as the user will hit enter after each prompt).
As

As a result always read a line. Then processes, then process the line (until you get familiar with the streams).

std::string line; std::getline(std::cin, line); std::stringstream  linestream(line); // Now processes linestream. std::string garbage; lienstream >> i >> garbage; // You may want to check for garbage after the number. if (!garbage.empty()) { std::cout << "Error\n"; } 

Its because you have a '\n' left lying on the input stream from a previous call.

cin > i; // This reads the number but the '\n' you hit after the number // is still on the input. 

The easiest way to do interactive user input.
Is to make sure each line is processed independently (as the user will hit enter after each prompt).
As a result always read a line. Then processes the line (until you get familiar with the streams).

std::string line; std::getline(std::cin, line); std::stringstream  linestream(line); // Now processes linestream. std::string garbage; lienstream >> i >> garbage; // You may want to check for garbage after the number. if (!garbage.empty()) { std::cout << "Error\n"; } 

Its because you have a '\n' left lying on the input stream from a previous call.

cin >> i; // This reads the number but the '\n' you hit after the number   // is still on the input. 

The easiest way to do interactive user input is to make sure each line is processed independently (as the user will hit enter after each prompt).

As a result always read a line, then process the line (until you get familiar with the streams).

std::string line; std::getline(std::cin, line); std::stringstream linestream(line); // Now processes linestream. std::string garbage; lienstream >> i >> garbage; // You may want to check for garbage after the number. if (!garbage.empty()) { std::cout << "Error\n"; } 
Source Link
Loki Astari
  • 266.5k
  • 87
  • 344
  • 575

Its because you have a '\n' left lying on the input stream from a previous call.

cin > i; // This reads the number but the '\n' you hit after the number // is still on the input. 

The easiest way to do interactive user input.
Is to make sure each line is processed independently (as the user will hit enter after each prompt).
As a result always read a line. Then processes the line (until you get familiar with the streams).

std::string line; std::getline(std::cin, line); std::stringstream linestream(line); // Now processes linestream. std::string garbage; lienstream >> i >> garbage; // You may want to check for garbage after the number. if (!garbage.empty()) { std::cout << "Error\n"; }