2010-09-15 51 views
4

回车我有以下代码:清除从存储器中C++

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 [ENTER] to continue..."; 
cin.get(); 

return 0; 

}

用户点击后返回到输入他们的名字,即回车结转到的结束代码立即作为cin.get()的输入应用,从而提前结束程序。我可以放置在什么紧随

cin >> name; 

,以阻止这种情况发生的线?我知道这是可能的,就像我以前做过的那样,但不记得它是什么或我能在哪里找到它。提前致谢。

回答

4

简单的答案:

所有名的
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; 
} 
+0

我同意,这是他们在大学第一年向我们展示的方式。它是一个简单的解决方案,以捕获额外的字符:)我确定有一个替代这个虽然。 – JonWillis 2010-09-15 13:29:52

+2

如果用户输入“Tim Hoolihan ” – 2010-09-15 14:16:15

+0

@LokiAstari无法确定为什么它不起作用,那么这将不起作用。请详细说明。 – 2014-08-24 15:00:01

0

首先是一个字符串,你正在阅读它在char

char name; 

应该

string name; 

另外补充#include<string>

我们清除缓冲区换行,你可以这样做:

std::cin.ignore(1); 

阅读name后。

+0

std :: cin.clear是否*不*删除任何悬而未决的输入 – 2010-09-15 13:37:49

+0

@Bart:感谢您的指点。我打算使用'cin.ignore' – codaddict 2010-09-15 13:43:23

+0

我们在读完名字后忽略了第一个字符。这恰好是换行符。 – codaddict 2010-09-15 13:49:40

3

你可以告诉你流与

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

这忽略了未来N个字符会导致cin跳过多达1000个字符,或者直到它发现(和删除)一个换行符('\n')字符。其他限制或终止字符可以根据需要指定。

6

真的,你想使用输入上的所有内容作为名字。
当前您的代码只读取第一个单词。

#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') 
} 
+1

+1为最佳做法的答案。我的确是管道磁带的答案 – 2010-09-15 15:26:23

0

cin忽略回车,'\n'前的值存储在变量和'\n'保持在输入流。当调用cin.get()时,它将取得输入流中的值(即'\n'),并因此被跳过。

为了避免这种情况,Tim的回答很完美!

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

或者你也可以做

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

UPDATE:由于洛基阿斯塔,指出当使用cin>>运营商的阵列name,它会读取到第一个空格字符。在'Tim Hoolihan'中,名字之间有一个空格。因此,名称数组将存储{'T','i','m','\n'}作为值,'\n'标记字符串的结尾。应该避免使用char数组作为字符串,而应使用#include<string>头文件中的类string。字符串之间可以包含空格字符,而数组不能包含空格字符。

#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; 
}