尝试显示包含整数的数组中的一串单词
Trying to display a string of words from an array that contains integers
我正在解决这个问题,但一直无法显示我需要显示的内容。这是问题:
Write a program that determines which of a company's four divisions (Northeast, Southeast, Northwest, and Southwest) had the greatest sales for a quarter. It should include the following two functions, which are called by main.
double getSales() is passed the name of a division. It asks the user for the division's quarterly sales figure, validates the input, then returns it. It should be called once for each division.
void findHighest() is passed the four sales totals. It determines which is the largest and prints the name of the high-grossing division, along with its sales figure.
除了显示部门名称外,我什么都能弄清楚。我是一般编码的新手,所以如果我在这里有其他我不知道的错误,请多多包涵。我已经尽我所能地查找了所有内容,并进行了如此多的更改,但仍然迷路了。请帮忙!
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
// Variable Declerations
const string NE = "Northeast";
const string SE = "Southeast";
const string NW = "Northwest";
const string SW = "Southwest";
double getSales(string);
void findHighest(double, double, double, double);
int main()
{
double salesNE, salesSE, salesNW, salesSW;
string division;
salesNE = getSales(NE);
salesSE = getSales(SE);
salesNW = getSales(NW);
salesSW = getSales(SW);
findHighest(salesNE, salesSE, salesNW, salesSW);
cout << "by " << division << endl;
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
cin >> sales;
while (sales < 0)
{
cout << "Invalid input. Please enter a positive integer.";
cin >> sales;
}
return sales;
}
void findHighest(int salesNE, int salesSE, int salesNW, int salesSW)
{
int divisions[] = { salesNE, salesSE, salesNW, salesSW};
int max;
max = divisions[0];
for (int count = 1; count > 5; count++)
{
if (divisions[count] < max)
max = divisions[count];
}
/*
cout << division << endl; - does not show.
*/
cout << " is the division with the highest sales total of: " << max << endl;
}
你已经解决了问题,只是没有完成。在 findHighest()
中,您将销售数字放入一个数组中。因此,您已经知道哪个值对应哪个部门。只需对部门名称做同样的事情,然后找到销售额最高的数组索引,这样就可以在同一索引处显示部门名称。
您的代码还有一些其他问题:
findHighest()
的声明和定义不匹配。
您正试图在 main()
中显示 division
,但说明明确告诉您要在 findHighest()
中显示它。
getSales()
没有考虑到用户输入的内容可能无法转换为 double
.
的可能性
您在 findHighest()
中的 for
循环编码错误。它需要在计数器中使用< 4
而不是> 5
,并且在比较中需要使用> max
而不是< max
。
试试这个:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// Variable Declerations
const string NE = "Northeast";
const string SE = "Southeast";
const string NW = "Northwest";
const string SW = "Southwest";
double getSales(string);
void findHighest(double, double, double, double);
int main()
{
double salesNE, salesSE, salesNW, salesSW;
salesNE = getSales(NE);
salesSE = getSales(SE);
salesNW = getSales(NW);
salesSW = getSales(SW);
findHighest(salesNE, salesSE, salesNW, salesSW);
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
do
{
if (cin >> sales)
{
if (sales >= 0) break;
cout << "Invalid input. Please enter a positive number.";
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number.";
}
}
while (true);
return sales;
}
void findHighest(double salesNE, double salesSE, double salesNW, double salesSW)
{
double sales[] = { salesNE, salesSE, salesNW, salesSW };
string divisions[] = { NE, SE, NW, SW };
int highest;
double max;
highest = 0;
max = sales[0];
for (int index = 1; index < 4; ++index)
{
if (sales[index] > max)
{
highest = index;
max = sales[index];
}
}
cout << divisions[highest] << " is the division with the highest sales total of: " << max << endl;
}
如果将 divisions[]
数组移动到全局范围,并使用 main()
中的数组来保存销售数据,则可以进一步简化。说明书上只说索取数字并传给findHighest()
,但没有说如何保存并传给他们:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// Variable Declerations
const string divisions[] = { "Northeast", "Southeast", "Northwest", "Southwest" };
const int numDivisions = sizeof(divisions)/sizeof(divisions[0]);
double getSales(string);
void findHighest(double[]);
int main()
{
double sales[numDivisions];
for(int index = 0; index < numDivisions; ++index)
{
sales[index] = getSales(divisions[index]);
}
findHighest(sales);
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
do
{
if (cin >> sales)
{
if (sales >= 0) break;
cout << "Invalid input. Please enter a positive number.";
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number.";
}
}
while (true);
return sales;
}
void findHighest(double sales[])
{
int highest = 0;
double max = sales[0];
for (int index = 1; index < numDivisions; ++index)
{
if (sales[index] > max)
{
highest = index;
max = sales[index];
}
}
cout << divisions[highest] << " is the division with the highest sales total of: " << max << endl;
}
或者:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// Variable Declerations
struct Division
{
string name;
double sales;
}
Division divisions[] =
{
{"Northeast", 0.0},
{"Southeast", 0.0},
{"Northwest", 0.0},
{"Southwest", 0.0}
};
const int numDivisions = sizeof(divisions)/sizeof(divisions[0]);
double getSales(string);
void findHighest();
int main()
{
for(int index = 0; index < numDivisions; ++index)
{
divisions[index].sales = getSales(divisions[index].name);
}
findHighest();
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
do
{
if (cin >> sales)
{
if (sales >= 0) break;
cout << "Invalid input. Please enter a positive number.";
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number.";
}
}
while (true);
return sales;
}
void findHighest()
{
int highest = 0;
double max = divisions[0].sales;
for (int index = 1; index < numDivisions; ++index)
{
if (divisions[index].sales > max)
{
highest = index;
max = divisions[index].sales;
}
}
cout << divisions[highest].name << " is the division with the highest sales total of: " << max << endl;
}
我正在解决这个问题,但一直无法显示我需要显示的内容。这是问题:
Write a program that determines which of a company's four divisions (Northeast, Southeast, Northwest, and Southwest) had the greatest sales for a quarter. It should include the following two functions, which are called by main.
double getSales() is passed the name of a division. It asks the user for the division's quarterly sales figure, validates the input, then returns it. It should be called once for each division.
void findHighest() is passed the four sales totals. It determines which is the largest and prints the name of the high-grossing division, along with its sales figure.
除了显示部门名称外,我什么都能弄清楚。我是一般编码的新手,所以如果我在这里有其他我不知道的错误,请多多包涵。我已经尽我所能地查找了所有内容,并进行了如此多的更改,但仍然迷路了。请帮忙!
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
// Variable Declerations
const string NE = "Northeast";
const string SE = "Southeast";
const string NW = "Northwest";
const string SW = "Southwest";
double getSales(string);
void findHighest(double, double, double, double);
int main()
{
double salesNE, salesSE, salesNW, salesSW;
string division;
salesNE = getSales(NE);
salesSE = getSales(SE);
salesNW = getSales(NW);
salesSW = getSales(SW);
findHighest(salesNE, salesSE, salesNW, salesSW);
cout << "by " << division << endl;
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
cin >> sales;
while (sales < 0)
{
cout << "Invalid input. Please enter a positive integer.";
cin >> sales;
}
return sales;
}
void findHighest(int salesNE, int salesSE, int salesNW, int salesSW)
{
int divisions[] = { salesNE, salesSE, salesNW, salesSW};
int max;
max = divisions[0];
for (int count = 1; count > 5; count++)
{
if (divisions[count] < max)
max = divisions[count];
}
/*
cout << division << endl; - does not show.
*/
cout << " is the division with the highest sales total of: " << max << endl;
}
你已经解决了问题,只是没有完成。在 findHighest()
中,您将销售数字放入一个数组中。因此,您已经知道哪个值对应哪个部门。只需对部门名称做同样的事情,然后找到销售额最高的数组索引,这样就可以在同一索引处显示部门名称。
您的代码还有一些其他问题:
findHighest()
的声明和定义不匹配。您正试图在
main()
中显示division
,但说明明确告诉您要在findHighest()
中显示它。
的可能性getSales()
没有考虑到用户输入的内容可能无法转换为double
.您在
findHighest()
中的for
循环编码错误。它需要在计数器中使用< 4
而不是> 5
,并且在比较中需要使用> max
而不是< max
。
试试这个:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// Variable Declerations
const string NE = "Northeast";
const string SE = "Southeast";
const string NW = "Northwest";
const string SW = "Southwest";
double getSales(string);
void findHighest(double, double, double, double);
int main()
{
double salesNE, salesSE, salesNW, salesSW;
salesNE = getSales(NE);
salesSE = getSales(SE);
salesNW = getSales(NW);
salesSW = getSales(SW);
findHighest(salesNE, salesSE, salesNW, salesSW);
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
do
{
if (cin >> sales)
{
if (sales >= 0) break;
cout << "Invalid input. Please enter a positive number.";
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number.";
}
}
while (true);
return sales;
}
void findHighest(double salesNE, double salesSE, double salesNW, double salesSW)
{
double sales[] = { salesNE, salesSE, salesNW, salesSW };
string divisions[] = { NE, SE, NW, SW };
int highest;
double max;
highest = 0;
max = sales[0];
for (int index = 1; index < 4; ++index)
{
if (sales[index] > max)
{
highest = index;
max = sales[index];
}
}
cout << divisions[highest] << " is the division with the highest sales total of: " << max << endl;
}
如果将 divisions[]
数组移动到全局范围,并使用 main()
中的数组来保存销售数据,则可以进一步简化。说明书上只说索取数字并传给findHighest()
,但没有说如何保存并传给他们:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// Variable Declerations
const string divisions[] = { "Northeast", "Southeast", "Northwest", "Southwest" };
const int numDivisions = sizeof(divisions)/sizeof(divisions[0]);
double getSales(string);
void findHighest(double[]);
int main()
{
double sales[numDivisions];
for(int index = 0; index < numDivisions; ++index)
{
sales[index] = getSales(divisions[index]);
}
findHighest(sales);
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
do
{
if (cin >> sales)
{
if (sales >= 0) break;
cout << "Invalid input. Please enter a positive number.";
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number.";
}
}
while (true);
return sales;
}
void findHighest(double sales[])
{
int highest = 0;
double max = sales[0];
for (int index = 1; index < numDivisions; ++index)
{
if (sales[index] > max)
{
highest = index;
max = sales[index];
}
}
cout << divisions[highest] << " is the division with the highest sales total of: " << max << endl;
}
或者:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
// Variable Declerations
struct Division
{
string name;
double sales;
}
Division divisions[] =
{
{"Northeast", 0.0},
{"Southeast", 0.0},
{"Northwest", 0.0},
{"Southwest", 0.0}
};
const int numDivisions = sizeof(divisions)/sizeof(divisions[0]);
double getSales(string);
void findHighest();
int main()
{
for(int index = 0; index < numDivisions; ++index)
{
divisions[index].sales = getSales(divisions[index].name);
}
findHighest();
return 0;
}
double getSales(string division)
{
double sales;
cout << "Enter the quarterly sales figure for " << division << endl;
do
{
if (cin >> sales)
{
if (sales >= 0) break;
cout << "Invalid input. Please enter a positive number.";
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number.";
}
}
while (true);
return sales;
}
void findHighest()
{
int highest = 0;
double max = divisions[0].sales;
for (int index = 1; index < numDivisions; ++index)
{
if (divisions[index].sales > max)
{
highest = index;
max = divisions[index].sales;
}
}
cout << divisions[highest].name << " is the division with the highest sales total of: " << max << endl;
}