help with for loops

Trying to get 'parallel arrays" to match up and give product, price, and have user input quantity then generate a total amount at end. Very new to C++ and having trouble getting the order and sequence down right. PLEASE HELP!!

#include <iostream>

using namespace std;

int main()
{
string PRODUCT[3] = { "Men's Golf Ballz", "Women's Golf Ballz", "Kids Golf Ballz" };

int QUANT[3];

double PRICE[3] = { 23.00, 20.00, 15.00 };

cout << "Product #1 - Men's Golf Ballz are $23.00/dozen" << endl;
cout << "Product #2 - Women's Golf Ballz are $20.00/dozen" << endl;
cout << "Product #3 - Kid's Golf Ballz are $15.00/dozen" << endl;

for (int counter = 0; counter < 3; counter++)
{
cout << "Enter quantity for Product # " << counter + 1 << ": ";
cin >> QUANT[counter];
}

for (int index = 0; index < QUANT; index++);
{
double total = int QUANT[] * double PRICE[];


}
}
Hi,
Please use code tags
http://www.cplusplus.com/articles/jEywvCM9/

here's a solution, necessary things are explained as comments
do ask if you have any doubt
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()
{
string PRODUCT[3] = { "Men's Golf Ballz", "Women's Golf Ballz", "Kids Golf Ballz" };

int QUANT[3];

double PRICE[3] = { 23.00, 20.00, 15.00 };

cout << "Product #1 - Men's Golf Ballz are $23.00/dozen" << endl;
cout << "Product #2 - Women's Golf Ballz are $20.00/dozen" << endl;
cout << "Product #3 - Kid's Golf Ballz are $15.00/dozen" << endl;
double total=0;//inistialize total to zero

for (int counter = 0; counter < 3; counter++)
{
cout << "Enter quantity for Product # " << counter + 1 << ": ";
cin >> QUANT[counter];
//the whole thing can be kept inside one for loop only
 total =  QUANT[counter] * PRICE[counter];//you dont need to redeclare data type
}
cout<< "Your total bill is : $"<<total;

}
Product #1 - Men's Golf Ballz are $23.00/dozen
Product #2 - Women's Golf Ballz are $20.00/dozen
Product #3 - Kid's Golf Ballz are $15.00/dozen
Enter quantity for Product # 1: 3
Enter quantity for Product # 2: 6
Enter quantity for Product # 3: 4
Your total bill is : $60


Hope it helps

PS: welcome to cplusplus.com
Last edited on
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
#include <iostream>

using namespace std;

int main()
{
    string PRODUCT[3] = { "Men's Golf Ballz", "Women's Golf Ballz", "Kids Golf Ballz" };

    int QUANT[3];

    double PRICE[3] = { 23.00, 20.00, 15.00 };

    cout << "Product #1 - Men's Golf Ballz are $23.00/dozen" << endl;
    cout << "Product #2 - Women's Golf Ballz are $20.00/dozen" << endl;
    cout << "Product #3 - Kid's Golf Ballz are $15.00/dozen" << endl;

    for (int counter = 0; counter < 3; counter++)
    {
        cout << "Enter quantity for Product # " << counter + 1 << ": ";
        cin >> QUANT[counter];
    }

    for (int index = 0; index < QUANT; index++);
    {
        double total = int QUANT[] * double PRICE[];
    }
}


Reposting your original code for readability and line numbers
Last edited on
Line 23 - QUANT is an array of int type. It's missing [x] and also, why is it being compared to index in the first place? Take out semi-colon at the end.

Line 25 - QUANT and PRICE have [] but missing the index. You do not specify type in front of variables. You specify type only when you declare variables.

This is the code after I fixed your last for-loop (Only the syntax, not the logic)
1
2
3
4
    for (int index = 0; index < 3; index++ )
    {
        double total = QUANT[index] * PRICE[index];
    }


Now, what are you trying to accomplish with this for-loop? You want to print the total price, right? In order to print the total price, shouldn't you include cout?
Last edited on
I got the code to start working without errors, but the equation is only giving me the last "products price * quantity". It's only giving me the total end price for the "Kids golf ballz" not including the other 2 products before it.
Like this?

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

using namespace std;

int main()
{
string PRODUCT[3] = { "Men's Golf Ballz", "Women's Golf Ballz", "Kids Golf Ballz" };

int QUANT[3];

double PRICE[3] = { 23.00, 20.00, 15.00 };

cout << "Product #1 - Men's Golf Ballz are $23.00/dozen" << endl;
cout << "Product #2 - Women's Golf Ballz are $20.00/dozen" << endl;
cout << "Product #3 - Kid's Golf Ballz are $15.00/dozen" << endl;
double total[3]={0};//inistialize total to zero also note that its an array

for (int counter = 0; counter < 3; counter++)
{
cout << "Enter quantity for Product # " << counter + 1 << ": ";
cin >> QUANT[counter];
//the whole thing can be kept inside one for loop only
 total[counter] =  QUANT[counter] * PRICE[counter];//you dont need to redeclare data type

}
cout<<"Men's Golf Ballz: $"<<total[0]<<endl;//just printing the arrya
cout<<"Women's Golf Ballz: $"<<total[1]<<endl;//using struct/class for these stuff would be better
cout<<"Kid's Golf Ballz: $"<<total[2]<<endl;

}
Product #1 - Men's Golf Ballz are $23.00/dozen
Product #2 - Women's Golf Ballz are $20.00/dozen
Product #3 - Kid's Golf Ballz are $15.00/dozen
Enter quantity for Product # 1: 4
Enter quantity for Product # 2: 6
Enter quantity for Product # 3: 7
Men's Golf Ballz: $92
Women's Golf Ballz: $120
Kid's Golf Ballz: $105
Last edited on
Topic archived. No new replies allowed.