Hey, I'm trying to get my program to run correctly. I can get the black screen to come up after compiling, but it does not say "Press any key to continue...". I'm reading from input file and want to see results in an output file. My ultimate goal is to get it to run, so I can see the results in my outfile.. My code is here:

[LIST=1]
[*]#include <iostream>
[*]#include <fstream>
[*]#include <iomanip>

[*]using namespace std;

[*]void openFiles(ifstream& inData, ofstream& outData);
[*]void initialize(int& fcounter,int& mcounter,double& gpa,double& msum,double& fsum);
[*]void sumGrades(int fcounter,int mcounter,double gpa,double msum,double fsum,ifstream& inData,ofstream& outData);
[*]void averageGrade(int fcounter,int mcounter,double gpa,double msum,double fsum,double& femaleaverage,double& maleaverage);
[*]void printResults(ofstream& outData,int fcounter,int mcounter,double gpa,double msum,double fsum,double femaleaverage,double maleaverage);

[*]int main()
[*]{
[*]    
[*]    ifstream inData;
[*]    ofstream outData;
[*]    
[*]    outData<<fixed<<showpoint;
[*]    outData.precision(2);
[*]     
[*]    double gpa;
[*]    int mcounter;
[*]    int fcounter;
[*]    double msum;
[*]    double fsum;
[*]    char gender;
[*]    double femaleaverage;
[*]    double maleaverage;
[*]    
[*]    openFiles(inData,outData);
[*]    initialize(fcounter,mcounter,gpa,msum,fsum);
[*]    outData<<"Processing grades.\n";
[*]    sumGrades(fcounter,mcounter,gpa,msum,fsum,inData,outData);
[*]    averageGrade(fcounter,mcounter,gpa,msum,fsum,femaleaverage,maleaverage);
[*]    printResults(outData,fcounter,mcounter,gpa,msum,fsum,femaleaverage,maleaverage);
[*]   
[*]    inData>>gender>>gpa;
[*]   
[*]    inData.close();
[*]    outData.close();
[*]    
[*]    
[*]    system ("PAUSE");
[*]    return 0;
[*]    
[*]    
[*]}

[*]    
[*]    void openFiles(ifstream& inData, ofstream& outData)
[*]    {
[*]    inData.open ("Project4.txt");
[*]    outData.open ("Project4_output.txt");
[*]    }
[*]    
[*]    void initialize(int& fcounter,int& mcounter,double& gpa,double& msum,double& fsum)
[*]    {
[*]         
[*]         int malecounter = 0;
[*]         int femalecounter = 0;
[*]         double malesum = 0;
[*]         double femalesum = 0;
[*]         double gpa1 = 0;
[*]    }     
[*]         
[*]    void sumGrades(int fcounter,int mcounter,double gpa,double msum,double fsum,ifstream& inData,ofstream& outData)
[*]{
[*]    
[*]    char gender;
[*]    while (inData)
[*]    {
[*]     switch (gender)
[*]     {
[*]            
[*]    case 'm':
[*]    msum += gpa;
[*]    mcounter++;
[*]    break;
[*]     
[*]    case 'f':
[*]    fsum += gpa;
[*]    fcounter++;
[*]    break;
[*]     }
[*]    }
[*]}    
[*]    void averageGrade(int fcounter,int mcounter,double gpa,double msum,double fsum,double& femaleaverage,double& maleaverage)
[*]    {
[*]    femaleaverage = ( fsum / fcounter );
[*]    maleaverage = ( msum / mcounter );
[*]    }
[*]    
[*]    void printResults(ofstream& outData,int fcounter,int mcounter,double gpa,double msum,double fsum,double femaleaverage,double maleaverage)
[*]    {
[*]    outData<<"Sum female GPA = "<<fsum<<endl;
[*]    outData<<"Sum male GPA = "<<msum<<endl;
[*]    outData<<"Female count = "<<fcounter<<endl;
[*]    outData<<"Male count = "<<mcounter<<endl;
[*]    outData<<"Average female GPA = "<<femaleaverage<<endl;
[*]    outData<<"Average male GPA = "<<maleaverage<<endl;
[*]    }
[/LIST]

Recommended Answers

All 13 Replies

Thanks for using code tags, but the code you posted contains no carriage returns '\n', and that makes it impossible to copy.

Learn to use your compiler's debugger so that you can step through the problem one line at a time and see what it is doing.

I didn't understand the above post. Here is my updated code. I am still having the same problem. My output comes up, but it comes up blank. I want it to say.. "Press any key to continue..."

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

using namespace std;

void openFiles(ifstream& inData, ofstream& outData,char& gender,double& gpa);
void initialize(int& fcounter,int& mcounter,double& msum,double& fsum);
void sumGrades(int fcounter,int mcounter,double msum,double fsum,ifstream& inData,ofstream& outData,char gender,double gpa);
void averageGrade(int fcounter,int mcounter,double msum,double fsum,double& femaleaverage,double& maleaverage);
void printResults(ofstream& outData,int fcounter,int mcounter,double msum,double fsum,double femaleaverage,double maleaverage);

int main()
{
     
    int countMale;
    int countFemale;
    double sumMaleGPA;
    double sumFemaleGPA;
    double averageFemaleGPA;
    double averageMaleGPA;
    char genders;
    double gpas;
    
    ifstream inData;
    ofstream outData;
    
    outData<<fixed<<showpoint;
    outData.precision(2);
    
    openFiles(inData,outData,genders,gpas);
    initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
    outData<<"Processing grades.\n";
    sumGrades(countFemale,countMale,sumFemaleGPA,sumMaleGPA,inData,outData,genders,gpas);
    averageGrade(countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);
    printResults(outData,countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);
   
    inData.close();
    outData.close();
    
    
    system ("PAUSE");
    return 0;
    
    
}

    
       
    void openFiles(ifstream& inData, ofstream& outData,char& gender,double& gpa)
    {
    inData.open ("Lab20.txt");
    outData.open ("Lab20_output.txt");
    
    inData>>gender>>gpa;
    }
     
    
    
    void initialize(int& fcounter,int& mcounter,double& msum,double& fsum)
    {
         
    int mcount = 0;
    int fcount = 0;
    double malesum = 0;
    double femalesum = 0;
  
    }  
      
         
    void sumGrades(int fcounter,int mcounter,double msum,double fsum,ifstream& inData,ofstream& outData,char gender,double gpa)
{
    inData>>gender>>gpa;
    while (inData)
    {
     switch (gender)
     {
            
    case 'm':
    msum += gpa;
    mcounter++;
    break;
     
    case 'f':
    fsum += gpa;
    fcounter++;
    break;
     }
     }
}   
    
    void averageGrade(int fcounter,int mcounter,double msum,double fsum,double& femaleaverage,double& maleaverage)
    {
    
    femaleaverage = ( fsum / fcounter );
    maleaverage = ( msum / mcounter );
    
    }
    
    void printResults(ofstream& outData,int fcounter,int mcounter,double msum,double fsum,double femaleaverage,double maleaverage)
    {
    outData<<"Sum female GPA = "<<fsum<<endl;
    outData<<"Sum male GPA = "<<msum<<endl;
    outData<<"Female count = "<<fcounter<<endl;
    outData<<"Male count = "<<mcounter<<endl;
    outData<<"Average female GPA = "<<femaleaverage<<endl;
    outData<<"Average male GPA = "<<maleaverage<<endl;
    }

Some functions which modify parameters are NOT getting the reference parameters they should be.

Can I get an example of it. Do you mean the "&" sign?

Look at your code, see how it works.

Can I get an example of it. Do you mean the "&" sign?

One example is sumGrades(), inside that function you modify fcounter, mcounter, msum and fsum. However, none of these are passed in by reference (&) so the sumGrades' code does not have the intended impact. So you should change to

void sumGrades(int & fcounter,int & mcounter,double & msum,double &fsum,ifstream& inData,ofstream& outData,char gender,double gpa)

There may be other such functions too, I did not look too closely.

One correction yet to your SumGrades() function, you probably want to change

inData>>gender>>gpa;
    while (inData)

to

while (inData>>gender>>gpa)  // loop while there is data for gender and gpa

I put pass by reference on all of them to make sure everything was passed. The blank screen comes back up, but it just disappears a second afterwards. The only result I am getting is the sumFemaleGPA in the output when I open the output file. I'm not getting results from anything else. Here is my updated code:

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

using namespace std;

void openFiles(ifstream& inData, ofstream& outData);
void initialize(int& fcounter,int& mcounter,double& msum,double& fsum);
void sumGrades(int& fcounter,int& mcounter,double& msum,double& fsum,ifstream& inData,ofstream& outData,char gender,double gpa);
void averageGrade(int& fcounter,int& mcounter,double& msum,double& fsum,double& femaleaverage,double& maleaverage);
void printResults(ofstream& outData,int& fcounter,int& mcounter,double& msum,double& fsum,double& femaleaverage,double& maleaverage);

int main()
{
     
    int countMale;
    int countFemale;
    double sumMaleGPA;
    double sumFemaleGPA;
    double averageFemaleGPA;
    double averageMaleGPA;
    char genders;
    double gpas;
    
    ifstream inData;
    ofstream outData;
    
    outData<<fixed<<showpoint;
    outData.precision(2);
    
    openFiles(inData,outData);
    initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
    outData<<"Processing grades.\n";
    sumGrades(countFemale,countMale,sumFemaleGPA,sumMaleGPA,inData,outData,genders,gpas);
    averageGrade(countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);
    printResults(outData,countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);
   
    inData.close();
    outData.close();
    
    
    system ("PAUSE");
    return 0;
    
    
}

    
       
    void openFiles(ifstream& inData, ofstream& outData)
    {
    inData.open ("Lab20.txt");
    outData.open ("Lab20_output.txt");
    
    }
     
    
    
    void initialize(int& fcounter,int& mcounter,double& msum,double& fsum)
    {
         
    int mcount = 0;
    int fcount = 0;
    double malesum = 0;
    double femalesum = 0;
  
    }  
      
         
    void sumGrades(int& fcounter,int& mcounter,double& msum,double& fsum,ifstream& inData,ofstream& outData,char gender,double gpa)
{
    while (inData>>gender>>gpa)
    {
     switch (gender)
     {
            
    case 'm':
    msum += gpa;
    mcounter++;
    break;
     
    case 'f':
    fsum += gpa;
    fcounter++;
    break;
     }
    }
}   
    
    void averageGrade(int& fcounter,int& mcounter,double& msum,double& fsum,double& femaleaverage,double& maleaverage)
    {
         
    femaleaverage = ( fsum / fcounter );
    maleaverage = ( msum / mcounter );
    
    }
   
    void printResults(ofstream& outData,int& fcounter,int& mcounter,double& msum,double& fsum,double& femaleaverage,double& maleaverage)
    {
    outData<<"Sum female GPA = "<<fsum<<endl;
    outData<<"Sum male GPA = "<<msum<<endl;
    outData<<"Female count = "<<fcounter<<endl;
    outData<<"Male count = "<<mcounter<<endl;
    outData<<"Average female GPA = "<<femaleaverage<<endl;
    outData<<"Average male GPA = "<<maleaverage<<endl;
    }

Shouldn't you change the initialize() function to initialize the variables you pass in?
Now you initialize four variables local to the function ...

void initialize(int& fcounter,int& mcounter,double& msum,double& fsum)
{
    int mcount = 0;
    int fcount = 0;
    double malesum = 0;
    double femalesum = 0;
}

The blank screen comes back up, but it just disappears a second afterwards.

That is expected since you are not displaying anything on screen (e.g. by means of cout)

The only result I am getting is the sumFemaleGPA in the output when I open the output file.

What is exactly in your input file?

I got a syntax error saying that the declaration(s) shadows the parameter.

I attached my input file.


Here is my updated code:

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

using namespace std;

void openFiles(ifstream& inData, ofstream& outData);
 void initialize(int& fcounter,int& mcounter,double& fsumGPA,double& msumGPA);
void sumGrades(int& fcounter,int& mcounter,double& msumGPA,double& fsumGPA,ifstream& inData,ofstream& outData,char gender,double gpa);
void averageGrade(int& fcounter,int& mcounter,double& msumGPA,double& fsumGPA,double& femaleaverage,double& maleaverage);
void printResults(ofstream& outData,int& fcounter,int& mcounter,double& msumGPA,double& fsumGPA,double& femaleaverage,double& maleaverage);

int main()
{
     
    int countMale;
    int countFemale;
    double sumMaleGPA;
    double sumFemaleGPA;
    double averageFemaleGPA;
    double averageMaleGPA;
    char genders;
    double gpas;
    
    ifstream inData;
    ofstream outData;
    
    outData<<fixed<<showpoint;
    outData.precision(2);
    
    openFiles(inData,outData);
    initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
    outData<<"Processing grades.\n";
    sumGrades(countFemale,countMale,sumFemaleGPA,sumMaleGPA,inData,outData,genders,gpas);
    averageGrade(countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);
    printResults(outData,countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);
   
    inData.close();
    outData.close();
    
    
    system ("PAUSE");
    return 0;
    
    
}

    
       
    void openFiles(ifstream& inData, ofstream& outData)
    {
    inData.open ("Lab20.txt");
    outData.open ("Lab20_output.txt");
    
    }
     
    
    
    void initialize(int& fcounter,int& mcounter,double& fsumGPA,double& msumGPA)
    {
         
    int fcounter = 0;
    int mcounter = 0;
    double fsumGPA = 0;
    double msumGPA = 0;
  
    }  
      
         
    void sumGrades(int& fcounter,int& mcounter,double& msumGPA,double& fsumGPA,ifstream& inData,ofstream& outData,char gender,double gpa)
{
    while (inData>>gender>>gpa)
    {
     switch (gender)
     {
            
    case 'm':
    msumGPA += gpa;
    mcounter++;
    break;
     
    case 'f':
    fsumGPA += gpa;
    fcounter++;
    break;
     }
    }
}   
    
    void averageGrade(int& fcounter,int& mcounter,double& msumGPA,double& fsumGPA,double& femaleaverage,double& maleaverage)
    {
         
    femaleaverage = ( fsumGPA / fcounter );
    maleaverage = ( msumGPA / mcounter );
    
    }
   
    void printResults(ofstream& outData,int& fcounter,int& mcounter,double& msumGPA,double& fsumGPA,double& femaleaverage,double& maleaverage)
    {
    outData<<"Sum female GPA = "<<fsumGPA<<endl;
    outData<<"Sum male GPA = "<<msumGPA<<endl;
    outData<<"Female count = "<<fcounter<<endl;
    outData<<"Male count = "<<mcounter<<endl;
    outData<<"Average female GPA = "<<femaleaverage<<endl;
    outData<<"Average male GPA = "<<maleaverage<<endl;
    }

>> I got a syntax error saying that the declaration(s) shadows the parameter.
That really seems to be happening there ...

void initialize(int& fcounter,int& mcounter,double& fsumGPA,double& msumGPA)
{
    int fcounter = 0; // a local variable is initialized here, this has no 
                 // effect on the fcounter variable you pass in to the function
    int mcounter = 0;  // same here
    double fsumGPA = 0; // and here
    double msumGPA = 0;  // and here
}

So instead you need to use

void initialize(int& fcounter,int& mcounter,double& fsumGPA,double& msumGPA)
{
    fcounter = 0;
    mcounter = 0;
    fsumGPA = 0;
    msumGPA = 0;
}

Your input data seemed OK.

It worked! It is kind of funny because out of all that time, the program wasn't working because of the shadow parameter thing (I had a few problems with awhile ago). I'm glad you pointed that out to me. Thanks!!

all that time, the program wasn't working because of the shadow parameter thing (I had a few problems with awhile ago).

Hey .. now don't forget that pass-by-reference thing ...

Yeah, i put the pass-by-reference on the initialize and the sumGrades function. I experimented to see what would give me results and what would not, but these two functions work.

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.