We help IT Professionals succeed at work.

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

ghost8067
ghost8067 asked
on
299 Views
Last Modified: 2012-06-21
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;

}
Comment
Watch Question

Top Expert 2007

Commented:
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);
     }

Author

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;

}
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Works like a charm. Thanks!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.