【发布时间】:2016-03-07 10:33:40
【问题描述】:
我编写了一个模拟钓鱼游戏的程序。该程序在一个运行循环中,只要用户愿意,就可以进行迭代。问题是输入的任何内容,除了 1 或 0,都会破坏我的代码。我已经尝试了几个小时不同的事情,我需要一些帮助!我的主要 cpp 文件代码包括在内。如果您的审核也需要我的头文件,请告诉我。
有人要求我编辑代码。问题是输入时除整数以外的任何内容都会破坏我的代码。我不想要这个。我希望程序捕获输入并继续循环,直到用户输入正确的输入(1 或 0)或退出游戏。
Game game; // Game object game declared
cout<<"Welcome to Go Fish 2.0!"<<endl; // welcome message
cout<<"Would you like to play?"<<endl; // Prompts user to play
cout<<"1 to play, 0 to exit:"<<endl; // Provides user with input choices
cin>>choice; // user enters game play choice
if (choice == 1) // while user chooses to play game
play = true; // play boolean is set to true
else if (choice == 0) // while user chooses to end game
play = false; // play boolean is false
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
cin.clear();
cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input
if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}
total=0; // variable total is initialized to 0
while (play == true) { // while play boolean is set to true; user wants to play game
total1 += game.playgame(total); // total1 variable keeps a running total of the game
//game.playgame(total) uses the game object to call the playgame function that passes the variable total
// when game.playgame(total) is called this funciton essentially mobilizes game play of die rolling and point accumulation
//each time this function is called (as many times as user wants to play game), the running total is incremented
cout<< "Do you want to play again? (0 for no, 1 for yes)"<<endl;// asks user if they want to play again
cin >> choice;// user enters choice
if (choice == 1) // if user enters 1
{
play = true; // play is assigned to true
}
else if (choice == 0) // if user enters 0
{
play = false; // play is assigned to false
}
/*
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
cin.clear();
cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input
if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}
if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
cout<<"My Total game points are "<<total1<<endl; // displays user's total points from whole game
if (total1>100) // if game total greater than 100
cout<<"Awesome! You're a great fisher!"<<endl; // congratulate user
if (total1<100)// if game total is less than 100
cout<<"Good job, but play again so you can fish some more."<<endl;// chastise user
system("pause");
}
}
return 0;// end main function
}
}
【问题讨论】:
-
请多多关照 - 格式化您的代码...
-
请看
std::uniform_int_distribution,而不是rand。它将为您提供更好的随机性并预先加载所有使用困难。 (或者用英语,更难设置,但它就是一劳永逸) -
上次编辑让事情变得更糟。不要再乱用这段代码,写一个简单的小程序,除了从用户那里读取 1 或 0 之外什么都不做。玩一会儿。如果你不能让它发挥作用,你可以提出一个重点突出、完整的问题。
-
请使用类似这样的方式来格式化代码:- prettyprinter.de 。很多人很难遵循代码。如果您正确缩进代码,您将获得更好的响应。
标签: c++ validation loops input