开发者

Clearing a carriage return from memory in C++

I have the following code:

int main()
{
// Variables
char name;

// Take the users name as input
cout << "Please enter you name..." << endl;
cin >> name;


// Write "Hello, world!" and await user response
cout << "Hello, " << name << "!" << endl;
cout << "Please press 开发者_StackOverflow社区[ENTER] to continue...";
cin.get();

return 0;

}

After the user hits return to enter their name, that carriage return is carried forward to the end of the code where it is immediately applied as input to cin.get(), thus ending the program prematurely. What can I place on the line immediately following

cin >> name;

to stop this from happening? I know that it's possible, as I've done it before, but can't remember what it is or where I can find it. Thanks a lot in advance.


Really you want to use everything on the input upto the newline as the name.
Currently your code only reads the first word.

#include <iostream>
#include <string>

int main()
{
    // Variables
    std::string name;

    // Take the users name as input
    // Read everything upto the newline as the name.
    std::cout << "Please enter you name..." << std::endl;
    std::getline(std::cin, name);

    // Write "Hello, world!" and await user response
    // Ignroe all input until we see a newline.
    std::cout << "Hello, " << name << "!\n";
    std::cout << "Please press [ENTER] to continue..." << std::flush;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
}


simplest answer:

int main()
{
// Variables
char name;

// Take the users name as input
cout << "Please enter you name..." << endl;
cin >> name;
cin.get(); // get return here


// Write "Hello, world!" and await user response
cout << "Hello, " << name << "!" << endl;
cout << "Please press [ENTER] to continue...";
cin.get();

return 0;
}


You can tell your stream to ignore the next N characters with

cin.ignore(1000, '\n');

This will cause cin to skip up to 1000 characters or until it found (and removed) a newline ('\n') character. Other limits or terminating characters can be specified as you desire.


cin ignores the carriage return, the value before '\n' is stored in the variable and '\n' remains in the input stream. When cin.get() is called, it takes the value already in the input stream (which is '\n') and and thus it gets skipped.

To avoid this situation Tim's answer is perfect!

cin >> name;
cin.get(); // get return here

Alternatively you can also do

(cin >> name).get(); // get return as soon as cin is completed.

UPDATE : As pointed out by Loki Astari, When using cin>> operator to the array name, it will read up to the first white space character. In 'Tim Hoolihan' there is a space between the names. Thus name array will store {'T','i','m','\n'} as the value, '\n' marking an end to the string. One should avoid using char array for strings, instead use the class string from #include<string> header file. A string can contain white space character in between, whereas an array can't.

#include<iostream>
#include<string>

int main()
{
  string name; // string is a class, name is the object
  cout << "Please enter you name..." << endl;
  (cin >> name).get(); 
  cout << "Hello, " << name << "!" << endl;
  // String using the "power" of an array
  cout << "First two characters of your name are " << name[0] << name[1] << endl;
  cout << "Please press [ENTER] to continue...";
  cin.get();
  return 0;
}


First of all name is a string and you are reading it in a char.

char name;

should be

string name;

Also add #include<string>

Now to clear the newline from the buffer you can do:

std::cin.ignore(1);

after reading the name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜