Variable or field declared void

Disclaimer that I've never worked with files before, so there's probably a lot more issues in this code than the void error. I can't even get the program to compile to find those errors though, so I'd appreciate insight into why I'm having this issue. Files are separated into .hpp and .cpp files for the assignment.

main
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
 /***************************
Description: Reads a file filled with letters, counts the letter frequencies in the file, writes the results to a new file (one new file per line)
***************************/
#include "count_letters.hpp"
#include "output_letters.hpp"
#include <cctype>
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::ifstream inputFile;
	std::ofstream outputFile;
	
	//get name of desired file from user
	std::cout << "Name the file you'd like to read:" << std::endl;
	std::string inputFileName;
	std::cin >> inputFileName;

	//open the file
	inputFile.open(inputFileName);

	while(!inputFile) //while file name doesn't exist
	{
		std::cout << "ERROR: Re-enter file name," << std::endl;
		std::cin >> inputFileName;
		inputFile.open(inputFileName);	
	}

	if (inputFile) //if file successfully opens
	{
		while (inputFile.peek() != EOF)
		{
			//create an array to pass to functions
			const int LETTERS = 26; //will count frequency of the 26 letters
			int array[LETTERS] = {0}; //every frequency begins at 0
			
			//call the functions
			count_letters(inputFile, array);
			output_letters(outputFile, array);
	
			inputFile.close(); //close the file
		}
	}
	
	else
	{
		std::cout << "Could not access file." << std::endl;
	}
	
	return 0;
}


count_letters.hpp
1
2
3
4
5
6
7
8
9
#ifndef COUNT_LETTERS_HPP
#define COUNT_LETTERS_HPP


void count_letters(std::ifstream& inputFile, int* array);


#endif



count_letters.cpp
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
/***************************
Description: Takes an input file stream variable and a pointer to an array of integers as parameters.
*Reads the paragraph string from the input file stream
*Counts the letter frequencies in the paragraph
*Stores the frequencies in the array
*Disregards upper/lowercase letters
***************************/
#include <iostream>
#include <string>
#include <fstream>
#include "count_letters.hpp"

void count_letters(std::ifstream& inputFile, int* array);
{
	std::string = reading;
	
	//reads line and store data in reading
	getline(inputFile, reading); 
	int max=reading.length(); //counts how many letters in line
	
	//32 spaces between caps and lower in acsii table, 65=A, 97=a
	for (length=0; length<max; length++)
	{
		for (count=0; count<26; count++)
		{
			char ch = reading.get();
			if (ch>64 && ch<90) //if uppercase
			{
				ch+=32; //change to lowercase
				array[ch-97]++;
			}
			else
			{
				array[ch-97]++;	
			}
		}
	}	
}


output_letters.hpp
1
2
3
4
5
6
7
8
9
#ifndef OUTPUT_LETTERS_HPP
#define OUTPUT_LETTERS_HPP


void output_letters(std::ofstream& outputFile,int* array);
	
#endif



output_letters.cpp
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
/***************************
Description: Takes an ouput file stream variable and a pointer to an array of integers THAT CONTAINS FREQUENCIES OF LETTERS as parameters
*Asks user for the filename the user wants to output to
*outputs the frequencies of letters to the output file
***************************/
#include <iostream>
#include <string>
#include <fstream>
#include "output_letters.hpp"

void output_letters(std::ofstream& outputFile, int* array)
{
	std::string outputFileName;
	char alphabet;

	//name and open output file
	std::cout << "What file would you like to output to?" << std::endl;
	std::cin >> outputFileName;
	outputFile.open(outputFileName);

	for(int count=0; count<26; count++)
	{
		alphabet = (count+97);
		outputFile << alphabet << ": " << array[count] << std::endl;
	}

	for(int count2=0; count2<26; count2++)
	{
		array[count]=0; //reset array for next round of counting
	}

	outputFile.close(); //close file
}
Last edited on
#include <fstream> (or better, #include <iosfwd>) in both your header (.hpp) files.
Last edited on
Watch out for extra semicolons where they don't belong.

In future please post all your error messages, exactly as they appear in your development environment, you should have multiple errors in your present code.

There is too much compile time and logic errors in your code, almost every second line has an error, I give up.

Just to name a few edits (with capital letter comments) from count_letters.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
void count_letters(std::ifstream& inputFile, int* array); // REMOVE THIS SEMICOLON
{
       	std::string reading; // REMOVED = (equal sign)

	//reads line and store data in reading
	std::getline(inputFile, reading); // ADDED STD::

      //...
       for (int length = 0; length < max; length++) // ADDED INT LENGTH
	{
		for (int count = 0; count < 26; count++) // ADDED INT
      //....
}


Last edited on
Topic archived. No new replies allowed.