help with a program

I'm new to this and i dont know why this does not work, thanks.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
#include <string>
using namespace 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;
  		else if( 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."
Last edited on
What exactly seems to be the problem?

I would suggest that you prompt the user to as to what they are required to enter for all of your inputs.

Given a value such as 1.093, do you think that an int is the right type to represent the value?
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?

Thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main() {
	cout.setf(ios::fixed);
  	cout.precision(4);
  	double rate;
  	cin >> rate;
  	double value;
  	double eurcount=0;
  	double usdcount;
  	string moneytype;
  	while( cin >> value >> moneytype ) {
  		if( moneytype=="EUR" ) eurcount+=value;
  		else if( moneytype=="USD" ) {
  			value/=rate;
  			eurcount+=value;
  		}
  		usdcount=eurcount*rate; 				
  	}
  	cout << eurcount << " " << usdcount << endl;
}
That is because there is not a breaking condition for this while loop ( i suppose too ).

There is a break condition for the while loop. You need to make the loop fail by either entering an invalid entry or forcing EOF (CTRL-D or CTRL-Z).

so the dot is the invalid entry...

i've read about the ctrl-z but thought it was not that correct.

That web that judges programs have accepted it so there you go. Thanks!
so the dot is the invalid entry...

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.

Ok, i'll take that.

Thanks then.
Topic archived. No new replies allowed.