program compiled. will not execute.

Alright, I have written this program for class. I guess it's supposed to be pretty straight forward passing array elements around between functions. It's taken me the better part of two days to write it and finally have it compiled. now when I go to open it using the ./ method it tells me permission denied.

so I looked around the web and tried using the gcc filename -o a.out method and it gives me this garbage:

1
2
3
4
5
6
7
8
9
10
11
12
13
 /tmp/ccEdcxsV.o: In function `readStudentFile(int*, int*, int*)':
tests.cpp:(.text+0x14d): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream()'
tests.cpp:(.text+0x172): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)'
tests.cpp:(.text+0x18f): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
tests.cpp:(.text+0x1a3): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
tests.cpp:(.text+0x1c7): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
tests.cpp:(.text+0x1db): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
tests.cpp:(.text+0x203): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
tests.cpp:(.text+0x217): undefined reference to `std::basic_ios<char, std::char_traits<char> >::operator void*() const'
tests.cpp:(.text+0x22e): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::close()'
tests.cpp:(.text+0x23f): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
tests.cpp:(.text+0x25d): undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()'
/tmp/ccEdcxsV.o: In function `inputStudentRecords(int*, int*, int*, int)': 



I'm assuming my code is faulty, but why would it still compile?

If there are any truly kind people looking to help a brother out, I'll put my whole code down at the bottom. Any tips would be most welcome and I'll be very grateful!!


//This program will recieve input from a file (if available)and the
// user for student identification numbers, and two test scores.
// The program will output a 3 column table displaying the two test
//scores for each student as well as display the max score and
//min score for each test, as well as the average score for each test
// Program will work for up to 40 students.

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes//
///////////////////////

int readStudentFile (int [], int [], int []);
//three arrays as parameters for student ID, and test 1 and test 2
//scores

int inputStudentRecords (int [], int [], int [], int);
//three student arrays as parameter and int to count the records

void calculateStatistics (int [], int [], int, int, int, int, int,
float, float);
//parameters are the two test arrays, int for the count, two ints for
// thenminimum score of each test, two ints for the max score of
//each test and two floats for the avg of each test

void outputStudentRecords (int [], int [], int [], int, int, int, int, int,
float, float);
//parameters are 3 student arrays, int for the count variable,
//two ints for min scores, two ints for max scores, and two floats for
// avgs



int main()
{
int test1 [40];
int test2 [40];
int stuID [40];
int count;
int min1;
int max1;
int min2;
int max2;
float avg1;
float avg2;

//function call for reading in saved files
readStudentFile (stuID, test1, test2);
//function call for user input
inputStudentRecords(stuID, test1, test2, count);
//function call for calculating mins, maxs, and avgs
calculateStatistics (test1, test2, count, min1, min2,
max1, max2, avg1, avg2);
//function call for outputting data
outputStudentRecords (stuID, test1, test2, count,
min1, min2, max1, max2, avg1, avg2);

return 0;
}

int readStudentFile (int stuID [], int test1 [], int test2 [])
{

ifstream inputFile;
int count = 0;

//open file and read contents into arrays
inputFile.open("grades.doc");

while (inputFile >>test2[count])
{
while (inputFile >>test1[count])
{
while (inputFile >>stuID[count])
{
}
}
count++;
}

inputFile.close();
return count;
}

int inputStudentRecords (int stuID [], int test1 [], int test2[], int count)
{
int response = 1;//to initialize the while loop
while (response == 1)
{
cout << "Enter '1' if you have student records to ";
cout << "input. Otherwise, enter any other digit. ";
cin >> response;
if (response == 1)
{
cout << endl << "Enter student ID: ";
cin >> stuID [count];
cout << endl << "Enter student's grade for ";
cout << "Test 1: ";
cin >> test1 [count];
cout << endl << "Enter student's grade for Test 2: ";
cin >> test2 [count];
count++;
}
}
return count;
}

void calculateStatistics (int test1 [], int test2 [], int count, int min1, int min2,int max1, int max2, float avg1, float avg2)
{
int i;
int total1;
int total2;
min1 = test1 [0];
min2 = test2 [0];
max1 = test1 [0];
max2 = test2 [0];

for (i = 1; i <= count; i++)
{
if (test1 [i] < min1)
min1 = test1 [i];
}
{ if (test2 [i] < min2)
min2 = test2 [i];
}
{ if (test1 [i] > max1)
max1 = test1 [i];
}
{ if (test2 [i] > max2)
max2 = test2 [i];
}
{
total1 += test1 [i];
}
{
total2 += test2 [i];
}

avg1 = (total1 + test1 [0])/count;
avg2 = (total2 + test2 [0])/count;
}

void outputStudentRecords (int stuID [], int test1 [], int test2 [], int count,
int min1, int min2, int max1, int max2, float avg1,
float avg2)
{
cout << "Student ID \t Test 1 \t Test 2\n";
cout << "--------------------------------------------";

for (int i = 0; i <= count; i++)
{
cout << stuID [i] << "\t\t";
cout << test1 [i] << "\t";
cout << test2 [i] << endl;
}

cout << "Lowest: \t\t" << min1 << "\t" << min2 << endl;
cout << "Highest: \t\t" << max1 << "\t" << max2 << endl;
cout << "Average: \t\t" << setw(3) << avg1 << "\t" << avg2;
}
Well you have quite a few uninitialized variables which would give you a lot of junk.

avg1, avg2, count, max1, max2, min1, min2 in main

and...

total1, total2 in calculateStatistics

You are just creating them and then trying to use them without assigning them an initial value.

example:

int total1
.....

total1 += test1[i]
Last edited on
You say that you have finally compiled your program and then you go on to show that you cannot compile it?

What are the names of your source files and what was the exact command that you did use to compile it?
no, it compiled. my program would not execute when I entered the ./a.out command.

when i compiled it i just typed in g++ [my filename]

That's what I've been using for the few things I have created, and it'll let me know when something is awry, and if it is not it just does nothing, and that's how I know it's ok.

mobotus has some good tips that I should certainly correct. I'm pretty new at this and have been writing this, banging my head against the wall for about 8 hours, so there may be more simple mistakes. I'm just not of what all could possibly be wrong.

any more tips or suggestions would be welcome. thanks, guys.
mobotus

i tried initializing all of those variables, same problem. compiles fine, permission denied when i execute using ./

same error when using gcc filename -o a.out

I notice in that error message a lot about ifstream and it says later on something about my readStudentFile function. Could something there be the problem? Using file streams is something that we have covered the least and I have never used it before.
Your compile line is probably wrong or needs changing to make sure it links the required libraries.

gcc filename -o a.out


to something like:

c++ filename.cpp -o a.out

g++ filename.cpp -o a.out

gcc filename.cpp -o a.out -l stdc++


In main, first initialize count to 0 if you haven't done so already. Then you'll want to use the return values of readStudentFile() and inputStudentRecords() like so:
1
2
count += readStudentFile(*stuff*);
count += inputStudentRecords(*stuff*);


Also you need a file to input from called "grades.txt". You want the file to be plain text which you edit via Notepad, as .doc files contain formatting info and other stuff that can confuse your program.

There may be other issues, haven't checked.

Edit: Also you can put your code in "code" tags to make it more readable in the future when posting here.
Last edited on
Topic archived. No new replies allowed.