I cant figure out what is wrong with this code

These are the instructions


and this is my code

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

using namespace std;
void Get_Info(string[],int[][3]);
void Get_Average(int[][3]);
void Prt_GradeBook(string[],int[][3],int[]);
int MakeUps(int[][3]);
int Grades[5][3],average[5];
string Students[5];
int main()
{
    
    Get_Info(Students,Grades);
    Get_Average(Grades);
    Prt_GradeBook(Students,Grades,average);
    cout<<"There are"<<MakeUps(Grades)<<"Makeup(s) to arrange. "<<endl;
    
    system("pause");
    return 0;
}

void Get_Info(string[5],int[5][3])
{
int i, j;
ifstream infile;
infile.open("Cosc 117 Test Results.txt");
while(infile)
{
infile>>Students[i];
}
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
infile>>Grades[i][j];
}
}
infile.close();
}

void Get_Average(int[5][3])
{
int i,j,sum;
for(i=0;i<5;i++)
{
sum=0;
for(j=0;j<3;j++)
{
sum+=Grades[i][j];
}
average[i]=sum/3;
}
}
void Prt_GradeBook(string[5],int[5][3],int[5])
{
int i, j;
cout<<"Name"<<setw(10)<<"Test #1"<<setw(10)<<"Test #2"<<setw(10)<<"Test #3"<<endl;
for(i=0;i<5;i++)
{
j=1;
cout<<Students[i]<<setw(10)<<Grades[i][j]<<setw(10)<<Grades[i][j+1]<<setw(10)<<Grades[i][j+2]<<endl;
}
}

int MakeUps(int[5][3])
{
int count=0,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
if(Grades[i][j]==0)
count++;
}
}
return count;
}


The application just stops working
like the case with infinite loops but i don't see anything wrong with it.

Please help me

Dani AI

Generated

Brief diagnosis and follow‑ups (years‑later readers): the original crash came from undefined behavior when reading input — an index was used before it was initialized and the code relied on while(infile)/stream state in a way that allowed invalid memory access. Moving the arrays into main and giving functions proper parameter names (as later did) fixed the immediate symptom, but the program can still be made much more robust and maintainable.

A safer input pattern (uses C++ containers and a clear parse loop):

#include <vector>
#include <array>
#include <string>
#include <fstream>

struct Student { std::string name; std::array<int,3> scores; };

std::vector<Student> loadGrades(const std::string &path) {
  std::vector<Student> out;
  std::ifstream in(path);
  if (!in) return out;                // fail‑fast if file missing
  Student s;
  while (in >> s.name >> s.scores[0] >> s.scores[1] >> s.scores[2]) {
    out.push_back(s);
  }
  return out;
}

Compute an average as a floating value to avoid integer truncation:

double avg = (s.scores[0] + s.scores[1] + s.scores[2]) / 3.0;

Practical tips and best practices:

  • Always check if (!infile) after open() and validate each extraction operation.
  • Initialize loop counters and avoid using while(infile) with extraction inside; prefer while (in >> ...) or bounded for loops.
  • Avoid globals for data; prefer std::vector/std::array/struct and pass by reference or return containers. This echoes ’s point.
  • Use compiler warnings (-Wall -Wextra) and sanitizers (-fsanitize=address,undefined) and step through with a debugger as suggested.
  • Keep code formatted and indented (as said) and replace system("pause") with portable alternatives.

These changes prevent out‑of‑bounds access, make errors visible early, and produce clearer, safer code for later maintenance.

Recommended Answers

All 9 Replies

can you run it through a debugger? VS 200x you use the 'step through' feature, on a *nix platform (including mac OSX) you would use gdb.

Try to see what part of the code it's getting stuck in...

could I have a sample of the input file for my purposes? I'd like to run it with the same set of circumstances as you have... (only I'm using g++ instead)

Otherwise, it prints nicely for me, I did comment out the system (pause) though...

Also, how do these methods get the values of the parameters? I see

int main()
{
    
    Get_Info(Students,Grades);
    Get_Average(Grades);
    Prt_GradeBook(Students,Grades,average);
    cout<<"There are"<<MakeUps(Grades)<<"Makeup(s) to arrange. "<<endl;
    
    system("pause");

but where are you getting input to pass off to the methods in the main body?
I might be missing it, It's getting late over here...

all of your function definitions do not name the variables that you are passing to them. You are also declaring the variables that you are passing to your functions globally. You don't need to have any function parameters if you are only using global variables but I suspect that your are supposed to be declaring the variables in main and then passing them to your functions. there also seems to be some accessing of array elements that are out of bounds but its hard to tell without any indention in your code.

also, your getInfo function clearly gets the information from the file, but your function returns void... so how would the other functions be able to get the information to process from the getInfo function?

He is using global variables bradonrunyon.

I see it now... thanks...

i figured it out guys
this is my code

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

using namespace std;
void Get_Info(string Students[],int Grades[][4]);
void Get_Average(int Grades[][4],int average[]);
void Prt_GradeBook(string Students[],int Grades[][4],int average[]);
int MakeUps(int Grades[][4]);


int main()
{int Grades[5][4],average[5];
    string Students[5];
    
    Get_Info(Students,Grades);
    Get_Average(Grades,average);
    Prt_GradeBook(Students,Grades,average);
    cout<<"There are "<<MakeUps(Grades)<<" Makeup(s) to arrange. "<<endl;
    
    system("pause");
    return 0;
}

void Get_Info(string Students[],int Grades[][4])
{
int i, j;
ifstream infile;
infile.open("Cosc 117 Test Results.txt");

for(i=0;i<5;i++)
{infile>>Students[i];

for(j=0;j<3;j++)
{
infile>>Grades[i][j];
}
}

}

void Get_Average(int Grades[][4],int average[])
{
int i,j,sum;
for(i=0;i<5;i++)
{
sum=0;
for(j=0;j<3;j++)
{
sum+=Grades[i][j];
}
average[i]=sum/3;
}
}
void Prt_GradeBook(string Students[],int Grades[][4],int average[])
{
int i, j;
cout<<"Name"<<setw(10)<<"Test #1"<<setw(10)<<"Test #2"<<setw(10)<<"Test #3"<<setw(10)<<"Average"<<endl;
for(i=0;i<5;i++)
{
j=0;
cout<<left<<setw(10)<<Students[i]<<setw(10)<<Grades[i][j]<<setw(10)<<Grades[i][j+1]<<setw(10)<<Grades[i][j+2]<<average[i]<<endl;

}
cout<<endl;
}

int MakeUps(int Grades[][4])
{
int count=0,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
if(Grades[i][j]==0)
count++;
}
}
return count;
}

and this is my infile
Appleton 92 88 72
Baker 63 73 57
Closer 89 0 92
Demsey 90 89 100
Eker 84 92 73

Thanks for responding guys

Glad you solved it. Do yourself a favor and indent. It makes things WAYYYY easier. NetBeans and Visual Studio both have code formatters. You'll spot things so much faster!

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.