trying to count numbers and words in a txt file

I'm trying to count numbers and words in a text file. Is it possible to run two counts simultaneously. This is coming up with multiple errors...

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
	// Variables
	int count = 0;       // To count numbers
	int count1 = 0;
	double number;       // To hold a number from the file
	double total = 0.0;  // Accumulator
	double average;      // To hold the average
	char words;
	double words1;

	// File stream object
	ifstream inFile;

	// Open the file.
	inFile.open("C:\\Users\\User\\Desktop\\programming110\\random.txt");
	// check if the file is open
	if (!"C:\\Users\\User\\Desktop\\programming110\\random.txt")
    {
        cout << "File open failure.";
    }

	while (inFile >> number)
	{
		// We have a number! Increment the counter.
		count++;

		// Add the number to the accumulator.
		total += number;
	}
	while (inFile >> words)
    {
        count1++
        words1 += words;
    }

	// Close the file.
	inFile.close();

	// Calculate the average of the numbers.
	average = total / count;

	// Display the results.
	cout << "Number of numbers: " << count << endl
		 << "Sum of the numbers: " << total << endl
		 << "Average of the numbers: " << average
		 << endl;
		 << "Number of words: " << count1 << endl;

    return 0;
}
Read in each white space delimited token as a string. Then check if the string holds a valid representation of a number.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <memory>
#include <fstream>

bool is_an_integer( std::string trimmed_str ) // decimal, hexadecimal or octal
{
    try
    {
        std::size_t pos = 0 ;
        // http://en.cppreference.com/w/cpp/string/basic_string/stol
        std::stoll( trimmed_str, std::addressof(pos), 0 ) ; // base == 0 => decimal, hexadecimal or octal
        return pos == trimmed_str.size() ; // the whole string was parsed
    }
    catch(...) {}

    return false ;
}

bool is_a_real_number( std::string trimmed_str ) // decimal, binary, infinity or NaN
{
    try
    {
        std::size_t pos = 0 ;
        // http://en.cppreference.com/w/cpp/string/basic_string/stof
        std::stold( trimmed_str, std::addressof(pos) ) ;
        return pos == trimmed_str.size() ; // the whole string was parsed
    }
    catch(...) {}

    return false ;
}

bool is_a_number( std::string str )
{ return is_an_integer(str) || is_a_real_number(str) ; }

// -12345 0xabcde 03562 +23.45 -0X234P5 +INFINITY NAN 

int main()
{
    int n_integers = 0 ;
    int n_real_numbers = 0 ;
    int n_words = 0 ;
    
    const char* const path = __FILE__ ; // "C:/Users/User/Desktop/programming110/random.txt"
    std::ifstream file(path) ;
    std::string str ;
    while( file >> str )
    {
        if( is_a_real_number(str) )
        {
            ++n_real_numbers ; // integers are real numbers
            if( is_an_integer(str) ) ++n_integers ; 
        }
        
        else ++n_words ;
    }

    // print counts
    std::cout << "integers: " << n_integers << "  real numbers: " << n_real_numbers << "  other: " << n_words << '\n' ;
}

http://coliru.stacked-crooked.com/a/1c6a4d22e8e1e505
Topic archived. No new replies allowed.