Thread: Need help on Reading from a file

  1. #46
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    soo
    Code:
    averagef= fgpa / fcounter;
    averagem= mgpa / mcounter;
    ?

  2. #47
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ahh... that looks good. And it makes sense, too, right?

    Now just output it. Then run your program and see if it gives you the output you expect.

  3. #48
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    hehe thanks, but I think something is wrong maybe in my declaration area? because I get the same answer for m and f

    Code:
    //include section
    #include <iostream>
    #include <conio.h> //for use with getch
    #include <string>  //string data types
    #include <fstream> // in and out data
    
    using namespace std;
    
    
    int main ()
    {
       
        string name,majorname,coursename;
        ifstream inData;
        ofstream outData;
        double gpa=0;
        double mgpa=0;
        double fgpa=0;
        int averagef=0;
        int averagem=0;
        int mcounter=0; 
        int fcounter=0;
        char gender=0;
        char m=0;
        
         
           
        
        inData.open("input.txt");
        outData.open("outData.txt");
        
        //stuff writting out to screen / displayed
        cout<<"Hello there, What is your name?"<< endl;
        getline(cin,name);
        cout<<"Welcome "<<name<<endl;
        cout<<"Please enter your degree your majoring in."<<endl;
        getline(cin,majorname);
        cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl;
        getline(cin,coursename);
        cout<<"What is your gender? Enter m for male or f for female"<<endl;
        cin>>gender;
    
    
    
        
        //Stuff written out to txt file
        outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." 
        " The class course name for there degree major is "<<coursename<< "."<<endl;
    
    
    
     while(inData >> gender >> gpa) // loop to count the males and females in the file
         { 
              
              if(gender=='m')
              {
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
                   
                                  }
               
            else if (gender=='f') 
              {
                   fcounter++; 
                   fgpa=fgpa+gpa; 
                  
              } 
              }
     
           
    
    
    averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file.
    averagem= mgpa / mcounter; // Average GPA
    
              if(gender=='m')
              {
              
                  cout<<"the average for the male gpa is: "<<averagem<<"" <<endl;
                                  }
               
            else if (gender=='f') 
              {
                 cout<<"the average for the female gpa is: "<<averagef<<"" <<endl;
              } 
              
    
    
    
            
            
      
        getch ();
        return 0;
    }

  4. #49
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Let's look at your output code:
    Code:
              if(gender=='m')
              {
              
                  cout<<"the average for the male gpa is: "<<averagem<<"" <<endl;
                                  }
               
            else if (gender=='f') 
              {
                 cout<<"the average for the female gpa is: "<<averagef<<"" <<endl;
              }
    When you use an if/else if, that means only one of those blocks of code will ever run. You're saying you want to the first block of code to run in some cases, and the second block of code to run in other cases. But you're trying to output the averages. Don't you always want to output the average for male and the average for female?

    There shouldn't be any restriction on when those cout lines are executed. I'm guessing that in your head you want the cout<<"the average for the male gpa is: "<<averagem<<"" <<endl; line to run if the gender is male and the cout<<"the average for the female gpa is: "<<averagef<<"" <<endl; line to run if the gender is female. But that's the wrong way to think about it. You should always want both lines to run, because you want to see both averages. You have already separated the male average from the female average by storing them in different variables (averagem and averagef), so there is no need to add the extra ifs.

  5. #50
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    but when i didn't have the if/else, it would show both the male and the female averages, when I asked for the male or for the female

  6. #51
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your assignment says, "Write a program that computes and outputs the average GPA for both females and male students." So don't you want to show both male and female averages?

    If you only want to show one or the other, then the problem is that you are using the gender variable for two different things. You use it to ask the user which gender he or she is. Then you use it later to read in the genders in the file. Whatever the last gender in the file is will be stored into the gender variable, so you can't use that variable to hold the answer the user gave.

  7. #52
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    I think she wants us to show only one average because if not she wouldn't have told us to ask the user if it were male or female?

  8. #53
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    But i'd also like to get only one if possible. But how would I make that work

    Code:
    //include section
    #include <iostream>
    #include <conio.h> //for use with getch
    #include <string>  //string data types
    #include <fstream> // in and out data
    
    using namespace std;
    
    
    int main ()
    {
       
        string name,majorname,coursename;
        ifstream inData;
        ofstream outData;
        double gpa=0;
        double mgpa=0;
        double fgpa=0;
        int averagef=0;
        int averagem=0;
        int mcounter=0; 
        int fcounter=0;
        char gender=0;
        char m=0;
        
         
           
        
        inData.open("input.txt");
        outData.open("outData.txt");
        
        //stuff writting out to screen / displayed
        cout<<"Hello there, What is your name?"<< endl;
        getline(cin,name);
        cout<<"Welcome "<<name<<endl;
        cout<<"Please enter your degree your majoring in."<<endl;
        getline(cin,majorname);
        cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl;
        getline(cin,coursename);
        cout<<"What is your gender? Enter m for male or f for female"<<endl;
        cin>>gender;
    
    
    
        
        //Stuff written out to txt file
        outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." 
        " The class course name for there degree major is "<<coursename<< "."<<endl;
    
    
    
     while(inData >> gender >> gpa) // loop to count the males and females in the file
         { 
              
              if(gender=='m')
              {
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
                   
                                  }
               
            else if (gender=='f') 
              {
                   fcounter++; 
                   fgpa=fgpa+gpa; 
                  
              } 
              }
     
           
    
    
    
    
    
              if(gender=='m')
              {
              averagem= mgpa / mcounter; // Average GPA
              cout<<"the average for the male gpa is: "<<averagem<<"" <<endl;
                                  }
               
            else if (gender=='f') 
              {
               averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file.
               cout<<"the average for the female gpa is: "<<averagef<<"" <<endl;
              } 
              
    
    
    
            
            
      
        getch ();
        return 0;
    }

  9. #54
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your variable gender is used to input what the users wants and later OVERWRITTEN by the internal code that gathers the information. Use a separate variable to store the user input and check that variable later.

  10. #55
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Something like that? or different
    Code:
    //include section
    #include <iostream>
    #include <conio.h> //for use with getch
    #include <string>  //string data types
    #include <fstream> // in and out data
    
    using namespace std;
    
    
    int main ()
    {
       
        string name,majorname,coursename;
        ifstream inData;
        ofstream outData;
        double gpa=0;
        double mgpa=0;
        double fgpa=0;
        double averagef=0;
        double averagem=0;
        int mcounter=0; 
        int fcounter=0;
        char genderFromFile=0;
        char gender=0;
        char m=0;
        
         
           
        
        inData.open("input.txt");
        outData.open("outData.txt");
        
        //stuff writting out to screen / displayed
        cout<<"Hello there, What is your name?"<< endl;
        getline(cin,name);
        cout<<"Welcome "<<name<<endl;
        cout<<"Please enter your degree your majoring in."<<endl;
        getline(cin,majorname);
        cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl;
        getline(cin,coursename);
        cout<<"What is your gender? Enter m for male or f for female"<<endl;
        cin>>gender;
    
    
    
        
        //Stuff written out to txt file
        outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." 
        " The class course name for there degree major is "<<coursename<< "."<<endl;
    
    
    
     while(inData >> gender >> gpa) // loop to count the males and females in the file
         { 
              
              if(gender=='m')
              {
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
                   
                                  }
               
            else if (gender=='f') 
              {
                   fcounter++; 
                   fgpa=fgpa+gpa; 
                  
              } 
              }
     
           
    
    
    
    
    while(inData >> genderFromFile)
    {
              if(gender=='m')
              {
              averagem= mgpa / mcounter; // Average GPA
              cout<<"the average for the male gpa is: "<<averagem<<"" <<endl;
                                  }
               
            else if (gender=='f') 
              {
               averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file.
               cout<<"the average for the female gpa is: "<<averagef<<"" <<endl;
              } 
              }
    
    
    
            
            
      
        getch ();
        return 0;
    }

  11. #56
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by lifeis2evil View Post
    Something like that? or different
    Code:
    //include section
    #include <iostream>
    #include <conio.h> //for use with getch
    #include <string>  //string data types
    #include <fstream> // in and out data
    
    using namespace std;
    
    
    int main ()
    {
       
        string name,majorname,coursename;
        ifstream inData;
        ofstream outData;
        double gpa=0;
        double mgpa=0;
        double fgpa=0;
        double averagef=0;
        double averagem=0;
        int mcounter=0; 
        int fcounter=0;
        char genderFromFile=0;
        char gender=0;
        char m=0;
        
         
           
        
        inData.open("input.txt");
        outData.open("outData.txt");
        
        //stuff writting out to screen / displayed
        cout<<"Hello there, What is your name?"<< endl;
        getline(cin,name);
        cout<<"Welcome "<<name<<endl;
        cout<<"Please enter your degree your majoring in."<<endl;
        getline(cin,majorname);
        cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl;
        getline(cin,coursename);
        cout<<"What is your gender? Enter m for male or f for female"<<endl;
        cin>>gender;// Asking what the user wants to show here. We store it in gender. 
    
    
    
        
        //Stuff written out to txt file
        outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." 
        " The class course name for there degree major is "<<coursename<< "."<<endl;
    
    
    
     while(inData >> gender >> gpa) /* We're reading from the file and storing in gender and gpa... wait, wha??? Did we just store in gender?
    But... but... we used that variable to store the information about what the user wanted to see! We're overwriting important data!*/
         { 
              
              if(gender=='m')
              {
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
                   
                                  }
               
            else if (gender=='f') 
              {
                   fcounter++; 
                   fgpa=fgpa+gpa; 
                  
              } 
              }
     
           
    
    
    
    
    
              if(gender=='m') // Wait, what? What we're reading here isn't what the user wanted to show, but some data read from the file! That's totally wrong!
              {
              averagem= mgpa / mcounter; // Average GPA
              cout<<"the average for the male gpa is: "<<averagem<<"" <<endl;
                                  }
               
            else if (gender=='f') // Wait, what? What we're reading here isn't what the user wanted to show, but some data read from the file! That's totally wrong!
              {
               averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file.
               cout<<"the average for the female gpa is: "<<averagef<<"" <<endl;
              } 
    
    
    
            
            
      
        getch ();
        return 0;
    }
    Ummm... no. I didn't ask to add another loop.
    Look at the code above. I added some comments and highlighted the lines in red to show you the problem.

  12. #57
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you tried it, and if so, did it do what you want it to do?

    I'm a bit suspicious that you loose the original input of gender, and that you are trying to read gender from the file after you have failed to read something from that said file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #58
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Then should I move everything here? or? I really don't know how to fix it :/

    Code:
    while(inData >> gender >> gpa) 
         { 
              
              if(gender=='m')
              {
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
                   averagem= mgpa / mcounter; 
                   cout<<"the average for the male gpa is: "<<averagem<<"" <<endl;
                                  }
               
            else if (gender=='f') 
              {
                   fcounter++; 
                   fgpa=fgpa+gpa; 
                  averagef= fgpa / fcounter;
               cout<<"the average for the female gpa is: "<<averagef<<"" <<endl;
              } 
              }

  14. #59
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sigh. No. You really seem to have no grasp for the language. We already told you that you were overwriting the data you inputted from the user.
    Make a new variable!

    Code:
    //include section
    #include <iostream>
    #include <conio.h> //for use with getch
    #include <string>  //string data types
    #include <fstream> // in and out data
    
    using namespace std;
    
    
    int main ()
    {
       
        string name,majorname,coursename;
        ifstream inData;
        ofstream outData;
        double gpa=0;
        double mgpa=0;
        double fgpa=0;
        double averagef=0;
        double averagem=0;
        int mcounter=0; 
        int fcounter=0;
        char genderFromFile=0;
        char gender=0;
        char show_gender = '\0';
        char m=0;
        
         
           
        
        inData.open("input.txt");
        outData.open("outData.txt");
        
        //stuff writting out to screen / displayed
        cout<<"Hello there, What is your name?"<< endl;
        getline(cin,name);
        cout<<"Welcome "<<name<<endl;
        cout<<"Please enter your degree your majoring in."<<endl;
        getline(cin,majorname);
        cout<<"What the name of the course your taking for your major "<<majorname<<" ?"<<endl;
        getline(cin,coursename);
        cout<<"What is your gender? Enter m for male or f for female"<<endl;
        cin>>show_gender;
    
    
    
        
        //Stuff written out to txt file
        outData<<"Person's name is " <<name<<". And there degree major is "<<majorname<<"." 
        " The class course name for there degree major is "<<coursename<< "."<<endl;
    
    
    
     while(inData >> gender >> gpa) 
         { 
              
              if(gender=='m')
              {
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
                   
                                  }
               
            else if (gender=='f') 
              {
                   fcounter++; 
                   fgpa=fgpa+gpa; 
                  
              } 
              }
     
           
    
    
    
    
    
              if(show_gender=='m')
              {
              averagem= mgpa / mcounter; // Average GPA
              cout<<"the average for the male gpa is: "<<averagem<<"" <<endl;
                                  }
               
            else if (show_gender=='f')
              {
               averagef= fgpa / fcounter;// Average GPA. Divide the total GPA by the number of entries found in the file.
               cout<<"the average for the female gpa is: "<<averagef<<"" <<endl;
              } 
    
    
    
            
            
      
        getch ();
        return 0;
    }
    Look at those red lines and try to understand what I just did. I made a new variable to hold the data.
    Try to do something similar in your own code, to make sure you learn it.

  15. #60
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    ok, did and somewhat get it but I still don't understand why it gives the same answer for both m + f and now its more off than before on the answer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM