int i;
static int count = 0;
while (1)
{
cout << "Enter the index number for the product you want to purchase!" << endl;
cin >> i;
switch (i)
{
case 1:
count++;
cout << "stationary box" << "(quantity=)" << count << endl;
break;
case 2:
count++;
cout << "Remote control car" << "(quantity=)" << count << endl;
break;
case 3:
count++;
cout << "Magnet" << "(quantity=)" << count << endl;
break;
}
}
Asked
Active
Viewed 69 times
-4

Evg
- 25,259
- 5
- 41
- 83
-
5use 3 counters? – zkoza Apr 11 '21 at 11:28
-
i actually used a static count array which did the job – Muhammad Ameer Ali Khan Apr 11 '21 at 12:00
-
Does this answer your question? [Get loop counter/index using for…of syntax in JavaScript](https://stackoverflow.com/questions/10179815/get-loop-counter-index-using-for-of-syntax-in-javascript) – Adesso Apr 11 '21 at 12:45
-
There's no point in using static variables here. – zkoza Apr 12 '21 at 11:46
1 Answers
1
Use three count variables:
count 1
,count2
,count3
Then increment them in the switch-case statement according to the input
int i;
int count1 = 0,
count2 = 0,
count3 = 0;
while (1)
{
cout << "Enter the index number for the product you want to purchase!" << endl;
cin >> i;
switch (i)
{
case 1:
count1++;
cout << "stationary box" << "(quantity=)" << count1 << endl;
break;
case 2:
count2++;
cout << "Remote control car" << "(quantity=)" << count2 << endl;
break;
case 3:
count3++;
cout << "Magnet" << "(quantity=)" << count3 << endl;
break;
}
}

CapTen101
- 476
- 6
- 12