While loop

I am having trouble on figuring out how to read each line from a txt file into a decimal variable, the only argument that works for what I'm doing is char.

Here's the instructions for my HW.

https://drive.google.com/file/d/1KZqoNS8a4EqRKqSmpUeRlGQAKupa46hF/view?usp=sharing

Here's the txt file.

https://drive.google.com/file/d/1KrLWYRDu7sPKtdOOGb2SCwP5cyaVjjI0/view?usp=sharing

I actually need help for all of the steps but I'm stuck with reading each line into a decimal variable.

Here's my current code.

#include "stdafx.h"
#include <iostream> // standard console i/o
#include <string> // string library
#include <iomanip> // output formatting
#include <fstream>

using namespace std; // standard namespaces

int main()
{
ifstream inFile; // handle to input file
string strFileName = // file name
"CIS022_S2019_Lab5a_data.txt";
char cChar; // character read from file
string strFileData; // data read from file
string strTotal = // output of total value
"Total value: %.4f";
double fFileData;

inFile.open(strFileName); // open the input file

if (inFile.is_open()) // did the file open
{
inFile.get(cChar); // get the first char of the file

while (!inFile.eof()) // while not end of file
{
cout << cChar; // display the character
inFile.get(cChar); // get the next character
}

inFile.close(); // close the file
}
else
printf("File %s not found. \n", strFileName.c_str()); // file not found error message



cout << endl << endl; // blank line
system("Pause"); // pause before closing
return 0;
}


If you check the instructions in the link above you will see that the console does not display the txt file, but I also don't know how to read it without displaying it. I've experimented with removing cout in my while section but I don't think that it works and the only thing that runs is my blank line and system pause at the end. Please help. (No programming experience at all)
I see you are trying to read characters char cChar;, but the data type of the file is a double or float.

I would recommend to change it to a double type double cChar; and would try to read the data from the file like...

1
2
3
4
5
while (inFile >> cChar)
{
     // Do something while it reads the file.
    // Like maintain a running total, for instance.
}


I see it wants you to use the eof.

Last edited on
I tried what you recommended but it won't work. It gives me this error.

Severity Code Description Project File Line Suppression State
Error (active) no instance of overloaded function "std::basic_ifstream<_Elem, _Traits>::get [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list CIS022_S2019_Lab5a d:\CIS022\Lab5a\CIS022_S2019_Lab5a\CIS022_S2019_Lab5a \CIS022_S2019_Lab5a.cpp 33
Something like 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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

int main()
{
	std::ifstream inFile;
	std::string fileName {"Numbers.txt"};
	double numbers{};
	int countLines{0};
	double runningTotal{0.0};

	inFile.open(fileName); // Step 4a.

	if (!inFile)
	{
		std::cout << "Error opening " << fileName << std::endl;
		return 0;
	}

	else
	{
		while (!inFile.eof()) // Step 4b.
		{
			inFile >> numbers; // Step 4b.
			countLines++; // Step 5.
			runningTotal += numbers; // Step 6.
		}
		inFile.clear(); // Clear the eof flag.
		inFile.close(); // Step 7.
	}

	double average {runningTotal / countLines}; // Step 8.

	// Step 9.
	std::cout << "There are " << countLines << " lines in the file.\n";
	std::cout << std::fixed << std::setprecision(4);
	std::cout << "Total value: " << runningTotal << std::endl;
	std::cout << "Average value: " << average << std::endl;
}
Thank you so much it worked! Just one question, when you are assigning variables like

int countLines{0};

is that the same as

int countLines = 0;

?

We've only used curly brackets in class when doing if else statements & switch cases.
It is the same. By the way, how many lines did you count? 1000?
1001, I double checked the txt file and there's actually a blank line at the end which I believe is being read also.
Last edited on
It should be 1000, like the example output in the instructions. There is a blank line after your last number in the text file; therefore the eof flag is not set and would count as part of the number.

A couple of options would be:
a) Delete the extra blank line at the end of the file.
b) Check that each input is a number. Something like:

1
2
3
4
5
6
7
8
9
10
		while (!inFile.eof()) // Step 4b.
		{
			// As long as it reads number, count it. do not count blank lines.
			if (inFile >> numbers) // Step 4c.
				countLines++; // Step 5.

			runningTotal += numbers; // Step 6.
		}
		inFile.clear();
		inFile.close(); // Step 7. 


There are more options, but since the professor was specific about using eof(), then checking each input is a number is feasible.
Something like 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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

int main()
{
	std::ifstream inFile/*{"Numbers.txt"}*/;
	std::string fileName {"Numbers.txt"};
	double numbers{};
	int countLines{0};
	double runningTotal{0.0};

	inFile.open(fileName); // Step 4a.

	if (!inFile)
	{
		std::cout << "Error opening " << fileName << std::endl;
		return 0;
	}

	else
	{
		while (!inFile.eof()) // Step 4b.
		{
			// As long as it reads number, count it. do not count blank lines.
			if (inFile >> numbers) // Step 4c.
				countLines++; // Step 5.

			runningTotal += numbers; // Step 6.
		}
		inFile.clear();
		inFile.close(); // Step 7.
	}

	double average {runningTotal / countLines}; // Step 8.

	// Step 9.
	std::cout << "There are " << countLines << " lines in the file.\n";
	std::cout << std::fixed << std::setprecision(4);
	std::cout << "Total value: " << runningTotal << std::endl;
	std::cout << "Average value: " << average << std::endl;
}

Topic archived. No new replies allowed.