c++ - 我不知道为什么我的 if else 语句在要求 cin 时终止
问题描述
我刚来这地方。我正在尝试编写一个程序,当您输入两种原色时,它会为您提供一种辅助颜色。我进行了设置,以便如果用户输入两种相同的原色,它会让您重新输入两种不同的原色,但在他们出于某种原因重新输入颜色后,程序终止而不是运行其他 else if 语句。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myColor1, myColor2;
cout << "Enter two primary colors (Ex. red blue): ";
cin >> myColor1 >> myColor2;
if (myColor1 == "purple" || myColor2 == "purple") {
cout << "One or both of the colors you selected are not primary, please select a primary color: ";
cin >> myColor1, myColor2;
}
else if (myColor1 == "orange" || myColor2 == "orange") {
cout << "One or both of the colors you selected are not primary, please select a primary color: ";
cin >> myColor1, myColor2;
}
else if (myColor1 == "green" || myColor2 == "green") {
cout << "One or both of the colors you selected are not primary, please select a primary color: ";
cin >> myColor1, myColor2;
}
else if (myColor1 == "red" && myColor2 == "red") {
cout << "Both the colors you selected are primary. Please select two different primary colors: ";
cin >> myColor1, myColor2;
}
else if (myColor1 == "yellow" && myColor2 == "yellow") {
cout << "Both the colors you selected are primary. Please select two different primary colors: ";
cin >> myColor1, myColor2;
}
else if (myColor1 == "blue" && myColor2 == "blue") {
cout << "Both the colors you selected are primary. Please select two different primary colors: ";
cin >> myColor1, myColor2;
}
else if (((myColor1 == "red") && (myColor2 == "blue")) || ((myColor1 == "blue") && (myColor2 == "red"))) {
cout << "Red and Blue = Purple" << endl;
}
else if (((myColor1 == "red") && (myColor2 == "yellow")) || ((myColor1 == "yellow") && (myColor2 == "red"))) {
cout << "Red and Yellow = Orange" << endl;
}
else if (((myColor1 == "Blue") && (myColor2 == "Yellow")) || ((myColor1 == "Yellow") && (myColor2 == "Blue"))) {
cout << "Blue and Yellow = Green" << endl;
}
return 0;
}
解决方案
我想这可能会对你有所帮助。我建议你应该在这个程序中使用 do-while 循环,顺便说一下,我是一个初学者,希望这里最好的代码是:
#include <iostream>
using namespace std;
int main(){
do
{
string myColor1,myColor2;
cout << "Enter two primary colors (Ex. red blue): ";
cin >> myColor1 >> myColor2;
if (myColor1 == "purple" || myColor2 == "purple")
{
cout << "One or both of the colors you selected are not primary, please select a primary color: ";
}
else if (myColor1 == "orange" || myColor2 == "orange")
{
cout << "One or both of the colors you selected are not primary, please select a primary color: ";
}
else if (myColor1 == "green" || myColor2 == "green")
{
cout << "One or both of the colors you selected are not primary, please select a primary color: ";
}
else if (myColor1 == "red" && myColor2 == "red")
{
cout << "Both the colors you selected are primary. Please select two different primary colors: ";
}
else if (myColor1 == "yellow" && myColor2 == "yellow")
{
cout << "Both the colors you selected are primary. Please select two different primary colors: ";
}
else if (myColor1 == "blue" && myColor2 == "blue")
{
cout << "Both the colors you selected are primary. Please select two different primary colors: ";
}
else if (((myColor1 == "red") && (myColor2 == "blue")) || ((myColor1 == "blue") && (myColor2 == "red")))
{
cout << "Red and Blue = Purple" << endl;
}
else if (((myColor1 == "red") && (myColor2 == "yellow")) || ((myColor1 == "yellow") && (myColor2 == "red")))
{
cout << "Red and Yellow = Orange" << endl;
}
else if (((myColor1 == "Blue") && (myColor2 == "Yellow")) || ((myColor1 == "Yellow") && (myColor2 == "Blue")))
{
cout << "Blue and Yellow = Green" << endl;
}
}while (true);
return 0;
}
推荐阅读
- django - Django - 如何设置用户创建文章是由用户登录
- python - 从子字典导入函数
- c++ - 如何尝试捕获诸如“#include”之类的预处理器指令
- reactjs - Next.js 在没有服务器的情况下生成站点地图
- angular - Highchart - 预期的类型来自属性 'dashStyle',它在类型 'XAxisPlotLinesOptions' 上声明
- database - 为什么 LMDB 编写器不需要读卡槽表的锁?
- angular - 如何在 ngrx Effects 中获得 POST 操作的 Apiresponse
- python-3.x - 如何从多对多关系中的相关模型中提取数据?
- react-native - React Native 向左滑动以打开类似 Instagram 的页面
- javascript - Javascript 将 Json 转换为数组字符串格式