我正在试着写一个程序来找出Hangman游戏中最常见的角色。我希望用户在标准输入中输入一个电影名称列表(用enter分隔)。电影名称的长度是可变的。
但是,在输入MOVIE_NAME_SIZE的所有字符之前,程序不会停止接受输入。我尝试检查'\0‘和'\n’。它似乎不起作用。我做错了什么?另外,有没有更简单的方法来做到这一点?
附言-如果之前有人问过这个问题,我很抱歉。我看了一下,有人建议使用一个名为getline()的方法,但这是针对整个字符串的,而我需要访问单个字符。
char movieList[MAX_MOVIES][MAX_NAME_SIZE];
char CapitalAlphabetList[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char smallAlphabetList[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char numberList[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int alphabetCount[26] = {0};
int numberCount[10] = {0};
int i, j, k;
for(i = 0; i < MAX_MOVIES; i++)
{
for(j = 0; j < MAX_NAME_SIZE; j++)
{
cin >> movieList[i][j];
if(movieList[i][j] == '\0')
break;
for(k = 0; k < 26; k++)
if((movieList[i][j] == smallAlphabetList[k]) || (movieList[i][j] == CapitalAlphabetList[k]))
alphabetCount[k]++;
for(k = 0; k < 10; k++)
if(movieList[i][j] == numberList[k])
numberCount[k]++;
}
}发布于 2015-07-23 19:18:41

简而言之,这就是在程序中使用strcmp()和cin.getline()在循环中遇到空的ENTER按键时停止的方法。用这些线条。这是因为当按enter键时,cin会添加一个'\0'。
§按照C++11 27.7.2.2.3/9中的规定,从std::istream到字符数组的格式化输入将以空结束输入:
#include <iostream>
#include <string.h>
using namespace std;
int main () {
char name[256]={' '};
while( true ){
memset(name,'\0',sizeof(name)); // reset char array
cout << ">";
cin.getline (name,256); // cin addes a \0 when empty enter is pressed.
cout<<"\n"<<name<<"\n";
if ( strcmp( name, "\0" ) == 0 ) break; // break at empty enter key
}
cout << " \nPress any key to continue\n";
cin.ignore();
cin.get()
return 0;
}必须修改程序,现在while()循环处理输入,直到按下一个空的enter键。然后,它会按照您的预期运行。
修复代码:
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main () {
const int MAX_MOVIES = 100;
const int MAX_NAME_SIZE = 100;
char movieList[MAX_MOVIES][MAX_NAME_SIZE];
char CapitalAlphabetList[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char smallAlphabetList[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char numberList[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int alphabetCount[26] = {0};
int numberCount[10] = {0};
int i=0, j=0, k=0 , l=0;
memset(movieList,'\0',sizeof(movieList));
cout<<"Movie Title Hangman \n\n";
while( true ){
for(l=0;l<i;l++)cout<<movieList[l]<<"\n";
cout<<"Turn number "<<i<<" , enter movie >";
memset(movieList[i],'\0',MAX_NAME_SIZE);
cin.getline (movieList[i],256);
cout<<"------------------------------------------\n";
if( strcmp(movieList[i],"\0") ==0){
for (i=0;i<MAX_MOVIES ;i++){
for(j=0;j<MAX_NAME_SIZE ;j++){
for(k = 0; k < 26; k++){
if((movieList[i][j] == smallAlphabetList[k]) || (movieList[i][j] == CapitalAlphabetList[k]))
alphabetCount[k]++;
}
for(k = 0; k < 10; k++){
if(movieList[i][j] == numberList[k])
numberCount[k]++;
}
}
}
break;
}
i++;
}
cout<<"\n\n";
cout<<"The most common characters are: \n\n";
for(k = 0; k < 26; k++) cout<<""<<smallAlphabetList[k] <<" = "<< alphabetCount[k]<<"\t";
cout<<"\n\n";
for(k = 0; k < 10; k++) cout<<""<<numberList[k] <<" = "<< numberCount[k]<<"\t";
cout << " \nPress any key to continue\n";
cin.ignore();
cin.get()
return 0;
}https://stackoverflow.com/questions/31576317
复制相似问题