Need help adding looped inputs

I'm writing this program for my custom tee business that I run on the side. I've only been programming since the spring semester started and was wondering how I would be able to save and add values that I've inputted. The point of the program is to be able to loop the user input until the user is done. I've built a more primitive version only using cout and cin and repeating the same statement four times, but I'd like to make something that can go beyond four as well as stop before four.

cout << costPerShirt << endl;
cout << "Customer cost = " << ((qty * costPerShirt) + ship) + (qty*labor) << endl;
cout << "Profit = " << ((qty * costPerShirt) + ship) - (qty*labor) << endl;

^^This is only in the do{}while so that I could see if the values were being added together.

I remember something about storing values using a for statement, but I'm not completely sure, maybe I'm wrong.

example: for (int i; i>1; i++);



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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
int main()
{
	double shirtCost;
	double transferCost;
	double perTransfer;
	double costPerShirt;
	double ship;
	double i = 0;
	int qty;
	double labor = 4.0;
	bool d = false;
        char p;

	cout << "Enter price of shirt" << endl;
	cin >> shirtCost;
		cout << "Enter order qty" << endl;
	cin >> qty;
	cout << "Enter all shipping costs" << endl;
	cin >> ship;
do{


		cout << "Enter cost of transfer and prints per transfer separtated by a space" << endl;
		cout << "Press <p> key when done" << endl;
		cin >> transferCost >> perTransfer;
		costPerShirt = (shirtCost + (transferCost / perTransfer));

		
		if (p == 'p') 
		d = false;
	cout << costPerShirt << endl;
	cout << "Customer cost = " << ((qty * costPerShirt) + ship) + (qty*labor) << endl;
	cout << "Profit = " << ((qty * costPerShirt) + ship) - (qty*labor) << endl;

	
	}

	while (transferCost > i || p != p);


	cout << costPerShirt << endl;
	cout << "Customer cost = " << ((qty * costPerShirt) + ship) + (qty*labor) << endl;
	cout << "Profit = " << ((qty * costPerShirt) + ship) - (qty*labor) << endl;
}
Last edited on
bump
closed account (D80DSL3A)
Let's see if I understand the task.

You want to process a sequence of shirt orders.
Each order requires input of:
price of shirt
order quantity
shipping cost (1 number)
transfer cost
per transfer ?
labor isn't prompted for. I guess it's a fixed $4 charge?

Now 'Customer cost' and 'profit' may be calculated for the order and displayed to the user.

Above to be repeated in a loop.
How does the user indicate to quit? The condition presently in the while
while (transferCost > i || p != p); I'm having trouble making sense of.

Is this about right?
Per transfer = how many graphics can fit on one transfer sheet.
Labor IS a fixed $4.

And the rest is correct.

I'm trying to make it to where the user can enter however much transfer data they need before pressing "p" to end (Multiple graphics mean multiple transfers).

I made the while statement the way it is because I believed it to be true that the do will infinitely loop until the while requisites are met. If I use p==p in the while, I get an infinite loop, but p!=p gives output of the latest data.

Maybe another method would work better, these are just the tools I know of for now.
closed account (D80DSL3A)
OK. Here's your code reorganized a bit. The condition for quitting requires entering a value. I went with a loop end query, asking user "Another order? (y/n): " for reading into a char variable (repeat).
User is expected to enter 'y' to enter another order or 'n' to quit. It's just a simple way to do it.
See if this is close to what you need.
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
#include<iostream>
//#include<cstdlib>
//#include<ctime>
//#include "vector3D.h"
using namespace std;

int main()
{
	double shirtCost = 1.0;// it's good practice to supply initial values for variables.
	double transferCost = 1.0;
	double perTransfer = 1.0;
	double costPerShirt = 1.0;
	double ship = 1.0;
//	double i = 0;
	int qty = 1.0;
	double labor = 4.0;
//	bool d = false;
    char repeat = 'n';// another order? 

    cout  << "This program takes multiple shirt orders\n";

    do{
        cout << "Enter price of shirt" << endl;
        cin >> shirtCost;
        cout << "Enter order qty" << endl;
        cin >> qty;
        cout << "Enter all shipping costs" << endl;
        cin >> ship;

        cout << "Enter cost of transfer and prints per transfer separtated by a space" << endl;
  //      cout << "Press <p> key when done" << endl;
        cin >> transferCost >> perTransfer;
        costPerShirt = (shirtCost + (transferCost / perTransfer));

 //       cout << costPerShirt << endl;
        cout << "Customer cost = " << ((qty * costPerShirt) + ship) + (qty*labor) << endl;
        cout << "Profit = " << ((qty * costPerShirt) + ship) - (qty*labor) << endl;

        cout << "Another order? (y/n): ";
        cin >> repeat;

	}while (repeat == 'y');

    return 0;
}


Did you also ask about saving the data?
...and was wondering how I would be able to save and add values that I've inputted

Do you mean just maintaining and reporting totals in the program, or writing data to a text file for viewing after the program run, or both?
Last edited on
Thank you! I did not know about char repeat which will be helpful in the future.

And I mean maintaining and reporting totals. For example if there was a program for calculating a mean, could you start out with the user entering number of variables?

EX: data= 25, 15, 35

Output: Enter number of variables
input: 3
Output: Enter variables
input(in any order): 25 15 35
output: Mean=...



closed account (D80DSL3A)
Your welcome. The simple example you propose for finding the mean could be written:
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
#include <iostream>
using namespace std;

int main()
{
    double entry = 0.0;// a single entry
    double total = 0.0;
    int numEntries;// number of entries the user will make

    cout << "How many numbers? ";
    cin >> numEntries;

    for( int i = 1; i <= numEntries; ++i )
    {
        // get the entry  then add it to the total
        cout << "Entry " << i << ": ";
        cin >> entry;
        total = total + entry;// one way to increment total by entry
   //     total += entry; // another way
    }

    // process result
    double mean = total/numEntries;
    cout << "the mean value of the entries is " << mean  << endl;

    return 0;
}

Have you encountered a for loop yet?
It just collects the 3 things you would have to do in a while loop all at the top, compare:
1
2
3
4
5
6
int i = 1;// declare and init the loop counter
while( i <= numEntries )// test condition
{
    // code as above
    ++i;// increment the loop counter.
}

You could find any number of totals, etc. in the loop, Examples;
1
2
3
4
5
cin >> qty;
totalQty += qty;
double custCost = ((qty * costPerShirt) + ship) + (qty*labor);// a local variable may be declared when needed.
totalCustCost += custCost;// total variables were declared before the loop.
numberOfOrders++;// increment order count by 1 
Last edited on
Yes, we did. Today in class we were working with arrays and I realized that the for loop was probably what I would want to use for this program. I think that's what I was trying to describe in my original post by mentioning this "example: for (int i; i>1; i++);"

Thanks for your help! I seriously appreciate all of it.
Topic archived. No new replies allowed.