首页 > 解决方案 > 我不知道为什么我的 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;
}

标签: c++

解决方案


我想这可能会对你有所帮助。我建议你应该在这个程序中使用 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;

}


推荐阅读