program window closes immediately

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2385 Views - Last Post: 30 April 2010 - 08:23 PM Rate Topic: -----

#1 Guest_Guest*


Reputation:

program window closes immediately

Posted 30 April 2010 - 12:31 PM

Hi, I'm trying to finish up some homework but I've hit a snag. The code I have seems to compile properly, no errors or anything, but the output window closes immediately. I don't get the chance to actually input anything. Here's what I have
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std; 

struct studentInfo
{
int id;
int score;
char grade;
};
const int MAX_NUM = 50;
studentInfo data[MAX_NUM];
bool successful = false;
bool readStuData(ifstream&, bool&, studentInfo data[]);
float getAverage(studentInfo data[], float& mean);
void assignGrades(studentInfo data[], float);
void reportResults(studentInfo data[]);

int main(int argc, char *argv[], float average)
{
ifstream inData("C:\\\\Documents and Settings\\My Documents\\scores.txt", ios::in);
readStuData(inData, successful, data);
getAverage(data, average);
cout<<"Student "<<data[0].id<<"test score is "<<data[0].score<<endl;
assignGrades(data, average);
reportResults(data);
return 0;
}

bool readStuData(ifstream& inData, bool& successful, studentInfo data[])
{

int number;


if(inData.fail())
{
cerr << "***ERROR: Cannot open" << inData << endl;
successful = false;
return successful;
return EXIT_FAILURE;
}
else
{
successful = true;
return successful;
inData >> data[0].id >> data[0].score;
number = 0;
while (!inData.eof())
{
if (number=0, number<MAX_NUM, number++)
{ 
inData >> data[number].id >> data[number].score;
number++;
}
}
}
inData.close();
return successful;
}
float getAverage(studentInfo data[], float& mean)
{
float sum = 0.0;
int i;

for (i=0; i<MAX_NUM; i++)
{
sum = data[i].score + sum;
}
mean = sum / MAX_NUM;

return (mean);
}
void assignGrades(studentInfo data[], float average)
{
int i;
for (i=0; i<MAX_NUM; i++)
{
if ((data[i].score <= average + 10) || (data[i].score >= average - 10))
{
data[i].grade = 'S';
}
if (data[i].score > average + 10)
{
data[i].grade = 'O';
}
if (data[i].score < average - 10)
{
data[i].grade = 'U';
}
}
}
void reportResults(studentInfo data[])
{
ofstream out("C:\\\\Documents and Settings\\My Documents\\lab.txt");
out << "TABLE OF STUDENT SCORES"<< setw(4) <<"Student ID #"<< setw(10)<<"Score"<< setw(14)<<"Grade"<< endl;
for (int i=0;i<MAX_NUM; i++)
out << setw(8) << data[i].id << setw(14)<< data[i].score << setw(14)<< data[i].grade << endl;
}


Thanks for any help in advance.

Is This A Good Question/Topic? 0

Replies To: program window closes immediately

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: program window closes immediately

Posted 30 April 2010 - 12:36 PM

From the C++ FAQs Topic
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: program window closes immediately

Posted 30 April 2010 - 12:41 PM

I'm sorry. I forgot to say that I did cin.ignore(); and cin.get(); already. The window stays open but it only displays the EXIT_FAILURE message.
Was This Post Helpful? 0

#4 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: program window closes immediately

Posted 30 April 2010 - 01:08 PM

Well, you should be able to figure this out by yourself, since you seem to have placed error notices in your code. Here's where you get the error:
if(inData.fail())
{
cerr << "***ERROR: Cannot open" << inData << endl;
successful = false;
return successful;
return EXIT_FAILURE;
}


EDIT: The file failed to open for some reason, most likely because the path is incorrect.

This post has been edited by diego_pmc: 30 April 2010 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_Guest*


Reputation:

Re: program window closes immediately

Posted 30 April 2010 - 01:09 PM

I really need help on this and I wanted to make a more relevant title to the problem. Sorry for the duplicate topics. Anyway, I can't figure out why this isn't allowing me to input any data.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std; 

struct studentInfo
{
int id;
int score;
char grade;
};
const int MAX_NUM = 50;
studentInfo data[MAX_NUM];
bool successful = false;
bool readStuData(ifstream&, bool&, studentInfo data[]);
float getAverage(studentInfo data[], float& mean);
void assignGrades(studentInfo data[], float);
void reportResults(studentInfo data[]);

int main(int argc, char *argv[], float average)
{
ifstream inData("C:\Users\Owner\Desktop\scores.txt", ios::in);
readStuData(inData, successful, data);
getAverage(data, average);
cout<<"Student "<<data[0].id<<"test score is "<<data[0].score<<endl;
assignGrades(data, average);
reportResults(data);
cin.ignore();
cin.get();
return 0;
}

bool readStuData(ifstream& inData, bool& successful, studentInfo data[])
{

int number;


if(inData.fail())
{
cerr << "***ERROR: Cannot open" << inData << endl;
successful = false;
return successful;
return EXIT_FAILURE;
}
else
{
successful = true;
return successful;
inData >> data[0].id >> data[0].score;
number = 0;
while (!inData.eof())
{
if (number=0, number<MAX_NUM, number++)
{ 
inData >> data[number].id >> data[number].score;
number++;
}
}
}
inData.close();
return successful;
}
float getAverage(studentInfo data[], float& mean)
{
float sum = 0.0;
int i;

for (i=0; i<MAX_NUM; i++)
{
sum = data[i].score + sum;
}
mean = sum / MAX_NUM;

return (mean);
}
void assignGrades(studentInfo data[], float average)
{
int i;
for (i=0; i<MAX_NUM; i++)
{
if ((data[i].score <= average + 10) || (data[i].score >= average - 10))
{
data[i].grade = 'S';
}
if (data[i].score > average + 10)
{
data[i].grade = 'O';
}
if (data[i].score < average - 10)
{
data[i].grade = 'U';
}
}
}
void reportResults(studentInfo data[])
{
ofstream out("C:\Users\Owner\Desktop\report.txt");
out << "TABLE OF STUDENT SCORES"<< setw(4) <<"Student ID #"<< setw(10)<<"Score"<< setw(14)<<"Grade"<< endl;
for (int i=0;i<MAX_NUM; i++)
out << setw(8) << data[i].id << setw(14)<< data[i].score << setw(14)<< data[i].grade << endl;
}




thanks for any help
Was This Post Helpful? 0

#6 Guest_Guest*


Reputation:

Re: program window closes immediately

Posted 30 April 2010 - 01:16 PM

Quote

Well, you should be able to figure this out by yourself, since you seem to have placed error notices in your code. Here's where you get the error:

if(inData.fail())
{
cerr << "***ERROR: Cannot open" << inData << endl;
successful = false;
return successful;
return EXIT_FAILURE;
}


right, but the program is supposed to return the error message without the right input and I don't get the chance to input anything. Thank you for the help.
Was This Post Helpful? 0

#7 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: program window closes immediately

Posted 30 April 2010 - 01:35 PM

I'm sorry, but I don't see the portion of code that is supposed to get input from the user. What function is it in?

Also, you are misusing return statements. When you return something, the function that returns stops executing:
    } else {
        successful = true;
        return successful;
        ///////////////////////////////////////////////////
        // Everything below this line never gets executed.
        ///////////////////////////////////////////////////
        inData >> data[0].id >> data[0].score;
        number = 0;
        while (!inData.eof())
        {
            if (number=0, number<MAX_NUM, number++)
            {
                inData >> data[number].id >> data[number].score;
                number++;
            }
        }

This post has been edited by diego_pmc: 30 April 2010 - 01:39 PM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: program window closes immediately

Posted 30 April 2010 - 01:38 PM

This is just a bunch of thrown together, copy & pasted code. Here's where the input is:

if(inData.fail())
{
cerr << "***ERROR: Cannot open" << inData << endl;
successful = false;
return successful;
return EXIT_FAILURE;
}
else
{
successful = true;
return successful;
inData >> data[0].id >> data[0].score;
number = 0;



It'll never happen.
Was This Post Helpful? 0
  • +
  • -

#9 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: program window closes immediately

Posted 30 April 2010 - 01:44 PM

You mean the inData >> data[0].id >> data[0].score; line, right? I noticed that, but I though he meant that he wants the user to input something, not to read from the file.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: program window closes immediately

Posted 30 April 2010 - 02:00 PM

Could be you're right. Not entirely clear what's happening here, except that the code I pointed out means nothing will ever get input.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Guest*


Reputation:

Re: program window closes immediately

Posted 30 April 2010 - 07:04 PM

So what would be the best way to fix this? Thank you all for your help so far.
Was This Post Helpful? 0

#12 Guest_Guest*


Reputation:

Re: program window closes immediately

Posted 30 April 2010 - 07:10 PM

Quote

You mean the inData >> data[0].id >> data[0].score; line, right? I noticed that, but I though he meant that he wants the user to input something, not to read from the file.


yes, that is what is supposed to happen.
Was This Post Helpful? 0

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: program window closes immediately

Posted 30 April 2010 - 07:19 PM

Do you not know the result of using return? Better go read your book/notes.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Guest*


Reputation:

Re: program window closes immediately

Posted 30 April 2010 - 07:36 PM

Quote

Do you not know the result of using return? Better go read your book/notes.


Thank you for all the insight and wisdom Mr. Amazing Programmer Guy! I understand you are an expert, apparently, in "Being annoyed with lazy people" but I'm not being lazy. I'm just looking for a little help from people who obviously have more experience/savvy with c++ programming.
Was This Post Helpful? -1

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: program window closes immediately

Posted 30 April 2010 - 07:50 PM

You know, I'm not amazing, not even close, and I would actually be the very last person to ever contend that is the case. There are many people far, far above my level here, dispensing valuable advice throughout the forums.

However, I don't suffer fools gladly, and those that are too lazy to read when given hints as to what their problem is qualify as fools in my book. Go do some research on your own, instead of expecting the people with experience to spoon-feed you the answers to your homework. That's not how people learn.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2