• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

.exe does not keep window open, but VS 2005 does.

The following program works fine except for one last issue. Although I have added a wait to the program, when I execute the .exe file, the window opens and closes. This functionality works fine when running in VS 2005, but not when using the .exe. Any assistance would be appreciated.

Thanks!

Progtram:

/*  This program will read an input file that contains student last name, first name,
      and 3 test scores. The program will outut 3 blocks. The first block will be a
      formatted print of the data from the input file. The program then calculates the
      avaerage for each student and assigns a letter grade according to the prg/410 schedule.
      The second block of output is this data in formatted form. Lastly, the program
      determines the higest student scores and prints them in descending order.
      Exercise Wk4_Ex01.cpp
      @author Jon Schab
      @version 1.0
      @created 10/3/07
*/


#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>


using namespace std;

enum { MAX_STUDENT = 10, MAX_SCORE = 3 };

// struct for input file
struct studentType
{
string studentFName;
string studentLName;
int testScore[MAX_SCORE];
string grade;
};


// file read and check for successful open
void readfile(studentType student[MAX_STUDENT])
{
int x = 0;
ifstream infile;

char filename[]= "students.txt";

infile.open(filename);

if (infile.fail())
{
cout << "File doesn't exist" << endl;
exit(0);
}

// populate input data
while( infile && x < 10 )
{
infile >> student[x].studentFName >> student[x].studentLName >> student[x].testScore[0] >>
student[x].testScore[1] >> student[x].testScore[2];
x++;
}

infile.close();
}


// format and print entire input file
void printfile(studentType* student)
{
cout << "Entire Input File" << endl << endl;
cout <<setw(17)<<left<<"Student Name" <<setw(15)<< "Student Test Scores" << endl;
string name = "";
for (int i=0;i<MAX_STUDENT;i++)
{
 name = student[i].studentLName + ", "+student[i].studentFName;
cout <<setw(17)<<left<<name;
cout << setw(5)<<right<<student[i].testScore[0] << " ";
cout << setw(5)<<right<<student[i].testScore[1] << " ";
cout << setw(5)<<right<<student[i].testScore[2] << " " << endl;

}
cout << endl;
}

void wait()
{
      cin.ignore(100,'\n');
      cout << "\n\nPress Enter to Continue";
      cin.get();
}

void calcgrade(studentType* student)
{
double average;
string grade;
cout << "\nStudents and Grades" << endl << endl;
cout <<setw(17)<<left<<"Student Name" <<setw(15)<< "Test Scores" <<setw(10)<<"Average"<<setw(5)<<"Grade"<< endl;
string name = "";
for (int i=0;i<MAX_STUDENT;i++)
{
average = ((student[i].testScore[0] + student[i].testScore[1] + student[i].testScore[2]) / 3.0);


// convert score to letter grade
if (average >= 95)
grade = "A";
else if (average >= 90)
grade = "A-";
else if (average >= 87)
grade = "B+";
else if (average >= 84)
grade = "B";
else if (average >= 80)
grade = "B-";
else if (average >= 77)
grade = "C+";
else if (average >= 74)
grade = "C";
else if (average > 70)
grade = "C-";
else if (average > 67)
grade = "D+";
else if (average > 64)
grade = "D";
else if (average > 60)
grade = "D-";
else
grade = "F";

name = student[i].studentLName + ", "+student[i].studentFName;
// print out student grade line
cout << setw(17)<<left<<name<<setw(5)<<left<< student[i].testScore[0]
     <<setw(5)<<left<< student[i].testScore[1] <<setw(5)<<left<< student[i].testScore[2]
       <<setw(10)<<left<< fixed << setprecision(2) << average <<setw(5)<<left << grade << endl;

}
}


//This method will bubble sort the student array
//and return the highest student that is required by the highest number.
//So, it will return the highest score obtaining student if 0 is passed.
//The total scores are counted  (all three tests) and thus gradually
//we can compute the sequence of students from higher to lower marks
//in descending order
studentType findHighestScore(studentType* student, int highest){
  //StudentType std;
  //bubble sort
  for (int i=0;i<MAX_STUDENT;i++)
  {
    for (int j=0;j<MAX_STUDENT;j++)
      {
        if( (student[i].testScore[0]+student[i].testScore[1]+student[i].testScore[2]) >=
              (student[j].testScore[0]+student[j].testScore[1]+student[j].testScore[2]))
        {
            studentType st = student[i];
              student[i]= student[j];
              student[j] = st;
        }
      }
  }
   
  return student[highest];
}


//This method will call the findHighestScore(student, index), so when 0
// is passed the top scoring student will be returned, when 9 is passed
//the student with lowest marks will be returned.
void printNamesHavingHighestScore(studentType* student){
  cout<<"\nStudent Names in Descending Order of  Highest Scores\n"<<endl;
      for(int i=0; i<10; i++)
  {
    studentType std = findHighestScore(student, i);
      cout<<std.studentLName<<", "<<std.studentFName<<endl;
      }
}

int main(int argc, char* argv[])

{


studentType student[MAX_STUDENT];
readfile(student);
printfile(student);
calcgrade(student);
printNamesHavingHighestScore(student);
wait();
return 0;

}
0
ghost8067
Asked:
ghost8067
  • 2
  • 2
1 Solution
 
josgoodCommented:
The file is missing.  You have it located with your source files.  When you run the .exe, it is looking for it in the same folder as the .exe.

Make readfile of type bool
     bool readfile(studentType student[MAX_STUDENT])
and then return false instead of exit(0)
     if (infile.fail())
     {
          cout << "File doesn't exist" << endl;
          return false;
     }
and then return true for the normal exit from the function.

In the main,
     if (readfile(student)) {
          printfile(student);
          calcgrade(student);
          printNamesHavingHighestScore(student);
     }

0
 
ghost8067Author Commented:
I tried the code, but when Imade the changes the program stopped displaying the data and just waits for enter and ends.  My code is below.

/*  This program will read an input file that contains student last name, first name,
      and 3 test scores. The program will outut 3 blocks. The first block will be a
      formatted print of the data from the input file. The program then calculates the
      avaerage for each student and assigns a letter grade according to the prg/410 schedule.
      The second block of output is this data in formatted form. Lastly, the program
      determines the higest student scores and prints them in descending order.
      Exercise Wk4_Ex01.cpp
      @author Jon Schab
      @version 1.0
      @created 10/3/07
*/


#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>


using namespace std;

enum { MAX_STUDENT = 10, MAX_SCORE = 3 };

// struct for input file
struct studentType
{
string studentFName;
string studentLName;
int testScore[MAX_SCORE];
string grade;
};


// file read and check for successful open
bool readfile(studentType student[MAX_STUDENT])
{
int x = 0;
ifstream infile;

char filename[]= "students.txt";

infile.open(filename);

if (infile.fail())
{
cout << "File doesn't exist" << endl;
return false;
}

// populate input data
while( infile && x < 10 )
{
infile >> student[x].studentFName >> student[x].studentLName >> student[x].testScore[0] >>
student[x].testScore[1] >> student[x].testScore[2];
x++;
}

infile.close();
}


// format and print entire input file
void printfile(studentType* student)
{
cout << "Entire Input File" << endl << endl;
cout <<setw(17)<<left<<"Student Name" <<setw(15)<< "Student Test Scores" << endl;
string name = "";
for (int i=0;i<MAX_STUDENT;i++)
{
 name = student[i].studentLName + ", "+student[i].studentFName;
cout <<setw(17)<<left<<name;
cout << setw(5)<<right<<student[i].testScore[0] << " ";
cout << setw(5)<<right<<student[i].testScore[1] << " ";
cout << setw(5)<<right<<student[i].testScore[2] << " " << endl;

}
cout << endl;
}

void wait()
{
      cin.ignore(100,'\n');
      cout << "\n\nPress Enter to Continue";
      cin.get();
}

void calcgrade(studentType* student)
{
double average;
string grade;
cout << "\nStudents and Grades" << endl << endl;
cout <<setw(17)<<left<<"Student Name" <<setw(15)<< "Test Scores" <<setw(10)<<"Average"<<setw(5)<<"Grade"<< endl;
string name = "";
for (int i=0;i<MAX_STUDENT;i++)
{
average = ((student[i].testScore[0] + student[i].testScore[1] + student[i].testScore[2]) / 3.0);


// convert score to letter grade
if (average >= 95)
grade = "A";
else if (average >= 90)
grade = "A-";
else if (average >= 87)
grade = "B+";
else if (average >= 84)
grade = "B";
else if (average >= 80)
grade = "B-";
else if (average >= 77)
grade = "C+";
else if (average >= 74)
grade = "C";
else if (average > 70)
grade = "C-";
else if (average > 67)
grade = "D+";
else if (average > 64)
grade = "D";
else if (average > 60)
grade = "D-";
else
grade = "F";

name = student[i].studentLName + ", "+student[i].studentFName;
// print out student grade line
cout << setw(17)<<left<<name<<setw(5)<<left<< student[i].testScore[0]
     <<setw(5)<<left<< student[i].testScore[1] <<setw(5)<<left<< student[i].testScore[2]
       <<setw(10)<<left<< fixed << setprecision(2) << average <<setw(5)<<left << grade << endl;

}
}


//This method will bubble sort the student array
//and return the highest student that is required by the highest number.
//So, it will return the highest score obtaining student if 0 is passed.
//The total scores are counted  (all three tests) and thus gradually
//we can compute the sequence of students from higher to lower marks
//in descending order
studentType findHighestScore(studentType* student, int highest){
  //StudentType std;
  //bubble sort
  for (int i=0;i<MAX_STUDENT;i++)
  {
    for (int j=0;j<MAX_STUDENT;j++)
      {
        if( (student[i].testScore[0]+student[i].testScore[1]+student[i].testScore[2]) >=
              (student[j].testScore[0]+student[j].testScore[1]+student[j].testScore[2]))
        {
            studentType st = student[i];
              student[i]= student[j];
              student[j] = st;
        }
      }
  }
   
  return student[highest];
}


//This method will call the findHighestScore(student, index), so when 0
// is passed the top scoring student will be returned, when 9 is passed
//the student with lowest marks will be returned.
void printNamesHavingHighestScore(studentType* student){
  cout<<"\nStudent Names in Descending Order of  Highest Scores\n"<<endl;
      for(int i=0; i<10; i++)
  {
    studentType std = findHighestScore(student, i);
      cout<<std.studentLName<<", "<<std.studentFName<<endl;
      }
}

int main(int argc, char* argv[])

{
studentType student[MAX_STUDENT];

if (readfile(student)){
printfile(student);
calcgrade(student);
printNamesHavingHighestScore(student);
}
wait();
return 0;

}
0
 
josgoodCommented:
readfile needs to
     return true;
as its last line.

That works on my box.
0
 
ghost8067Author Commented:
Works like a charm. Thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now