output keeps running on

My output keeps running on and when I put a for loop at the beginning only one line works. Can someone please help me. Thanks in advance.






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


main() {
int num_receipts;
int salesman1, salesman2;
int
salesman1_total = 0,
salesman2_total = 0;
int price, cost;
int initial_stock_on_hand;


cout << "Name of salesman #1? ";
cin >> salesman1;

cout << "Name of salesman #2? ";
cin >> salesman2;

cout << "What's the initial stock on hand? ";
cin >> initial_stock_on_hand;

cout << "What's the sales price for a computer? ";
cin >> price;

cout << "What's your cost per unit? ";
cin >> cost;

cout << "How many sales receipts are there? ";
cin >> num_receipts;

for (int i = 1; i <= num_receipts; i++) {
int quantity;
int salesman_id;
cout << "Data for sales receipt #" << i << endl;

cout << "Who was the salesman? ";
cin >> salesman_id;

cout << "How many computers? ";
cin >> quantity;

if (salesman_id == 1)
salesman1_total += quantity;
else
salesman2_total += quantity;

cout << endl;
}

int total_quantity = salesman1_total + salesman2_total;

cout << "-------- End-of-day Report -------" << endl;
cout << "Initial stock on hand: " << initial_stock_on_hand << endl;
cout << "Total quantity sold: " << total_quantity << endl;
cout << "Day's take: " << total_quantity * price << endl;
cout << "Day's profit: " << total_quantity * (price - cost) << endl;
if (salesman1_total > salesman2_total)
cout << "Winner is salesman 1" << salesman1 << "with " << salesman1_total <<
" units sold" << endl;
else
cout << "Winner is salesman 2" << salesman2 << "with " << salesman2_total <<
" units sold" << endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>

using namespace std;


int main()//use int main
{
    int num_receipts;
    string salesman1, salesman2;//STRING NOT INT
    int salesman1_total = 0, salesman2_total = 0;
    int price, cost;
    int initial_stock_on_hand;


    cout << "Name of salesman #1? "; //this sais name so i changed it to string..
    cin>>salesman1;

    cout << "Name of salesman #2? ";//same as above
    cin>>salesman2;

    cout << "What's the initial stock on hand? ";//fine
	cin>>initial_stock_on_hand;

    cout << "What's the sales price for a computer? ";//perfect
	cin>>price;

    cout << "What's your cost per unit? ";//couldnt be better
	cin>>cost;

    cout << "How many sales receipts are there? ";//fantastic
	cin>>num_receipts;

	for(int i=1; i<=num_receipts; i++)
	{
		int quantity;
		int salesman_id;

		cout<<"Data for sales receipt #"<<i<<endl;

		cout<<"Who was the salesman?: ";
		cin>>salesman_id;

		cout<<"How many computers?: ";
		cin>>quantity;

		if(salesman_id==1)
			salesman1_total+=quantity;
		else if(salesman_id==2)
			salesman2_total+=quantity;
	}

	int total_quantity=salesman1_total+salesman2_total;

	cout<<"---------End of the day report---------"<<endl;
	cout<<"Initial stock at hand: "<<initial_stock_on_hand<<endl;
	cout<<"Total quantity sold: "<<total_quantity<<endl;
	cout<<"Day's take: "<<total_quantity*price<<endl;
	cout<<"Day's profit: "<<total_quantity*(price-cost)<<endl;

	if(salesman1_total>salesman2_total)
		cout<<"Winnder is "<<salesman1<<" with "<<salesman1_total<<" sales."<<endl;
	else if(salesman2_total>salesman1_total)
		cout<<"Winnder is "<<salesman2<<" with "<<salesman2_total<<" sales."<<endl;
	else
		cout<<"Sales man 1 and two are equal!";

	cin.ignore();
        cin.ignore();
        cin.ignore();

	return 0;

}


THIS Works fine, your code was a bit messy so i rewrote it.

The first problem is that you are asking for a name to be typed in yet the variable type is an integer! Unless you are an alien where you guys call yourself 25213 you need to change that to a string;

The second problem which can cause unforseen errors is that you just declared main and not int main() I dont know the exact details of this one but i remember reading an article that sais its a no no. so unless you know what youre doing i would refrain from doing that.

anyways you might want to add in a system("Pause")(EDIT:Though most people will tell you not to use that in "real" applications) in there because cin.ignore() sucks (it gets stuck in an input buffer and so you never know when you need to put like 5 of them in there);



if this were a real application id probably ask you to redo it however. There are some very confusing parts in it like the sales man id. why not just use their names? And a few other things that i had to think about for some time before i understood.
Last edited on
Topic archived. No new replies allowed.