c++ - Cin isn't waiting for input -


this program:

#include <iostream> #include <string> #include <stdlib.h>   using namespace std;  int main(){   string strkingdom = ""; bool conquered_me;//see if conquered, going use on other program , true = game over. int gold; int food; int citizens; int soldiers;    cout << endl <<"name of kingdom: "; cin >> strkingdom; cout << endl << "were conquered (true/false): "; cin >> conquered_me; cout << endl << "how many gold have?:"; cin>>gold; cout << endl << "how many food have?:"; cin >> food; cout << endl << "how many citizens have?:"; cin >> citizens; cout << endl << "how many soldiers have?:"; cin >> soldiers;   return 0; } 

the problem when compile progam lets me insert first 2 variables , shows rest of questions (after compile):

name of kingdom: steve

were conquered (true/false): false

how many gold have?: how many food have?: how many citizens have?: how many soldiers have?:

you need employ std::getline , std::string read various values. (you can use functions atoi parse them.) here code sample using std::getline function.

#include <iostream> #include <string> #include <cstdlib>  int main(){   std::string strkingdom = ""; bool conquered_me;//see if conquered, going use on other program , true = game over. int gold; int food; int citizens; int soldiers;  std::string tstring = ""; // used read , subsequently parse string.   std::cout << std::endl <<"name of kingdom: "; std::getline(std::cin,strkingdom);  std::cout << std::endl << "were conquered (true/false): "; std::getline(std::cin,tstring); conquered_me = (tstring == "true");  std::cout << std::endl << "how many gold have?:"; std::getline(std::cin,tstring); gold = std::atoi(tstring.c_str());  std::cout << std::endl << "how many food have?:"; std::getline(std::cin,tstring); food = std::atoi(tstring.c_str());  std::cout << std::endl << "how many citizens have?:"; std::getline(std::cin,tstring); citizens = std::atoi(tstring.c_str());  std::cout << std::endl << "how many soldiers have?:"; std::getline(std::cin,tstring); soldiers = std::atoi(tstring.c_str());   return 0; } 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -