C++ 循环,用于为总值添加数字
我的家庭作业有问题。我在程序上的一切都很正常,但for循环增加了升级的价值 任务如下: 在用户选择了一辆车后,进行了一些发动机升级。这些升级最多可以从0(无升级)升级到5。你也需要一个漂亮的环 升级费用为每级100美元,加上较低的升级费用 示例:1级升级为100美元 示例:2级升级为$100+$200=$300 示例:3级升级为$100+$200+$300=$600 等等等等等等等等等等等等 这是我目前掌握的代码:C++ 循环,用于为总值添加数字,c++,loops,for-loop,C++,Loops,For Loop,我的家庭作业有问题。我在程序上的一切都很正常,但for循环增加了升级的价值 任务如下: 在用户选择了一辆车后,进行了一些发动机升级。这些升级最多可以从0(无升级)升级到5。你也需要一个漂亮的环 升级费用为每级100美元,加上较低的升级费用 示例:1级升级为100美元 示例:2级升级为$100+$200=$300 示例:3级升级为$100+$200+$300=$600 等等等等等等等等等等等等 这是我目前掌握的代码: #include <iostream> #include <i
#include <iostream>
#include <iomanip>
int main () {
int quoteNum = 0;
int carChoice = 0;
double carCost = 0;
double upgradeCost = 0;
int upgradeChoice = 0;
int engineLevel = 0;
double totalCost = 0;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
do {
std::cout << "Welcome to Bob's used car lot and chop shop!\n";
std::cout << "As you can see we have quite a few to choose from!\n";
std::cout << "Which one would you like?: \n";
std::cout << "[1] 2005 Volkswagen Beetle ($8,000)\n";
std::cout << "[2] 2008 Pontiac G6 ($8,581)\n";
std::cout << "[3] 2004 Chevy S-10 ($10,500)\n";
std::cout << "[4] 2016 Jeep Patriot ($15,209)\n";
std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)\n";
std::cin >> carChoice;
} while (carChoice <= 0 || carChoice > 5);
switch (carChoice) {
case 1:
carCost = 8000.00;
break;
case 2:
carCost = 8581.00;
break;
case 3:
carCost = 10500.00;
break;
case 4:
carCost = 15209.00;
break;
case 5:
carCost = 24390.00;
break;
}
do {
std::cout << "Quote:{" << quoteNum++ << "} ";
std::cout << " Car($" << carCost << ")";
std::cout << " E(" << engineLevel << ")";
std::cout << " Upgrades($" << upgradeCost << ")" << std::endl;
std::cout << "Do you want to upgrade your car?\n";
std::cout << "[-/+1] Downgrade / Upgrade Engine\n";
std::cout << "[ 2] Clear all upgrades\n";
std::cout << "[ 3] Reset car\n";
std::cout << "[ 4] Buy Car!!!\n";
std::cout << "What would you like to do?: \n";
std::cin >> upgradeChoice;
if (upgradeChoice == -1) {
engineLevel--;
upgradeCost--;
}
if (upgradeChoice == 1) {
for (upgradeCost = 0; upgradeCost <= 5; upgradeCost++) {
upgradeCost = 100.00;
}
}
if (upgradeChoice == 2) {
engineLevel = 0;
upgradeCost = 0.00;
}
if (upgradeChoice == 3) {
std::cout << "Welcome to Bob's used car lot and chop shop!\n";
std::cout << "As you can see we have quite a few to choose "
"from!\n";
std::cout << "Which one would you like?: \n";
std::cout << "[1] 2005 Volkswagen Beetle ($8,000)\n";
std::cout << "[2] 2008 Pontiac G6 ($8,581)\n";
std::cout << "[3] 2004 Chevy S-10 ($10,500)\n";
std::cout << "[4] 2016 Jeep Patriot ($15,209)\n";
std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)\n";
std::cin >> carChoice;
std::cin.ignore();
switch (carChoice) {
case 1:
carCost = 8000.00;
break;
case 2:
carCost = 8581.00;
break;
case 3:
carCost = 10500.00;
break;
case 4:
carCost = 15209.00;
break;
case 5:
carCost = 24390.00;
break;
}
}
if (upgradeChoice == 4) {
totalCost = carCost + upgradeCost;
std::cout << "Bill: Car($" << carCost << ")";
std::cout << " Upgrades($" << upgradeCost << ")";
std::cout << " Total ($" << totalCost << ")\n";
std::cout << "Press ENTER to continue";
std::cin.ignore();
std::cin.get();
return 0;
}
} while (upgradeChoice >= -1 || upgradeChoice <= 4 && upgradeChoice != 0);
}
#包括
#包括
int main(){
int quoteNum=0;
int carChoice=0;
双倍成本=0;
双重升级成本=0;
int upgradeChoice=0;
int engineLevel=0;
双重总成本=0;
std::cout我用课堂解决了这个问题,我建议你开始学习面向对象编程,因为它是编程的一个非常重要的部分,即使你在学校没有开始学习它。它使你的开发方式更容易看穿
对于这个项目,我使用了带有GNUC/C++编译器的C++11
虽然它可能包含错误或常规错误,但很抱歉
包含的标题:
#include <iostream>
#include <string>
#include <vector>
#include <sstream> // std::stringstream
using USHORT = unsigned short;
template <typename T>
std::string convert_string(const T value) {
std::stringstream stream;
stream << value;
return stream.str();
}
// note: use std::to_string instead of convert_string, if your compiler supports it
class Car {
USHORT year, engine_level;
std::string name;
float price, upgrade_cost;
public:
Car(USHORT year, std::string name, float price) {
this->year = year;
this->name = name;
this->price = price;
engine_level = 1;
upgrade_cost = 0.0;
}
std::string to_string() { return convert_string(year) + " " + name + " " + " ($" + convert_string(price) + ")"; }
friend std::ostream& operator<<(std::ostream& os, Car car) {
os << car.to_string() << std::endl;
return os;
}
std::string get_engine_level() { return "E(" + convert_string(engine_level) + ")"; }
std::string get_price() { return "Car($" + convert_string(price) + ")"; }
std::string get_upgrade_cost() { return "Upgrades($" + convert_string(upgrade_cost) + ")"; }
void downgrade() {
if(engine_level > 1) {
engine_level--;
upgrade_cost -= (100 + (engine_level * 100));
} else {
std::cout << "You can't downgrade your car from level 1!" << std::endl;
}
}
void upgrade() {
engine_level++;
upgrade_cost += (100 + (engine_level * 100));
}
void clear_upgrades() { engine_level = 1; upgrade_cost = 0.0; }
std::string get_total_cost() { return "Total ($" + convert_string(price + upgrade_cost) + ")"; }
};
class CarDealerAndWorkshop {
std::vector<Car> cars_for_sale;
public:
CarDealerAndWorkshop(std::vector<Car> cars_for_sale) { this->cars_for_sale = cars_for_sale; }
void welcome_message() {
std::cout <<
"Welcome to Bob's used car lot and chop shop!\n"
"As you can see we have quite a few to choose from!\n"
"Which one would you like?: ";
}
void upgrade_message() {
std::cout <<
"Do you want to upgrade your car?\n"
"[-/+1] Downgrade / Upgrade Engine\n"
"[ 2] Clear all upgrades\n"
"[ 3] Reset car\n"
"[ 4] Buy Car!!!\n"
"What would you like to do?: ";
}
void show_cars() {
int counter = 1;
for (auto car : cars_for_sale) {
std::cout << "[" << counter++ << "] ";
std::cout << car;
}
}
unsigned number_of_cars() { return cars_for_sale.size(); }
bool validate(const USHORT number) { return number <= number_of_cars() && number >= 0; }
Car* get_car_by_num(const USHORT num) { return &cars_for_sale[num]; }
void buy(const int number) { cars_for_sale.erase(cars_for_sale.begin() + number); }
bool are_there_cars() {
if(number_of_cars() == 0) {
std::cout << "We are very sorry, but we have ran out of cars!\n";
return false;
}
return true;
}
};
class Customer {
Car* car_choice;
std::string name;
public:
Customer(std::string name) { this->name = name; }
void set_car_choice(Car* car) { car_choice = car; }
Car* get_car_choice() { return car_choice; }
};
最后是客户端代码:
int main () {
CarDealerAndWorkshop Bobs_Dealership({
{2005, "Volkswagen Beetle", 8000},
{2008, "Pontiac G6", 8581},
{2004, "Chevy S-10", 10500},
{2016, "Jeep Patriot", 15209},
{2012, "Jeep Wrangler Sport", 24390}
});
Customer customer("Bill");
Bobs_Dealership.welcome_message();
bool is_quit_dealer = false;
while(!is_quit_dealer) {
if(!Bobs_Dealership.are_there_cars())
is_quit_dealer = true;
USHORT car_choice;
do {
Bobs_Dealership.show_cars();
std::cin >> car_choice;
} while (!Bobs_Dealership.validate(car_choice-1));
customer.set_car_choice(Bobs_Dealership.get_car_by_num(car_choice-1));
unsigned quote_num = 0;
bool is_quit_workshop = false;
short upgrade_choice;
while(!is_quit_workshop) {
std::cout << "Quote:{" << quote_num++ << "} " << std::endl;
std::cout << customer.get_car_choice()->get_price() << std::endl;
std::cout << customer.get_car_choice()->get_engine_level() << std::endl;
std::cout << customer.get_car_choice()->get_upgrade_cost() << std::endl;
Bobs_Dealership.upgrade_message();
std::cin >> upgrade_choice;
switch(upgrade_choice) {
case -1: customer.get_car_choice()->downgrade(); break;
case 1: customer.get_car_choice()->upgrade(); break;
case 2: customer.get_car_choice()->clear_upgrades(); break;
case 3: is_quit_workshop = true; break;
case 4:
std::cout << customer.get_car_choice()->get_price() << std::endl;
std::cout << customer.get_car_choice()->get_upgrade_cost() << std::endl;
std::cout << customer.get_car_choice()->get_total_cost() << std::endl;
std::cout << "Press ENTER to continue" << std::endl;
std::cin.sync();
std::cin.get();
Bobs_Dealership.buy(car_choice-1);
is_quit_workshop = true;
}
}
}
return 0;
}
int main(){
CardeAlandWorkshop Bobs_经销商({
{2005,“大众甲壳虫”,8000},
{2008,“庞蒂亚克G6”,8581},
{2004,“雪佛兰S-10”,10500},
{2016,“吉普爱国者”,15209},
{2012,“吉普牧马人运动”,24390}
});
客户(“账单”);
Bobs_经销商。欢迎留言();
bool为退出经销商=错误;
而(!正在退出经销商){
如果(!Bobs_经销商。有车吗())
是否退出经销商=真;
乌什特汽车;
做{
Bobs_经销商。展示汽车();
标准::cin>>汽车选择;
}而(!Bobs_defactory.validate(car_choice-1));
客户。设置车辆选择(Bobs经销商。按数量获取车辆(车辆选择-1));
无符号引号_num=0;
bool为“退出车间”=假;
短时间的升级选择;
而(!正在退出车间){
std::cout这太宽泛了。你有什么特别的问题?你需要帮助的循环是什么?@Carcigenicate我需要帮助升级选择和englineLevel循环。我需要程序在用户输入1到5次时增加100的增量,在用户输入-1时删除更改。@Hansel你们有吗e有什么建议吗?--是的,调试你自己的代码。编程不是写一个程序,看到它不起作用,就发布它,这样它就不起作用了,然后坐下来等待答案。调试你自己的代码是学习如何写程序的重要组成部分。upgradeCost=100.00;
-没有增量这里-您总是只分配upgradeCost
相同的值。此外,您对循环和分配使用相同的变量,这在默认情况下会中断code@Hansel--那么,下次代码中出现错误时,您还会再来吗?现在是学习如何调试代码的时候,因为不这样做是一种不可抗拒的行为优秀的程序员。没有人能在第一次编写出完美的程序,而调试自己的代码又是,一旦你进入编程世界,你必须学会做的事情之一。良好的开端。如果可能的话,我想给你一些提示。正如你提到的C++11,你可以使用而不是typedef
,而不是你rconvert\u string
。开始使用并首选'\n'
到std::endl
,除非您真的需要刷新输出。@Bob\uuuu。感谢Bob的建议!是的,我想使用std::to\u string
,但我的MinGW不支持它,我需要很快更新它。