For loop keep going until 1000 euro
Sep 13, 2016 at 8:45pm UTC
Hi,
I had to design and write a program to ask for five conversions. In each case ask the user for the exchange rate and for the sum (in pounds) to be converted. Show the user the equivalent number of euros each time.
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
#include <iostream>
using namespace std;
void main()
{
int i, j;
float euro;
float pounds;
float rate;
float eurototal =0;
float poundstotal = 0;
cout << "Conversion program" ;
cout << endl << endl;
cout << "Enter exchange rate: " ;
cin >> rate;
cout << endl;
for (i = 1; i <= 5; i++)
{
cout << "Enter the " << i << " value in pounds: " ;
cin >> pounds;
euro = pounds*rate;
cout << "Equivalent in euro is: " << euro << endl << endl;
eurototal = eurototal + euro;
poundstotal = poundstotal + pounds;
}
cout << "Total converted amount in euro is: " << eurototal << endl;
cout << "Total converted amount in pounds is: " << poundstotal << endl;
cout << endl << endl;
system("pause" );
}
The problem came up when I had to do another part of the task:
"As above but this time allow the user to make as many conversions as required until 1000 euros or more have been converted".
I have tried to research through the web but I haven't been able to find a solution.. Has anyone any idea or tip?
Thank you in advance.
Sep 13, 2016 at 9:17pm UTC
Use a do-while loop instead of the for loop.
1 2 3 4
do
{
// Conversion goes here
} while (eurototal < 1000); // If eurototal >= 1000, it'll end the loop
Last edited on Sep 13, 2016 at 9:19pm UTC
Sep 13, 2016 at 9:19pm UTC
@Tkey
The other part of the task is ..
Instead of a for loop of 5, use a do/while loop.
change for (i = 1; i <= 5; i++)
to do
and add }while (eurototal<1000.0);
as a new line 31.
Sep 14, 2016 at 10:04pm UTC
Yea, finally it's working! Thank you for help!
Topic archived. No new replies allowed.