No output from reading a simple string in

Feb 15, 2018 at 5:05pm
This simple code should output the name string but doesn't for some reason. The file its reading is this format and exact info:

January 3.2 February 1.2 March 2.2
August 2.3 September 2.4

I have to read in both numbers and names but cant even get output from one of the two so i need to fix this before i read in both.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  using namespace std;

int main()
{
	ifstream inputFile;
	int number, count = 0;
	string name;
	inputFile.open("Rainfall.txt");
	while (inputFile >> name)
	{

		cout << name << endl;
	}
	inputFile.close();

	return 0;
}

Feb 15, 2018 at 5:09pm
inputFile.open("Rainfall.txt");

This code expects the file Rainfall.txt to be in the same directory as the running executable. Is it? That is almost certainly not the same place as your "project" files.

Alternatively, provide the complete path:

inputFile.open("c:/some/directory/Rainfall.txt");
Feb 15, 2018 at 5:32pm
Well i made a different project with the same input file and did the same format to read it in like that and it worked, now i am editing that file so I thought it would still work but for some reason this does not work.
Feb 16, 2018 at 8:14am
You might want to try this:

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
#include <fstream>
using std::ifstream;

#include <iomanip>
using std::left;
using std::right;
using std::setw;

#include <iostream>
using std::cin;
using std::cout;

#include <string>
using std::string;

int main()
{
	double amount = 0.0;
	string monthName = " ";
	int count = 0;

	ifstream inFile("Rainfall.txt");

	if (inFile)
	{
		cout << setw(15) << left << "Month Name: " 
			  << setw(12) << right << "Rainfall Amount:\n\n";

		while (inFile.good())
		{
			inFile >> monthName >> amount;
			
			cout << setw(15) << left << monthName 
				  << setw(12) << right << amount << " mm\n";
		}
	}
	else
	{
		cout << "Error: The file could not be opened!\n";
	}

	cin.get();
	cin.ignore();
	return 0;
}


Which should result in the following output:

http://i66.tinypic.com/28iyuiw.jpg
Topic archived. No new replies allowed.