I’m trying to code a program that displays letters in alphabetical order. It complies but the problem is that when it compiles, the letters [characters] don’t show up. The only thing the text file has is some random letters {A E C B D O).

int count(char input_filename[]);
void load_file(char input_filename[], int things[], int array_size);
void display(int things[], int array_size);
void bubble_sort(int things[], int array_size);
void pause(void);

char data_filename[] = "C:\\Data\\Letters.txt";

//************************************...
// main
//************************************...

int main(void)
{
int record_count = count_file_values(data_filename);
int acres[record_count];
load_file(data_filename, acres, record_count);
cout << "The letters are: \n";
display(acres, record_count);
bubble_sort(acres, record_count);
cout << "\n\nThe sorted letters are: \n";
display_array(acres, record_count);
pause();

return 0;
}

int count_file_values(char input_filename[])
{
fstream inData; 
double next_value;
char number_of_values = 0;

inData.open(input_filename, ios::in); 
if (!inData)
{
cout << "\n\nError opening input data file: " << input_filename << "\n\n";
pause();
exit(EXIT_FAILURE);
}
// Get data values and count them

while (inData >> next_value)
{
number_of_values++;
}

// Close the files
inData.close(); 

return number_of_values;
}

void load_file(char input_filename[], int things[], int array_size)
{
fstream inData; 

inData.open(input_filename, ios::in); 
if (!inData)
{
cout << "\n\nError opening input data file: " << input_filename << "\n\n";
pause();
exit(EXIT_FAILURE);
}
else
{
for (int i = 0; i < array_size; i++) 
{
inData >> things[i];
}
inData.close();
}
return;
}


void display(int things[], int array_size)
{
for (char i = 0; i < array_size; i++)
{
cout << things [i] << " " ;
}


return;
}

void bubble_sort(int things[], int array_size)
{
bool moresortneeded; 
int temp; 

do
{
moresortneeded = false;
for(int i = 0; i < array_size - 1; i++)
{
if(things[i] > things[i+1])
{
temp = things[i];
things[i] = things[i+1];
things[i+1] = temp;
moresortneeded = true;
}
}
}
while(moresortneeded);

return;
}

void pause(void)
{
cout << "\n\n";
system("PAUSE");
cout << "\n\n";
return;
}

Recommended Answers

All 3 Replies

What compiler did you use to compile that ? I tried to compile it with VC++ 2008 Express and got billions of errors. The first error was undefined function count_file_values() that appears on line 15 of the code you posted.

So, don't complain about the program not working right when there are so many errors and warnings. Fix all the compile errors/warnings before even attempting to run it.

count_file_values() always returns 0 because the file contains alphabetical character values but the function is attempting to read double numbers. Change the data type of next_value from double to char.

also, array things must be of type char, not int because it will hold characters, not integers.

What compiler did you use to compile that ? I tried to compile it with VC++ 2008 Express and got billions of errors. The first error was undefined function count_file_values() that appears on line 15 of the code you posted.

I'm using Dev C++. When I compile , there are no errors. The only error comes up when it displays the characters. It displays the ""The letters are" and ""The sorted letters are:" but it fails to actually display the characters from the text file.

Somehow I don't think this compiles.

In my compiler:

Error E2268 x.cpp 15: Call to undefined function 'count_file_values' in function main()
Error E2313 x.cpp 16: Constant expression required in function main()
Error E2451 x.cpp 18: Undefined symbol 'cout' in function main()
Error E2268 x.cpp 22: Call to undefined function 'display_array' in function main()
Error E2451 x.cpp 30: Undefined symbol 'fstream' in function count_file_values(char *)
Error E2379 x.cpp 30: Statement missing ; in function count_file_values(char *)
Error E2451 x.cpp 34: Undefined symbol 'inData' in function count_file_values(char *)
Error E2090 x.cpp 34: Qualifier 'ios' is not a class or namespace name in function count_file_values(char *)
Error E2121 x.cpp 34: Function call missing ) in function count_file_values(char *)
Error E2451 x.cpp 37: Undefined symbol 'cout' in function count_file_values(char *)
Error E2268 x.cpp 39: Call to undefined function 'exit' in function count_file_values(char *)
Error E2451 x.cpp 39: Undefined symbol 'EXIT_FAILURE' in function count_file_values(char *)
Warning W8057 x.cpp 52: Parameter 'input_filename' is never used in function count_file_values(char *)
Error E2451 x.cpp 56: Undefined symbol 'fstream' in function load_file(char *,int *,int)
Error E2379 x.cpp 56: Statement missing ; in function load_file(char *,int *,int)
Error E2451 x.cpp 58: Undefined symbol 'inData' in function load_file(char *,int *,int)
Error E2090 x.cpp 58: Qualifier 'ios' is not a class or namespace name in function load_file(char *,int *,int)
Error E2121 x.cpp 58: Function call missing ) in function load_file(char *,int *,int)
Error E2451 x.cpp 61: Undefined symbol 'cout' in function load_file(char *,int *,int)
Error E2451 x.cpp 63: Undefined symbol 'EXIT_FAILURE' in function load_file(char *,int *,int)
Warning W8057 x.cpp 74: Parameter 'input_filename' is never used in function load_file(char *,int *,int)
Warning W8057 x.cpp 74: Parameter 'things' is never used in function load_file(char *,int *,int)
Error E2451 x.cpp 81: Undefined symbol 'cout' in function display(int *,int)
Warning W8057 x.cpp 86: Parameter 'things' is never used in function display(int *,int)
Error E2451 x.cpp 114: Undefined symbol 'cout' in function pause()
Error E2268 x.cpp 115: Call to undefined function 'system' in function pause()
*** 22 errors in Compile ***

And in Dev-CPP

Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\wjp\x.cpp" -o "C:\wjp\x.exe"    -I"C:\ProgramFiles\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\ProgramFiles\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\ProgramFiles\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\ProgramFiles\Dev-Cpp\include\c++\3.4.2"  -I"C:\ProgramFiles\Dev-Cpp\include"   -L"C:\ProgramFiles\Dev-Cpp\lib" 
C:\wjp\x.cpp: In function `int main()':
C:\wjp\x.cpp:15: error: `count_file_values' undeclared (first use this function)
C:\wjp\x.cpp:15: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\wjp\x.cpp:18: error: `cout' undeclared (first use this function)
C:\wjp\x.cpp:22: error: `display_array' undeclared (first use this function)

C:\wjp\x.cpp: In function `int count_file_values(char*)':
C:\wjp\x.cpp:29: error: `int count_file_values(char*)' used prior to declaration
C:\wjp\x.cpp:30: error: `fstream' undeclared (first use this function)
C:\wjp\x.cpp:30: error: expected `;' before "inData"

C:\wjp\x.cpp:34: error: `inData' undeclared (first use this function)
C:\wjp\x.cpp:34: error: `ios' has not been declared
C:\wjp\x.cpp:34: error: `in' undeclared (first use this function)
C:\wjp\x.cpp:37: error: `cout' undeclared (first use this function)
C:\wjp\x.cpp:39: error: `EXIT_FAILURE' undeclared (first use this function)
C:\wjp\x.cpp:39: error: `exit' undeclared (first use this function)

C:\wjp\x.cpp: In function `void load_file(char*, int*, int)':
C:\wjp\x.cpp:56: error: `fstream' undeclared (first use this function)
C:\wjp\x.cpp:56: error: expected `;' before "inData"
C:\wjp\x.cpp:58: error: `inData' undeclared (first use this function)
C:\wjp\x.cpp:58: error: `ios' has not been declared
C:\wjp\x.cpp:58: error: `in' undeclared (first use this function)
C:\wjp\x.cpp:61: error: `cout' undeclared (first use this function)
C:\wjp\x.cpp:63: error: `EXIT_FAILURE' undeclared (first use this function)
C:\wjp\x.cpp:63: error: `exit' undeclared (first use this function)
C:\wjp\x.cpp: In function `void display(int*, int)':
C:\wjp\x.cpp:81: error: `cout' undeclared (first use this function)
C:\wjp\x.cpp: In function `void pause()':
C:\wjp\x.cpp:114: error: `cout' undeclared (first use this function)
C:\wjp\x.cpp:115: error: `system' undeclared (first use this function)

Execution terminated
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.