Looping Question

I have been trying to make this program work for a week now and can not figure it out. I need to use looping to prompt a user to enter there first initial and there are only three employees so i need to validate the input.

Next I need to ask that user to enter there sales (max of 4) and run an accumulating loop once the users are done entering data the program should print a final report with the the employees and the sales they entered. this is what i have.

I think I am over thinking this project but I can not get it to do what I want and I have tried a few different methods. Any information would be appreciated. Also we havent gotten into functions yet so I cant use them.


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ofstream outputFile;
string name;
int number;

//set format for dollar values
cout << fixed << showpoint << setprecision(2);
//Get user to open their file
do
{
cout << "To enter sales for Reid, enter R.\n"
<< "To enter sales for Jarid, enter J.\n"
<< "To enter sales for Nicole, enter N.\n"
<< "Enter your choice: ";
cin >> name;
// input validation for name
while (!(name == "R" || name == "J" || name == "N"))
{
cout << "Enter the first letter of your first name in caps: ";
cin >> name;
}

if (outputFile)
{
name += ".txt";
outputFile.open(name.c_str());
int sales;
double total = 0.0;

cout << "How many sales ammounts will you enter (Maximum is 4): ";
cin >> sales;
// input validation number of sales entered
if ((sales <= 0) || (sales >= 5))
{
cout << "Number is invalid may not be less than 1 or more "
<< "than 4.\n"
<< "Enter ammount of sales to be entered: ";
cin >> sales;
}
for (int count = 1; count <= sales; count++)
{
double ammount;
cout << "Enter the ammount for sale " << count << ": ";
cin >> ammount;
// Store total
total += ammount; //accumulate running total

double commission = total * .1;
outputFile << commission << endl;
outputFile << total << endl;

cout << "Commission earned for sales entered is: " << commission << endl;

// close file
outputFile.close();
}
}
cout << "Enter Z to quit or any key to go to menu: ";
cin >> name;
} while (name != "Z");


// display sales table

ifstream inputFile;

double total;
double commission;

inputFile.open("R.txt");

inputFile >> commission;
cout << "Total commission earned for Reid is: " << commission << endl;

inputFile.close();

inputFile.open("J.txt");
inputFile >> commission;
cout << "Total commission earned for Jarid is: " << commission << endl;
inputFile.close();

inputFile.open("N.txt");
inputFile >> commission;
cout << "Total commission earned for Nicole is: " << commission << endl;
inputFile.close();


system ("pause");
return 0;
}
Well I was able to run the program and it seems to be working correctly. I wouldn't be able to give you a more accurate assessment unless I had the exact specifications of what the program needs to do.

I think the only things you could do to make the program "better" is to use functions and formatting the output to the command line a little bit better so it's not so hard to see what the program is doing after using it for a few iterations.
I was going to format after I finally finished the code. The only thing I was trying to figure out was how to go through the loops and lets way "Reid" entered some sales and it calculated his commission and stored it in a file. If I do the loop again and enter more sales info for "Reid" the new information overwrites the old. I was trying to get it to accumulate the total no matter now many iterations where ran.
Last edited on
Ah, ok. That's as simple as pointing you back to the documentation and giving some sample code:

http://www.cplusplus.com/doc/tutorial/files/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	ofstream outputFile;
	outputFile.open("Filename.txt", ios::app);

	outputFile << "This text will be added" << endl;
	outputFile << "to the end of the file" << endl;

	return 0;
}

The program must prompt user to enter their first initial, then allows them to enter up to 4 sale amounts at a time for the sales person. I need to calc and display the commission to a running total for that sales person. and when 'Z' is pressed the program will display a final report with each employees total commission.
ok,

That's a very basic design concept that you need to be able to develop on your own. If I were to help you at this point I would effectively be writing it for you and not allowing you to grow. Good luck.
I don't want you to write it for me I just wanted you to look at my code and tell me if I am on the right track with what I am doing.

Would there be any benefit to using char instead of string?
Should I have premade text files instead of what i am doing by having the program write the .txt file by the user entry.
should I use a switch statement instead?

I have devoted most of my time this week trying to write this and I do not want to give up on it I just want to be able to understand what I am doing wrong.

Thank you for all of your help.
I think the biggest thing that will help with your design is to make it modular with functions. For example:

1
2
3
SaveInfoToFile(); 

GetAmount(int sales, Employee emp);


1
2
3
4
5
struct Employee{
    int sales;
    double commission;
    string name;
};


Your program is at least running so I'd say you're about half way there. It's just designing it to use more Object Oriented techniques so you can make sense of the data flow more easily.
Thank you again for the help.
Topic archived. No new replies allowed.