How to make this program run 4 times without ending
I just started "Introduction to C++" this semester, and I'm stuck on an exercise that involves taking data from a table and writing a program that will let you input and display each item (line by line) showing the Item, Cost, and calculated Total Cost.
The program I've written works perfectly for the first item, but I need it to repeat the same 3 questions (allowing user input of different items/costs/discounts each time it repeats the questions), while printing the calculated total after each question is answered. I'm assuming this will involve either a do-while
loop or a for
loop, but I can't figure out how to integrate what I've already written into a loop.
#include <iostream>
using namespace std;
int main()
{
string Item;
float Cost;
float Discount;
float TotalCost;
cout << "What is the item? \n";
cin >> Item;
cout << "What is the cost? \n";
cin >> Cost;
cout << "What is the discount? \n";
cin >> Discount;
TotalCost = Cost - Discount;
cout << Item << "'s Total Cost is " << TotalCost;
return 0;
}
I've tried making a for
loop (code I've tried below), but it doesn't work, and I haven't been able to find any loop examples in my book or online that involve accepting user input each time the process loops.
#include <iostream>
using namespace std;
int main()
{
string Item;
float Cost;
float Discount;
float TotalCost;
for (int a = 0; a < 5; a++)
{
cout << "What is the item? \n";
cin >> Item;
cout << "What is the cost? \n";
cin >> Cost;
cout << "What is the discount? \n";
cin >> Discount;
TotalCost = Cost - Discount;
cout << Item << "'s Total Cost is " << TotalCost;
}
return 0;
}
from Recent Questions - Stack Overflow https://ift.tt/3zWLtKm
https://ift.tt/eA8V8J
Comments
Post a Comment