#include <iostream>
#include <string>
usingnamespace std;
int main() {
cout.setf(ios::fixed);
cout.precision(4);
int rate;
cin >> rate;
int value;
int eurcount=0;
int usdcount;
string moneytype;
while( cin >> value >> moneytype ) {
if( moneytype=="EUR" ) eurcount+=value;
elseif( moneytype=="USD" ) {
value/=rate;
eurcount+=value;
}
usdcount=eurcount*rate;
}
cout << eurcount << " " << usdcount << endl;
}
This is the problem:
"Write a program that, given the exchange rate from euros to dollars (EUR->USD), and a list of values given in either euros or dollars, prints the sum of such values both in euros and in dollars.
For example, if we are given 1 EUR = 1.093 USD and the list
31 USD
2.40 EUR
27.25 USD
50 USD
The sum of all such values in euros is 101.4393, whereas the sum of all such values in dollars is 110.8732. Thus, the program must print both values as result.
Your program can only include iostream and string, no other library can be used. Your program must not store the input by any means and must not define nor use functions or procedures other than those defined in the iostream and string libraries. Failure to satisfy these requirements will invalidate (final score = 0) your program.
Exam score: 2.5 Automatic part: 50%
Input
The input starts with a strictly positive real number, that represents how much dollars costs one euro. After that, several lines follow, each one with a strictly positive real number x followed by either EUR or USD, representing that x must be interpreted as a value in euros or dollars, respectively.
Output
The output has the sum of the money represented by the given x’s printed twice, once in euros and once in dollars, separated by a blank space. Print both values with 4 digits after the decimal point. Use the following instructions at the beginning of your program to fix such precision."
jlb, i know that would make more sense but the problem does not require that so if i put for example: "enter exchange rate"... would make the program wrong but yeah, i know that is better.
Well, when i execute it prints 0 and another value that it is not the one i am looking for.
Cire, i know it should be a double or some kind like that but i thought that if i set precision before, an integer will be ok too. I'll change that and see if that was the error, thanks.
i know that would make more sense but the problem does not require that so if i put for example: "enter exchange rate"... would make the program wrong but yeah, i know that is better.
There is nothing in your instructions that seem to prohibit prompting the user for the values, but whatever.
an integer will be ok too.
But remember that there are no fractions with integers. So knowing this how can you retrieve a value of 1.093 into your variable? Remember the extraction operator knows that integers have no fractions as well.
Ok, caption that. So setting precision only works for the output.
I have one more question; If i put the cout inside while, if i input a value and it's moneytype n times, it prints the updated count n times after every input.
So that's because it is in the while loop i suppose.
Then i move the cout outside the while and i does not print the output unless i input a dot. That is because there is not a breaking condition for this while loop ( i suppose too ).
So my question: is there a way to print the count once there are no more inputs without having to input a dot, or i must introduce a break condition?
Not necessarily, a decimal point is a valid character for a floating point type, when using the default locale. You would be better off using EOF or some other character, like a letter.