gpt4 book ai didi

C++ : The if statement not functioning correctly and proceeds to Invalid output

转载 作者:行者123 更新时间:2023-11-30 04:43:35 25 4
gpt4 key购买 nike

刚开始学习 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;
}
}
}

我已经检查了我的 if else 语句,但找不到问题所在。

从“从可用的原声吉他中选择”输入 12 后,它一直继续“无效”。

最佳答案

你读了一个char这里:

cout << "Choose from the available Acoustic Guitars:";
cin >> acoustic;

但随后您将其与 int 进行比较一行之后:

if (acoustic == 1) {

相反,这应该是:

if (acoustic == '1') {

还有这个:

if (pay1 = 6900) {

应该是:

if (pay1 == 6900) {

否则,无论输入什么,您都会得到不正确的输出,因为 pay1 = 6900pay16900并返回 6900 , 隐式转换为 true .

还有这一行:

else (pay1 < 6900);

需要改成

else if (pay1 < 6900)

因为否则(pay1 < 6900)不是if的条件声明,而是 (pay1 < 6900); else 中发生了什么case(只计算一个 bool 值并丢弃它),导致以下 "You do not have enough money"要始终打印的消息。

关于C++ : The if statement not functioning correctly and proceeds to Invalid output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58181280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com