increment off by 1

Jul 28, 2016 at 12:04am
Hey Im trying to do a currency converter for USD and EUR. The program has to ask first what conversion is desired, then what the conversion rate is, and then it shows the first ten values in a tabular form. When I run the program, it shows the first ten values but the converted value is off by 1. In other words, the converted converted value for 2 goes to 1 and the one for 3 goes to 2 and so on. Here´s what I have:

// Intro to Programming: Assigment #1
#include <iostream>
using namespace std;

int option;
double dollars=1;
double euros=1;
double rate;

int main()
{
cout << "Please select which conversion is desired: " << endl;
cout << "1. USD->EUR" << endl;
cout << "2. EUR->USD" << endl;
cin >> option;

cout << "Please enter the conversion rate, " << endl;
cout << "For example: 1EUR=1.4USD" << endl;
cin >> rate;

if (option == 1)
{
cout << "USD EUR" << endl;
cout << "________________"<< endl;

while (dollars <= 10)
{
cout << dollars++ << " " << dollars*rate << endl;
}
cout << "Thank you for using this program" << endl;
}

if (option == 2)
{
cout << "EUR USD" << endl;
cout << "________________"<< endl;

while (euros <= 10)
{
cout << euros++ << " " << euros*rate << endl;
}
cout << "Thank you for using this program" << endl;
}
system ("pause");
}



let me know if I have other errors.... thank you!!!
Jul 28, 2016 at 2:12am
closed account (48T7M4Gy)
Track the value of euro and dollars a line at a time and I think you will find the first value converted is 2, not 1 as you expect because dollars++ and euros++ are a bit too early in the proceedings.
Jul 28, 2016 at 2:32am
what do you exactly mean by tracking the values one line at a time?
Last edited on Jul 28, 2016 at 2:33am
Jul 28, 2016 at 2:43am
closed account (48T7M4Gy)
Print out the values as you proceed through each line of the while loop. You can do it manually with a pencil and paper. You should see your problem straight away.
Jul 28, 2016 at 2:57am
I got it fixed already, I moved the dollars++ and euros++ one line down.
thank you!
Jul 28, 2016 at 3:09am
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.