变量类型的问题
issue with variable types
我应该制作一个向用户询问一些基本问题的运输程序。
以下是说明:在线购物者在网络上搜索商品并从税率最低的州购买。编写一个简单的 C++ 程序来执行以下操作:向用户询问商品的单价、购物者要购买的商品数量、第一个州的名称、第一个州的税率、商品的名称第二州和第二州的税率。然后,计算每个州的总成本并决定购物者应该从哪个州购买商品。向购物者显示以下信息:单位商品成本、购买的商品数量、第一州的名称和税率、第二州的名称和税率,以及您对购物者应该从哪个州购买的建议.
列出程序中的所有文字和变量。
到目前为止,这是我的代码:
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
double itemPrice, numberItem, taxRate, totalCost;
double stateTax1, stateTax2;
int stateName1, stateName2;
// ask the item's price
cout << "What is the price of the item?\n";
cin >> itemPrice;
// ask the # items
cout << "How many items are you going to purchase?\n";
cin >> numberItem;
// ask the name of the first state
cout << "What state are you having it shipped to?\n";
cin >> stateName1;
// ask the tax rate of the first state
cout << "What is the tax rate in " << stateName1 << "?\n";
cin >> stateTax1;
// ask the name of the second state
cout << "What state are you having shipped from?\n";
cin >> stateName2;
// ask the tax rate of the second state
cout << "What is the tax rate in " << stateName2 << "?\n";
cin >> stateTax2;
// first if the first state has a lower tax rate
// else if second state has lower tax rate
if (stateTax1 < stateTax2)
{
totalCost = itemPrice * numberItem * stateTax1;
cout << "The item cost is " << itemPrice << ".\n";
cout << "The number of items is " << numberItem << ".\n";
cout << "The name of the first state is " << stateName1 << ".\n";
cout << "The tax rate of the first state is " << stateTax1 << ".\n";
cout << "The name of the second state is " << stateName2 << ".\n";
cout << "The tax rate of the second state is " << stateTax2 << ".\n";
cout << "You should consider purchasing the item from " << stateName1 << ".\n";
}
else
{
totalCost = itemPrice * numberItem * stateTax2;
cout << "The item cost is " << itemPrice << ".\n";
cout << "The number of items is " << numberItem << ".\n";
cout << "The name of the first state is " << stateName1 << ".\n";
cout << "The tax rate of the first state is " << stateTax1 << ".\n";
cout << "The name of the second state is " << stateName2 << ".\n";
cout << "The tax rate of the second state is " << stateTax2 << ".\n";
cout << "You should consider purchasing the item from " << stateName2 << ".\n";
}
return 0;
}
我的问题是如何让 stateName 变量正常工作。我确信这是我应该知道但我不知道的一些基本字符串。除此之外,我相信我的其余代码都可以正常工作。尽管任何和所有提示都将不胜感激。
"Display the following to shopper: The unit item cost, the number of items purchased, the name and tax rate of the first state, the name and tax rate of the second state, and your recommendation on from which state the shopper should make the purchase."
你代码中的变量定义
int stateName1, stateName2;
// ^^^
将只接受输入语句的整数数值,如
cin >> stateName1;
每当您输入类似
的内容时
Oklahoma
从输入提示中,std::cin
将被设置为fail()
状态,进一步的输入将被忽略,除非您调用std::cin.clear();
"My question is how do I get the stateName variables to work properly."
您可能希望接受 stateName1
和 stateName2
的字母数字输入,因此类型需要是 std::string
,而不是 int
std::string stateName1, stateName2;
应该合适(请注意,您需要 #include <string>
而不是 #include <cstring>
来执行此操作)。
我应该制作一个向用户询问一些基本问题的运输程序。 以下是说明:在线购物者在网络上搜索商品并从税率最低的州购买。编写一个简单的 C++ 程序来执行以下操作:向用户询问商品的单价、购物者要购买的商品数量、第一个州的名称、第一个州的税率、商品的名称第二州和第二州的税率。然后,计算每个州的总成本并决定购物者应该从哪个州购买商品。向购物者显示以下信息:单位商品成本、购买的商品数量、第一州的名称和税率、第二州的名称和税率,以及您对购物者应该从哪个州购买的建议.
列出程序中的所有文字和变量。
到目前为止,这是我的代码:
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int main()
{
double itemPrice, numberItem, taxRate, totalCost;
double stateTax1, stateTax2;
int stateName1, stateName2;
// ask the item's price
cout << "What is the price of the item?\n";
cin >> itemPrice;
// ask the # items
cout << "How many items are you going to purchase?\n";
cin >> numberItem;
// ask the name of the first state
cout << "What state are you having it shipped to?\n";
cin >> stateName1;
// ask the tax rate of the first state
cout << "What is the tax rate in " << stateName1 << "?\n";
cin >> stateTax1;
// ask the name of the second state
cout << "What state are you having shipped from?\n";
cin >> stateName2;
// ask the tax rate of the second state
cout << "What is the tax rate in " << stateName2 << "?\n";
cin >> stateTax2;
// first if the first state has a lower tax rate
// else if second state has lower tax rate
if (stateTax1 < stateTax2)
{
totalCost = itemPrice * numberItem * stateTax1;
cout << "The item cost is " << itemPrice << ".\n";
cout << "The number of items is " << numberItem << ".\n";
cout << "The name of the first state is " << stateName1 << ".\n";
cout << "The tax rate of the first state is " << stateTax1 << ".\n";
cout << "The name of the second state is " << stateName2 << ".\n";
cout << "The tax rate of the second state is " << stateTax2 << ".\n";
cout << "You should consider purchasing the item from " << stateName1 << ".\n";
}
else
{
totalCost = itemPrice * numberItem * stateTax2;
cout << "The item cost is " << itemPrice << ".\n";
cout << "The number of items is " << numberItem << ".\n";
cout << "The name of the first state is " << stateName1 << ".\n";
cout << "The tax rate of the first state is " << stateTax1 << ".\n";
cout << "The name of the second state is " << stateName2 << ".\n";
cout << "The tax rate of the second state is " << stateTax2 << ".\n";
cout << "You should consider purchasing the item from " << stateName2 << ".\n";
}
return 0;
}
我的问题是如何让 stateName 变量正常工作。我确信这是我应该知道但我不知道的一些基本字符串。除此之外,我相信我的其余代码都可以正常工作。尽管任何和所有提示都将不胜感激。
"Display the following to shopper: The unit item cost, the number of items purchased, the name and tax rate of the first state, the name and tax rate of the second state, and your recommendation on from which state the shopper should make the purchase."
你代码中的变量定义
int stateName1, stateName2;
// ^^^
将只接受输入语句的整数数值,如
cin >> stateName1;
每当您输入类似
的内容时Oklahoma
从输入提示中,std::cin
将被设置为fail()
状态,进一步的输入将被忽略,除非您调用std::cin.clear();
"My question is how do I get the stateName variables to work properly."
您可能希望接受 stateName1
和 stateName2
的字母数字输入,因此类型需要是 std::string
,而不是 int
std::string stateName1, stateName2;
应该合适(请注意,您需要 #include <string>
而不是 #include <cstring>
来执行此操作)。