c++:if 语句无法正常运行,并继续执行无效输出
c++ : The if statement not functioning correctly and proceeds to Invalid output
刚开始学习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
语句,找不到问题所在。
从"从可用的原声吉他中选择"输入1
或2
后,它继续进入"无效"。
你在这里读到char
:
cout << "Choose from the available Acoustic Guitars:";
cin >> acoustic;
但是,您稍后将其与一行int
进行比较:
if (acoustic == 1) {
相反,这应该是:
if (acoustic == '1') {
还有这个:
if (pay1 = 6900) {
应该是:
if (pay1 == 6900) {
因为否则无论输入什么,您都会得到不正确的输出,因为pay1 = 6900
将pay1
设置为6900
并返回6900
,这被隐式转换为true
。
还有这一行:
else (pay1 < 6900);
需要更改为
else if (pay1 < 6900)
因为否则(pay1 < 6900)
不是if
语句的条件,而是(pay1 < 6900);
else
情况下发生的情况(它只是计算一个布尔值并丢弃它(,导致始终打印以下"You do not have enough money"
消息。
@Blaze非常感谢你,现在我几乎完成了,现在我在输入是或否后遇到是或否的问题,程序刚刚结束,我不知道它有什么问题>.<</p>
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";
}
}
}
相关文章:
- C++ 在信号处理程序后继续执行
- 超时后,线程睡眠不会继续执行
- 在构造函数中删除后继续执行
- c++:if 语句无法正常运行,并继续执行无效输出
- 英特尔 Pin:RECEIVED_ACCESS_FAULT异常后正确继续执行
- 如果发生错误,则返回错误值,否则,继续执行函数
- 睡眠线程或进程的指令是否在线程/进程休眠时继续执行?
- 使用 UWP 中的 IAsyncOperation 继续执行任务
- 是什么导致程序在我继续执行另一个功能之前停止
- 使用 ASSERT_DEATH 时,有没有办法在应用程序终止后自动继续执行所有测试
- 现代C++.从继续执行的工作线程返回数据结构
- 在击中断点并继续执行VS时,如何在运行时自动修改变量的值
- 在调用GNUPLOT之后,如何继续执行C 代码
- OpenMP 构造,在至少 1 个线程完成后立即继续执行
- 如果我已经完成了当前功能,我该如何选择继续执行另一个功能
- 循环浏览文件夹中的所有文件以继续执行脚本
- 在程序继续执行的同时,如何使代码的两部分之间存在时间间隔或延迟?C++
- C++if-else语句继续执行if,而不是else下的代码
- 继续执行另一个线程
- pthread_join()是否允许在调用线程上继续执行