[homework] Reading in numbers and integers

I am in a C++ class at my college and I am having trouble reading in strings and integers. I will post the instructions, text file, and my code and any help or shove in the right direction would be great. Thanks in advance.

Here is the problem:

Program must convert to and from US dollars. Ask user whether converting to or
from, then what other currency and what amount. Display all available currencies
and current rates. Calculate conversion with 5% bank fee deducted from final
amount.

Each currency is stored in two lines: the first line specifies the currency name, the second the
current exchange rates. The final line reads “STOP,” indicating that all records have been
processed.
In addition, you must:
• complete and turn in an algorithm describing your solution to this problem
• use proper spelling and grammar in the displayed text, and ensure that the user
experience is consistent and intuitive

Here is my code:
(I know it doesn't do everything it is supposed to do yet, but I just need help getting started.
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

#include<iostream>
#include<fstream>


using namespace std;

int main(){
	// initialize variables
	ifstream infile;
	
	string country[5];
	float conversion_f[5];
	
	
	infile.open("rates.txt");
	
	while(infile){
		for(int i = 0; i < 5; i++){
			infile >> country[i] >> conversion_f[i];
			}
		}
	for(int i = 0; i < 5; i++){
		cout << country[i] << " " << conversion_f[i] <<  endl;
		}
	
	
	return 0;
	}


Here is the rates.txt file for the input:

Australian Dollars
0.96
British Pound Sterling
0.63
Canadian Dollars
1.00
Euro
0.74
Russian Rubles
30.01
STOP
------------------------

Here is what I'm having trouble doing... I want to read the strings and the floats in. Here is my output:

0
-1.46276e-05
3.98796e-34
-1.43755e-05
-1.4396e-05

-------------------------

I really don't know what I am doing wrong. :-/ Any help is greatly appreciated.
Just a couple things one thing I see right now...
Did you not
#include <string> ?
You'll be needing that for sure.

and how about the content of that text file?
Last edited on
infile >> country[i] >> conversion_f[i];

Using this style it will only read until it hits a space, so if you have something like "Australian Dollars" it will read one word into each variable, giving unexpected results.

You will want to look into getline (which reads in the entire sentence) and then work from there.
ok awesome. Thank you both! with getline(), can I use two types? or should I put it in there similar to this:

1
2
getline(infile, country[i]);
getline(infile, conversion[i]);


or is that completely ludicrous?
well I think you can pass an additional argument to getline: (this is optional)

getline(filestream, variable, delimiter);
The delimiter is a character of your choosing. (like ',')
getline(); will read from "filestream" until it reaches "delimiter"
then will assign the input up till "delimiter" to "variable"
then the get pointer moves to next byte after "delimiter"
and you're ready for another similar getline() call!

very useful.. depending on the format/content of your text files..
of which I was a little curious

btw.. I am not absolutely positive this will eliminate the blank space problem..

question to others reading: when you define a delimiter for getline(), will it still stop reading at " "?
Last edited on
Topic archived. No new replies allowed.