Hey guys. New to C++ and, as most of you will probably dread, a university student
My problem is as follows:
`character_reference' undeclared (first use this function) appears on the line to call my output function from main, however the undeclared varibale it's reffering to is about 8 lines up in the same scope
Any idea what went wrong (yes, the code is incomplete, but up until now, that chunk did execute correctly)
My code is here (if it can't be read on the forums correctly) http://home.iprimus.com.au/lansdown/fun_init.html
Code:
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <fstream>
//\\//\\//\\//\\//\\//\\//\\//\\//\\//
/* Prototypes */
//\\//\\//\\//\\//\\//\\//\\//\\//\\//
void input();
void output(char character_reference[], int character_frequency[], string index[]);
void down_string(string& words); // Turns strings into lower case.
void down_char(char& letter); // Turns characters into lower case.
void evaluate(char ch, int character_frequency[]);
void word_evaluation();
//\\//\\//\\//\\//\\//\\//\\//\\//\\//
/* Global Variables */
//\\//\\//\\//\\//\\//\\//\\//\\//\\//
string name_of_file, new_filename, words, different_words;
int lines = 1;
char letter;
ofstream file_output;
/////////////////////////////////////
//=================================//
/*| MAIN |*/
//=================================//
/////////////////////////////////////
int main()
{
//\\//\\//\\//\\//\\//\\
/* Array declaration */
//\\//\\//\\//\\//\\//\\
char character_reference[26];
int character_frequency[26];
string index[6000];
input(); // Displays the starting point of the file and asks for the relevant file names
output(character_reference, character_frequency, index); // Displays the statistics of the input file. THIS LINE IS THE PROBLEM!
system("PAUSE");
return 0;
}
///////////////////////////////////////////////////
/* down_string: converts strings into lower case */
///////////////////////////////////////////////////
void down_string(string& words)
{
for (int i=0;i<words.length();i++)
{
down_char(words[i]);
}
}
////////////////////////////////////////////////////
/* down_char: converts characters into lower case */
////////////////////////////////////////////////////
void down_char(char& letter)
{
if ((letter>=65)&&(letter<=90))
{
letter+=32;
}
}
///////////////////////////////////////////////////
/* Input */
///////////////////////////////////////////////////
void input()
{
// screen io
cout << "FILE STATISTICS" << endl;
cout << "Enter name of a text file to be processed: ";
cin >> name_of_file;
cout << endl;
cout << "Enter a (new) file name to save the information on the given file: ";
cin >> new_filename;
// End of input
}
///////////////////////////////////////////////////
/* Output */
///////////////////////////////////////////////////
void output(char character_reference[], int character_frequency[], string index[])
{
cout << "The file '" << name_of_file << "' has ";
ifstream input;
input.open(name_of_file.c_str());
int numWords = 0;
while (input >> words)
{
numWords++;
}
input.close();
// Call to evaluate the number of lines and words
input.open(name_of_file.c_str());
char ch;
while (input.get(ch))
evaluate(ch, character_frequency);
// Calls to word evaluation to acertain the different words.
word_evaluation();
// change this from cout to file_output
cout << lines <<" lines, " << numWords << " words, ";
cout << different_words << " Different Words, \nand the following letter frequencies: " << endl;
cout << "The alphabet letters occur in the following order of relative frequency\n";
cout << "(left to right, from most frequent to least frequent)" << endl;
// End of output.
}
/////////////////////////////////////
/* Evaluation (lines and words) */
/////////////////////////////////////
void evaluate(char ch, int character_frequency[])
{
down_char(ch);
if (ch == '\n')
lines ++;
for (int i = 97; i < 122; i++)
{
if (ch == i)
character_frequency[i -97] ++;
}
// End of evaluation
}
///////////////////////////////////////
/* Word Evaluation (different words) */
///////////////////////////////////////
void word_evaluation(string word, string index)
{
/*
Check word against the index, if in the index, do nothing.
If not in the index, put the word in the index
*/
int count = 0;
for (int i = 0; i < word; i ++)
{
down_string(word);
if (!(bsearch(word,index)))
{
index[count] = word;
count++;
// put word into the index
}
// sort the index
// output the index, line by line
}
///////////////////////////////////////
/* Binary Search */
///////////////////////////////////////
int bsearch(int x, int a[], int n)
{
// PRECONDITION: a[0] <= a[1] <= ... <= a[n-1]
// POSTCONDITION: return the index the element if it is found
// or return n(the dimention of a) if x was not found
int lo=0, hi=n-1, mid;
while (lo <= hi)
{
mid = (lo + hi)/2;
if (a[mid] == x) return mid;
if (a[mid] < x) lo = mid + 1;
else hi = mid-1;
}
return n; // x was not found in a[0..n-1)
}