c++;:if语句运行不正常,并继续执行无效输出 刚刚开始学习C++并尝试建立一个自动排序系统,但是在继续运行之前,我尝试着运行这个问题: #include <iostream> using namespace std; char acoustic, fender, hartwood, electric, gibson, ibanez, drums, pearl, roland, piano, casio; char rolandpi, equip, string, headphone, amp, mixer, micro, tuner, pick, music, Yes, No; int pay1 = 0, pay2 = 0; int main() { cout << "Welcome to the Music Shop" << endl <<endl; cout << "a. Acoustic Guitar" << endl; cout << "a.1 Fender Acoustic Guitar - P6,900.00" << endl; cout << "a.2 Hartwood Acoustic Guitar - P6,300.00" << endl <<endl; cout << "b. Electric Guitar" << endl; cout << "b.1 Gibson Electric Guitar -P8,500.00" << endl; cout << "b.2 Ibanez Electric Guitar -P25,000.00" << endl <<endl; cout << "c. Drums" << endl; cout << "c.1 Pearl Drum Kits -P27,000.00" << endl; cout << "c.2 Roland Electronic Drums -P24,000.00" << endl <<endl; cout << "d. Piano" << endl; cout << "d.1 Casio Digital Piano -P19,000.00" << endl; cout << "d.2 Roland Piano -P120,000.00" << endl <<endl; cout << "e. Music Equipments" << endl; cout << "e.1 Guitar String -P113.00" << endl; cout << "e.2 Headphones -P1,600.00" << endl; cout << "e.3 Amplifier -P2,800.00" << endl; cout << "e.4 Digital Mixer -P4,750.00" << endl; cout << "e.4 Vocal Microphone -P860.00" << endl; cout << "e.5 Guitar Tuner -P537.00" << endl; cout << "e.6 Guitar Pick -P360.00" << endl <<endl; cout << "Choose the music instrument or equipment you want to buy: "; cin >> music; switch (music) { case 'a': cout<< "Acoustic Guitar" << endl; cout<< "1. Fender Acoustic Guitar - P6,900.00" << endl; cout<< "2. Hartwood Acoustic Guitar - P6,300.00" << endl <<endl; cout<<"Choose from the available Acoustic Guitars:"; cin>>acoustic; if (acoustic == 1){ cout<<"Enter your payment:"; cin>>pay1; if (pay1=6900){ cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl; } else if (pay1>6900){ pay1 -= 6900; cout<<"Your change is:"<<pay1<<endl; } else (pay1<6900); { cout<<"You do not have enough money"<<endl; } } else if (acoustic == 2){ cout<<"Enter your payment:"; cin>>pay1; if (pay1=6300){ cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl; } else if (pay1>6300){ pay1 -= 6300; cout<<"Your change is:"<<pay1<<endl; } else (pay1<6300); { cout<<"You do not have enough money"<<endl; } } else { cout<<"Invalid"<<endl; } } } #包括 使用名称空间std; char acoustic,fender,hartwood,electric,gibson,ibanez,鼓,pearl, 罗兰,钢琴,卡西欧; char rolandpi、设备、字符串、耳机、放大器、混音器、微型、调谐器、拾取、, 音乐,是的,不是; int pay1=0,pay2=0; int main() { 你能在这里读一个字符吗 cout << "Choose from the available Acoustic Guitars:"; cin >> acoustic;

c++;:if语句运行不正常,并继续执行无效输出 刚刚开始学习C++并尝试建立一个自动排序系统,但是在继续运行之前,我尝试着运行这个问题: #include <iostream> using namespace std; char acoustic, fender, hartwood, electric, gibson, ibanez, drums, pearl, roland, piano, casio; char rolandpi, equip, string, headphone, amp, mixer, micro, tuner, pick, music, Yes, No; int pay1 = 0, pay2 = 0; int main() { cout << "Welcome to the Music Shop" << endl <<endl; cout << "a. Acoustic Guitar" << endl; cout << "a.1 Fender Acoustic Guitar - P6,900.00" << endl; cout << "a.2 Hartwood Acoustic Guitar - P6,300.00" << endl <<endl; cout << "b. Electric Guitar" << endl; cout << "b.1 Gibson Electric Guitar -P8,500.00" << endl; cout << "b.2 Ibanez Electric Guitar -P25,000.00" << endl <<endl; cout << "c. Drums" << endl; cout << "c.1 Pearl Drum Kits -P27,000.00" << endl; cout << "c.2 Roland Electronic Drums -P24,000.00" << endl <<endl; cout << "d. Piano" << endl; cout << "d.1 Casio Digital Piano -P19,000.00" << endl; cout << "d.2 Roland Piano -P120,000.00" << endl <<endl; cout << "e. Music Equipments" << endl; cout << "e.1 Guitar String -P113.00" << endl; cout << "e.2 Headphones -P1,600.00" << endl; cout << "e.3 Amplifier -P2,800.00" << endl; cout << "e.4 Digital Mixer -P4,750.00" << endl; cout << "e.4 Vocal Microphone -P860.00" << endl; cout << "e.5 Guitar Tuner -P537.00" << endl; cout << "e.6 Guitar Pick -P360.00" << endl <<endl; cout << "Choose the music instrument or equipment you want to buy: "; cin >> music; switch (music) { case 'a': cout<< "Acoustic Guitar" << endl; cout<< "1. Fender Acoustic Guitar - P6,900.00" << endl; cout<< "2. Hartwood Acoustic Guitar - P6,300.00" << endl <<endl; cout<<"Choose from the available Acoustic Guitars:"; cin>>acoustic; if (acoustic == 1){ cout<<"Enter your payment:"; cin>>pay1; if (pay1=6900){ cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl; } else if (pay1>6900){ pay1 -= 6900; cout<<"Your change is:"<<pay1<<endl; } else (pay1<6900); { cout<<"You do not have enough money"<<endl; } } else if (acoustic == 2){ cout<<"Enter your payment:"; cin>>pay1; if (pay1=6300){ cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl; } else if (pay1>6300){ pay1 -= 6300; cout<<"Your change is:"<<pay1<<endl; } else (pay1<6300); { cout<<"You do not have enough money"<<endl; } } else { cout<<"Invalid"<<endl; } } } #包括 使用名称空间std; char acoustic,fender,hartwood,electric,gibson,ibanez,鼓,pearl, 罗兰,钢琴,卡西欧; char rolandpi、设备、字符串、耳机、放大器、混音器、微型、调谐器、拾取、, 音乐,是的,不是; int pay1=0,pay2=0; int main() { 你能在这里读一个字符吗 cout << "Choose from the available Acoustic Guitars:"; cin >> acoustic;,c++,if-statement,char,comparison-operators,C++,If Statement,Char,Comparison Operators,相反,这应该是: if (acoustic == '1') { 还包括: if (pay1 = 6900) { 应该是: if (pay1 == 6900) { 因为否则,无论输入什么,您都将得到不正确的输出,因为pay1=6900将pay1设置为6900,并返回6900,该值隐式转换为true 此外,这一行: else (pay1 < 6900); else(工资1

相反,这应该是:

if (acoustic == '1') {
还包括:

if (pay1 = 6900) {
应该是:

if (pay1 == 6900) {
因为否则,无论输入什么,您都将得到不正确的输出,因为
pay1=6900
pay1
设置为
6900
,并返回
6900
,该值隐式转换为
true

此外,这一行:

else (pay1 < 6900);
else(工资1<6900);
需要改成

else if (pay1 < 6900)
else if(付款1<6900)

因为否则
(pay1<6900)
不是
if
语句的条件,而是
(pay1<6900);
是在
else
情况下发生的事情(只计算布尔值并丢弃它),导致下面的
“您没有足够的钱”
信息要一直打印。

@Blaze非常感谢您现在我几乎完成了,现在输入Yes后我遇到了Yes或No的问题,程序刚刚结束,我不知道有什么问题>。谢谢您,我还注意到我在else之后放置了(pay1@Student是的,我监督了这一条。我编辑了我的帖子,并插入了这条
else
行应该是什么样子的,以及它当前运行不正常的原因。谢谢你,如果你不介意你还有其他问题,我会在下面补充一个问题,你应该单独发布,而不是作为你上一个问题的答案。总之,这是一个问题即使使用以前的代码(没有
answer
的声明),IPET也不会编译。您可以尝试
std::string answer;
,然后将
if(answer==Yes){
更改为
if(answer==Yes){
否则如果(no){在这种上下文中没有意义,则可能是
else if(answer==no)){
后跟一个
else
以捕获所有其他情况。
else if (pay1 < 6900)
cout<<"\nWould you like to buy an Electric Guitar?"<<endl;
    cout<<"Yes or No?"<<endl;
    cin>>answer;

    if (answer == Yes){
    cout<< "Electric Guitar" << endl;
    cout<< "1. Gibson Electric Guitar   -P8,500.00" << endl;
    cout<< "2. Ibanez Electric Guitar   -P25,000.00" << endl <<endl;
    cout<<"Choose from the available Electric Guitars:";
    cin>>electric;

        if (electric=='1'){
        cout<<"Enter your payment:";
        cin>>pay2;
        if (pay2==8500){
            cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl;
        }
            else if (pay2>8500){
                pay2 -= 8500;
                cout<<"Your change is:"<<pay2<<endl;
            }
                else if(pay2<8500)
                {
                    cout<<"You do not have enough money"<<endl;
                }   
    }
    else if (electric=='2'){
        cout<<"Enter your payment:";
        cin>>pay2;
        if (pay2==25000)
        {
            cout<<"You have succesfully purchased Fender Acoustic Guitar"<<endl;
        }
            else if (pay2>25000){
                pay2 -= 25000;
                cout<<"Your change is:"<<pay2<<endl;
            }
                else if (pay2<25000)
                {
                    cout<<"Invalid"<<endl;
                }   
    }
    else {
        cout<<"Invalid";
    }
} 
    else if (No){
        cout<<"Thank you for checking our other products";
}
}
}