So with this program, basically, I ask for the names of three people, and store those strings in an array. That parts seems to work fine.
After that, I ask for their quarterly reports. So each person gets 4, and it's ordered through the array so that:
Index 0-3 goes to person A
Index 4-7 goes to person B
And index 8-11 goes to person C.
In this second for-loop that processes this second array, I have a list of if/else if statements that determine the name of the person in question that I will be asking for. In the separate function itself, readSales(), I have a similar thing set up to determine which quarter to ask for. This is designed to loop 12 times in order to get all 12 indexes filled.
For some reason, after I input the people's names, the program crashes. Any idea why?
Also, I know "using namespace std;" isn't very popular, but that's how my professor wants it so that's how I have it.
// In this program, I will
// (1) Ask for the names of three salespeople.
// (2) Accept sales for each quarter (for each person).
// (3) Display their name and total sales amount.
// I will use three functions:
// (1) main
// (2) readInfo
// (3) displayInfo
// No global variables, and I will pass data as parameters.
#include <iostream>
#include <string>
using namespace std;
// In order to pass an array through a void function,
// I must create it in the main function.
// So...
string readNames(int); // Function that gathers data from user.
double readSales(string, int);
void displayInfo(); // Function that displays final result.
int main() // Our main function
{
// Create my variables for arrays and final result.
string arrNames[3];
double arrSales[12], result;
// I must call my first function now.
for (int i = 0; i < 3; i++)
{
arrNames[i] = readNames(i); // Successfully gathers all 3 names and stores them in the array. Woo!
}
// Now I must gather the number data, using a double array.
string person = "uninitialized";
int x = 0;
for (x; x < 12; x++);
{
if (x < 4) // setup to ask question
person = arrNames[0];
if (x < 8)
person = arrNames[1];
if (x < 12)
person = arrNames[2];
arrSales[x] = readSales(person, x); // Successfully gathers all 12 quarters and stores them. Yay!
}
cout << arrNames[3];
} // end function main()
string readNames(int count)
{
for (count; count < 3;)
{
string i;
cout << "Please input salesperson " << count + 1 << "'s name: ";
cin >> i;
cout << endl;
return i;
}
return 0;
} // end function readNames()
double readSales(string person, int count) // reading the sales
{
double i; // variable I am returning at the end of function.
int quarter;
if (count == 0 || count == 4 || count == 8)
{
quarter = 1;
}
else if (count == 1 || count == 5 || count == 9)
{
quarter = 2;
}
else if (count == 2 || count == 6 || count == 10)
{
quarter = 3;
}
else if (count == 3 || count == 7 || count == 11)
{
quarter = 4;
}
else
return 0;
cout << "Please input salesperson " << person << "'s sales for Quarter " << quarter << " (Please round to the nearest cent): $" << endl;
cin >> i;
return i;
}
Please remove the semi-colon in the for (x; x < 12; x++);
You can make it like this,
int x = 0;
for (x; x < 12; x++)
{
if ((x >= 0) && ( x <= 3)) // setup to ask question
{
person = arrNames[0];
}
else if ((x >= 4) && (x <= 7))
{
person = arrNames[1];
}
else
{
person = arrNames[2];
}
arrSales[x] = readSales(person, x); // Successfully gathers all 12 quarters and stores them. Yay!
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.