2015-07-21 84 views
1

这是程序:CIN是不是在等待输入

#include <iostream> 
#include <string> 
#include <stdlib.h> 


using namespace std; 

int main(){ 


string strKingdom = ""; 
bool conquered_me;//see if was conquered, was going to use this on other program and true = game over. 
int gold; 
int food; 
int citizens; 
int soldiers; 



cout << endl <<"Name of kingdom: "; 
cin >> strKingdom; 
cout << endl << "were you conquered (true/false): "; 
cin >> conquered_me; 
cout << endl << "How many gold do you have?:"; 
cin>>gold; 
cout << endl << "How many food do you have?:"; 
cin >> food; 
cout << endl << "How many citizens do you have?:"; 
cin >> citizens; 
cout << endl << "How many soldiers do you have?:"; 
cin >> soldiers; 


return 0; 
} 

的问题是,当我编译它的编程'只让我插入的第2个变量,然后它显示的剩余的问题(编译后):

王国的名字:史蒂夫

是你征服(真/假):假

多少黄金,你有?: 多少FOO d你有吗?: 你有几个公民?: 你有几个士兵?:

+0

你给的确切输入是什么?我猜测它正在读取您给出的“6”值的“2”输入。 (你有没有空间在你的王国的名字吗?看看:http://www.cplusplus.com/reference/string/string/getline/ –

+2

只是不要使用'istream :: operator >>' 。为了获得用户输入,使用'std :: getline()'并解析结果字符串。 –

+1

因此,存储std :: string是唯一可用的。下面的数据类型,包括那bool,导致你的失控行为,这是你的一个线索:-) – donjuedo

回答

0

您需要使用std :: getline和std :: string来读取各种值。 (然后你可以使用像atoi这样的函数来解析它们。)以下是使用std :: getline函数的代码示例。

#include <iostream> 
#include <string> 
#include <cstdlib> 

int main(){ 


std::string strKingdom = ""; 
bool conquered_me;//see if was conquered, was going to use this on other program and true = game over. 
int gold; 
int food; 
int citizens; 
int soldiers; 

std::string tString = ""; // Used to read and subsequently parse the string. 


std::cout << std::endl <<"Name of kingdom: "; 
std::getline(std::cin,strKingdom); 

std::cout << std::endl << "were you conquered (true/false): "; 
std::getline(std::cin,tString); 
conquered_me = (tString == "true"); 

std::cout << std::endl << "How many gold do you have?:"; 
std::getline(std::cin,tString); 
gold = std::atoi(tString.c_str()); 

std::cout << std::endl << "How many food do you have?:"; 
std::getline(std::cin,tString); 
food = std::atoi(tString.c_str()); 

std::cout << std::endl << "How many citizens do you have?:"; 
std::getline(std::cin,tString); 
citizens = std::atoi(tString.c_str()); 

std::cout << std::endl << "How many soldiers do you have?:"; 
std::getline(std::cin,tString); 
soldiers = std::atoi(tString.c_str()); 


return 0; 
} 
+0

它的工作原理!谢谢。 – Murteira

+0

不,不。例如,对于'conqured_me',输入可以是“真实的”(带有空格),它会被解释为false。或者如果你没有在'gold'或'food'中输入数字,你会得到一个未捕获的异常。 – sbabbi

+0

@sbabbi,其实它更有趣一点。我们已经有了stdlib.h和字符串,还有一个'using namespace std',这样我们就可以得到一点混沌,它决定了我们将要得到的atoi,旧的C或者C++ 11。 – user4581301

7

输入字符串“true”到bool变量不起作用。您应该输入10。你的“真”不能被“消耗”,所以它留在缓冲区中。接下来,您正尝试读取int的值,因此“true”也不匹配。等...直到节目结束。

这就是我该怎么做:

#include <string> 
#include <iostream> 

#include <errno.h> 
#include <stdlib.h> 

using namespace std; 

void askForString(string aPrompt, string &aValue) { 
    cout << aPrompt << " "; 
    cin >> aValue; 
} 

void askForBool(string aPrompt, bool &aValue) { 
    string tString; 
    while (1) { 
     cout << aPrompt << " "; 
     cin >> tString; 
     if (tString == "true") { 
      aValue = true; 
      break; 
     } else if (tString == "false") { 
      aValue = false; 
      break; 
     } else { 
      cout << "Repeat, please?" << endl; 
     } 
    } 
} 

void askForInt(string aPrompt, int &aValue) { 
    string tString; 
    char *endptr; 
    while (1) { 
     cout << aPrompt << " "; 
     cin >> tString; 
     errno = 0; 
     aValue = strtol(tString.c_str(), &endptr, 10); 
     if (errno || tString.c_str() == endptr || (endptr != NULL && *endptr != 0)) { 
      cout << "Repeat, please?" << endl; 
     } else { 
      break; 
     } 
    } 
} 

int main(void) { 
    string strKingdom; 
    bool conquered_me; 
    int gold; 
    int food; 
    int citizens; 
    int soldiers; 

    askForString("Name of kingdom:", strKingdom); 

    askForBool("were you conquered (true/false):", conquered_me); 

    askForInt("How many gold do you have?:", gold); 

    askForInt("How many food do you have?:", food); 

    askForInt("How many citizens do you have?:", citizens); 

    askForInt("How many soldiers do you have?:", soldiers); 

    cout << "Kingdom: " << strKingdom << endl; 
    cout << "Conquered: " << (conquered_me ? "true" : "false") << endl; 
    cout << "Gold: " << gold << endl; 
    cout << "Food: " << food << endl; 
    cout << "Citizens: " << citizens << endl; 
    cout << "Soldiers: " << soldiers << endl; 

    return 0; 
} 
+0

正确,但建议一个解决方案和一些演示解决方案的示例代码将使这个答案有用。 – user4581301

+0

@ user4581301:特别为你改进了我的回答;) – nsilent22

+0

我和未来的提问者谢谢。 – user4581301

2

把他们都转换为字符串,并根据需要转换。

1

您的阅读命令都没有检查错误。所有的这些都应该被写成这样:

while (!(std::cin >> strKingdom)) { 
    std::cerr << 'Bad input' << std::endl; 
    std::cin.clear(); // clear the error 
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore the rest of the line 
    // output the same prompt again? 
} 

为了使这个简单,你可能想要写一个辅助函数:

template<typename T> void get_input(const char *prompt, T &result) { 
    std::cout << prompt << std::endl; 
    while (!(std::cin >> result)) { 
     std::cerr << 'Bad input' << std::endl; 
     std::cin.clear(); 
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
     std::cout << prompt << std::endl; } } 

然后,您可以专注,对于布尔类型的阅读真/假正确...

+0

...或者你可以使用'boolalpha'作为流已经设计来支持(详见我的答案)。 –

1

这条线:

cin >> conquered_me; 

应该是这样的:

cin >> boolalpha >> conquered_me; 

否则,输入需要“0”或“1”。

使用boolalpha您的输入可以是“真”或“假。

2

出于某种原因(可能与旧代码的兼容性)iostreams的默认我在转换true1false0/O。

这只是默认设置 - 有一个名为boolalpha的操纵器,它会将流设置为使用truefalse(或本地等效项)。

所以,这样的代码:

std::cout << 1 == 0;    // produces `0` 
std::cout << boolalpha << 1 == 0; // produces `false` 

这也适用于输入,这样你就可以改变你的代码是这样的:

cin >> boolalpha >> conquered_me; 

...并预期它应该工作(在:它应该接受falsetrue的输入,并从中产生值falsetrue,如果它不是标准库中的错误)。