我的代码就像一个收银机,输入产品代码并对其进行计费并打印收据。当我执行我的程序时,收据上似乎没有显示价格,也没有加起来。是不是因为我的 priceA、priceB 等以及价格的计算在 if 语句中?如果是这样,我如何在 if 语句内部使用变量值?
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main ()
{
using namespace std;
cout << "Welcome to SM Supermarket!\n";
int code = 0;
string product[10];
product[0] = "Hagen Daaz";
product[1] = "Bounty Tissue";
product[2] = "Chips Ahoy Cookies";
product[3] = "Tide Laundry Detergent";
product[4] = "Lays Original Chips";
product[5] = "Sunny D Orange Juice";
product[6] = "Organic Bananas";
product[7] = "Oreo Cookies";
product[8] = "Red Delicious Apples";
product[9] = "Extra Cheesy Kraft Dinner";
int priceA = 0, priceB = 0, priceC = 0, priceD = 0, priceE = 0, priceF = 0, priceG = 0, priceH = 0, priceI = 0, priceJ = 0;
// while (code != 000){
cout << "Please enter the barcode of your item\n";
cin >> code;
int amount = 0;
cout << "How many do you want to purchase?\n";
cin >> amount;
if (code == 0123){
priceA = 7 * amount;
printf("%s\n",product[0].c_str());
}
else if (code == 0124){
priceB = 10 * amount;
printf("%s\n",product[1].c_str());
}
else if (code == 0125){
priceC = 3 * amount;
printf("%s\n",product[2].c_str());
}
else if (code == 0126){
priceD = 6 * amount;
printf("%s\n",product[3].c_str());
}
else if (code == 0127){
priceE = 2 * amount;
printf("%s\n",product[4].c_str());
}
else if (code == 0133){
priceF = 3 * amount;
printf("%s\n",product[5].c_str());
}
else if (code == 0134){
priceG = 5 * amount;
printf("%s\n",product[6].c_str());
}
else if (code == 0130){
priceH = 2 * amount;
printf("%s\n",product[7].c_str());
}
else if (code == 0131){
priceI = 3 * amount;
printf("%s\n",product[8].c_str());
}
else if (code == 0132){
priceJ = 4 * amount;
printf("%s\n",product[9].c_str());
}
//}
int coupon = 0;
//while (coupon != 000){
cout << "Please enter your coupon code, if you do not have any, please press '0'. Press '000' when finished entering coupon(s)\n";
cin >> coupon;
if (coupon == 051){
cout << "50% discount of 'Bounty Tissue'\n";
priceB = priceB / 2;
}
else if (coupon == 017){
cout << "$2 off any Hagen Daaz product\n";
priceA = priceA - 2;
}
else if (coupon == 046){
cout << "$1 off two Lays original Chips\n";
priceE = priceE * 2 - 1;
}
else if (coupon == 035){
cout << "$1 off any Sunny D product\n";
priceF = priceF - 1;
}
else if (coupon == 0){
cout << "Your purchase is being processed\n";
}
//}
int bags = 0;
cout << "How many bags would you like to purchase(1 bag/$1 each)?\n";
cin >> bags;
bags = bags * 1;
int subtotal = priceA + priceB + priceC + priceD + priceE + priceF + priceG + priceH + priceI + priceJ + bags;
cout << "Subtotal:\n\n " << subtotal;
double total = subtotal * 1.1;
cout << "Total:\n\n " << total;
int payment = 0;
int pin = 0;
double change = 0;
double cash = 0;
cout << "Would you like to pay with debit or cash? Press '1' for debit and '2' for cash\n";
cin >> payment;
if (payment == 1){
cout << "Enter your pin number: ";
cin >> pin;
cout << "Approved. Your purchase is processing...\n";
}
else if (payment == 2){
cout << "Please enter the amount you are paying with:\n";
cin >> cash;
change = cash - total;
}
cout << "Thank you for your purchase. Your reciept is printing...\n";
cout << "<<<<<<<<<<<<<<<<<<<<<SM Supermarket>>>>>>>>>>>>>>>>>>>>>\n";
cout << " Cashier: Samantha & Madeleine\n";
cout << "\n";
cout << "\n";
cout << "--------------------------------------------------------\n";
printf("%s\n",product[0].c_str());
cout << "$" << priceA << "\n";
printf("%s\n",product[1].c_str());
cout << "$" << priceB << "\n";
printf("%s\n",product[2].c_str());
cout << "$" << priceC << "\n";
printf("%s\n",product[3].c_str());
cout << "$" << priceD << "\n";
printf("%s\n",product[4].c_str());
cout << "$" << priceE << "\n";
printf("%s\n",product[5].c_str());
cout << "$" << priceF << "\n";
printf("%s\n",product[6].c_str());
cout << "$" << priceG << "\n";
printf("%s\n",product[7].c_str());
cout << "$" << priceH << "\n" ;
printf("%s\n",product[8].c_str());
cout << "$" << priceI << "\n";
printf("%s\n",product[9].c_str());
cout << "$" << priceJ << "\n";
cout << "\n";
cout << "Bags";
cout << "$" << bags;
cout << "\n";
cout << "-----------------";
cout << "Subtotal: " << subtotal << "\n";
cout << "Total: " << total << "\n";
cout << "Amount Given: " << cash << "\n";
cout << "Change: " << change << "\n";
cout << "-----------------";
cout << "Thanks for shopping at SM Supermarket!";
cout << "***************************************";
cout << "Please press '1' to exit. Have a nice day!";
int exit = 0;
cin >> exit;
}