Counting the amound of strings with s while loop.

I am finisihing up a code I have to write where it takes data from a txt file, albums, band names, and price of the album. Formats them to display in the console as album, artist, and price then put the total amount. I have everything there but after wards I have to print it to a summary text file with the number of downloads and total amount. I get the ammount, but can't get the count right with my loop. Just need to see what I am doing wrong. This is the contents of the text file:
Turn on the Bright Lights
Interpol
9.49
House of Jealous Lovers
The Rapture
1.29
Fever to Tell
Yeah Yeah Yeahs
6.99
Desperate Youth, Blood Thirsty Babes
TV on the Radio
8.91
The Fragile
Nine Inch Nails
12.49

And here is the 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  #include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
	ifstream	inputFile;
	ofstream	outputfile;

	string		album_Name, 
				artist_Name;
	
	int			count = 0;

	double		cost_Of_Album1, 
				cost_Of_Album2, 
				cost_Of_Album3, 
				cost_Of_Album4,
				cost_Of_Album5,
				total_Cost;
	
	//Takes the values from file orders.txt to be brought onto the program.
	inputFile.open("orders.txt");

	outputfile.open("summary.txt");

	cout << "Welcome to the Online Music store.\n";
	
	cout << "You have submitted the following order: \n";

	cout << setfill('*') << setw(70) << "*" << endl;

	cout << setfill(' ');


	cout << left << setw(5) << "Title" << right << setw(41) << "Artist" << setw(24) << "Cost" << endl;

	getline(inputFile, album_Name);
	getline(inputFile, artist_Name);
	inputFile >> cost_Of_Album1; 
	cout << album_Name << setw(23) << artist_Name << setw(22) << cost_Of_Album1 << endl;
	
	inputFile.ignore();
	getline(inputFile, album_Name);
	getline(inputFile, artist_Name);
	inputFile >> cost_Of_Album2;
	cout << album_Name << setw(28) << artist_Name << setw(19) << cost_Of_Album2 << endl;
		
	inputFile.ignore();
	getline(inputFile, album_Name);
	getline(inputFile, artist_Name);
	inputFile >> cost_Of_Album3;
	cout << album_Name << setw(42) << artist_Name << setw(15) << cost_Of_Album3 << endl;
 
	inputFile.ignore();
	getline(inputFile, album_Name);
	getline(inputFile, artist_Name);
	inputFile >> cost_Of_Album4;
	cout << album_Name << setw(19) << artist_Name << setw(15) << cost_Of_Album4 << endl;
	
	inputFile.ignore();
	getline(inputFile, album_Name);
	getline(inputFile, artist_Name);
	inputFile >> cost_Of_Album5;
	cout << album_Name << setw(44) << right << artist_Name << setw(15) << cost_Of_Album5 << endl;

	total_Cost = cost_Of_Album1 + cost_Of_Album2 + cost_Of_Album3 + cost_Of_Album4 + cost_Of_Album5;
	
	cout << setfill('-') << setw(70) << "-" << endl;

	cout << setfill(' ');
	
	cout << "Total" << setw(65) << total_Cost << endl;

	cout << setfill('=') << setw(70) << "=" << endl;

	while (inputFile >> album_Name)
	{
		count++;

	}
	outputfile << "Downloads : " << count << endl;
	outputfile << "Total Due: " << total_Cost << endl;

	
		outputfile.close();
		inputFile.close();
		
		system("PAUSE");
		return 0;
}


THanks in advance for any help to move forward.
Hello rrush1,

I have not tested this yet, but at line 79 you are trying to read the file,but the file pointer is at the end. So when you try to read the file the read fails and then the while loop fails so nothing is ever counted.

Try putting this before the while loop at line 79:result = fseek(inputFile, 0L, SEEK_SET); then you will need to read all three lines to make the proper count. Something like:

1
2
3
4
5
6
7
8
9
result = fseek(inputFile, 0L, SEEK_SET);

while (inputFile >> album_Name)
	{
                inputFile >> artist_Name;
                inputFile >> cost_Of_Album1;  // <--- Changing the variable is not important.
		count++;

	}


This way you will read the entire file and have a proper count. You will need to include "cstdio" or "stdio.h". The first one would be the better choice.

Hope that helps,

Andy
Unfortunately that did not work. I threw in the cstudio in but still did not thanks for your help though man!
Hello rrush1,

Upon further testing I found this to work:

1
2
3
4
5
6
7
8
9
	inputFile.seekg(0, inputFile.beg);

	while (getline(inputFile, album_Name))
	{
		getline(inputFile, artist_Name);
		inputFile >> cost_Of_Album1;  // <--- Changing the variable is not important.
		inputFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		count++;
	}


and you do not need anything more than the header file "fstream".

Sorry for the confusion as I do not use "seekg" very often.

The line inputFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); uses the heder file "limits". And the way I have my include files I have to "#undef max" before I include "limits".

Hope that helps,

Andy
Sorry meant to get back you didn't have enough time to try this and had to move on to other project. I am gonna try it this evening though again thank you for your help.
Topic archived. No new replies allowed.