Problem with reading a file

Here is my problem...

I am trying to read the data from the input file below and stop when the input file says "STOP". When I try to debug and print it to the screen, it prints something like a combination of the two variables... Here is my code:

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
40
41
42
43
44
45
46
/*******************************************************
* Program: Currency Converter
* Authors: Matthew Haggard and Adam Edwards
* Date Modified: 2013-02-04
*
* This program will convert from US dollars to some
* other currency with a bank fee deducted from the 
* final amount. 
*******************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>

using namespace std;

struct Money{
	string country;
	string number; // convert from string to float later.
	};
	
int main(){
	// Initialize variables
	
	int array = 5;
	Money x[array];
	ifstream infile;
	
	infile.open("rates.txt"); // Open the file
	
	for(int i = 0; i < array; i++){
		getline(infile, x[i].country);
		getline(infile, x[i].number);
	
		// DEBUG
		cout << x[i].country << " - " << x[i].number<< endl;
		}

	cout << x[0].country << endl << x[1].country << endl;
	
	infile.close(); // Close the file
	
	}


Here is my input file:

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

Here is my output:

- 0.96ian Dollars
- 0.63 Pound Sterling
- 1.00n Dollars
- 0.74
- 30.01Rubles
Australian Dollars
British Pound Sterling

Any help would be greatly appreciated....

My guess is that `x[0].country' holds "Australian Dollars\r"
because `rates.txt' was written in windows, but you are using *nix

You may fix the `text' file before processing, by instance with vimset fileformat=unix
or you could remove the '\r' at the end of the strings.

also http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
Oh ok. Yea that makes alot of sense. I was using Windows and Ubuntu depending on where I was at. I ran the program on Windows and it worked just fine... Thanks alot for your help. I didn't even know that was a problem before now.
Topic archived. No new replies allowed.