2

The system did this:

Please input the Full Name of the user:Please input the Full Name of the user:

It output the string "Please input the Full Name of the user:" twice , how do i change the code to make it just cout once

string fullname = "";

    do
    {
    cout << "Please input the Full Name of the user: ";
    getline (cin,fullname);
    }while(fullname.length()<1);

C++ What is causing the system to output twice

baoky chen
  • 799
  • 2
  • 11
  • 20

4 Answers4

3

You could try flushing your input stream to get rid of leftover newlines: std::cin.ignore(x); (with x being the number of characters to ignore, e.g. INT_MAX).

f00id
  • 113
  • 5
2

You are performing an input operation without checking the result, which is a hard programming and understanding error. Do this instead:

for (std::string line; ; )
{
    std::cout << "Name: ";
    if (!std::getline(std::cin, line) || !line.empty()) { break; }
}

The first condition checks whether the input succeeded (which is false when the input stream is closed), and the second checks whether the read line is non-empty. The short-circuit semantics of || make the second check legal.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • That will still print "Name:" twice. Thus not solving the OP current question. – Martin York Aug 14 '12 at 16:05
  • 1
    @LokiAstari: Correct, it'll print the prompt until it reads an acceptable response. There are a million variations of this depending on how you want to prompt. Personally, I'd have a separate initial prompt, followed by "Try again" prompts, which I think cause the least amount of user confusion. – Kerrek SB Aug 14 '12 at 16:06
2

Simple solution would be to move the std::cout statement outside of the do-while loop.

string fullname = "";
cout << "Please input the Full Name of the user: ";
do
{ 
    getline (cin,fullname);
}while(fullname.length()<1);
Derek
  • 1,104
  • 13
  • 35
  • 1
    Yes. Good programming practice. Side step the problem by making the problem less visible. The problem here is that you have not fixed the problem (you are just hiding it). This kind of fix results in an accumulation of [technical debt](http://en.wikipedia.org/wiki/Technical_debt) that one day will come back and kick you in the butt. – Martin York Aug 14 '12 at 16:07
  • I completely agree that this is going around the actual problem. I did label this as a simple solution. – Derek Aug 14 '12 at 16:16
0

As others have pointed out the problem is that you have an extra '\n' character on the input stream.

Contrary to the popular answer I don't think flushing (ignore()) the current input is a good solution. You are treating the symptom not the problem. If you are using ignore() you are potentially throwing away user input that you might actually want or something that could have detected an error from the user:

> Input Your age
> 36xxxx

// If you use
std::cin >> age;
// Then sometime later in your code you use
// ignore to make sure that you have "correctly" skipped to the next new line
std::ignore(std::numeric_limits<std::streamsize>::max(), '\n');

// You have now just ignored the fact that the user typed xxx on the end of the input.
// They were probably doing that to force an error in the code or something else erroneous
// happened but you just missed it with std::ignore()

The best solution is not to get into this situation.
This problem is caused by using a combination of operator<<() and std::getline() to parse user input. I love using operator<<() to parse normal or regular input; but manual user input (ie Question/Answer) is harder to predict and users input is line based (there input ends at the '\n' character because the buffer is flushed when they hit <enter>).

As a result when I parse manual user input I always use std::getline(). This way I know I got the whole of their answer. It also allows me to validate the input to make sure there was not a typing error.

 std::cout << "What is your age\n";

 std::string        line;
 std::getline(std::cin, line);   // Get user input

 // Use stream for easy parsing.
 std::stringstream  linestream(line);

 // Get value we want.
 linestream >> age;

 // Validate that we have not thrown away user input.
 std::string error;
 linestream >> error;
 if (error.length() != 0) { /* do stuff to force user to re-input value */ }
Martin York
  • 257,169
  • 86
  • 333
  • 562