Thread: Need help on Reading from a file

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Shrug. You can always go ahead and propose a C++-orienented solution.

  2. #32
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's not about proposing a solution, it's about helping the OP solve the problem themselves. Especially for a homework assignment, that means using the tools they know and understand.

    If I was lifeis2evil I would ignore everything in this thread since friday.

    >> a for loop or a while loop or a do while loop?
    I gave you a big hint by writing out in english how I thought the loop should go. The english translates to code pretty directly. You want to read while the input is successful. So which type of loop should you add?

    I noticed you've tried a while loop. Good idea, since you are looping while something is true. However, you want to stop when the input fails (which normally happens at the end-of-file). To do that in C++, you put the read directly in the while control:
    Code:
    while (inData >> gender >> gpa)
    Does that make sense? The code (inData >> gender >> gpa) evaluates to true if the read succeeds, and false if it fails. So when you've read all the lines in the file, the read will fail and your while loop will finish, which is what you want.

    So what does your current code look like if you use that for your loop?

  3. #33
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    is this better or do i use the while (inData >> gender >> gpa)? and did i declare everything correctly?

    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, mgpa, fgpa;
        int average, mcounter, fcounter;
        char gender, m, f;
        
         
           
        
        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;
    
    inData >> gender >> gpa;
    
     while(inData) // loop to count the males and females in the file
         { 
             inData >> gender >> gpa;
     
              if(gender=='m'); 
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
               
            else if (gender=='f') 
              
                   fcounter++; 
                   fgpa=fgpa+gpa; 
              } 
               
     
            
      
        getch ();
        return 0;
    }

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can do it either way (if you do it correctly), but I like my way better because it is less code and is a little cleaner.

    In your version, you made a mistake, though. The point is to check inData immediately after you read in data, and before you use that data. In your code, inside the loop you call inData >> gender >> gpa, but then you use the values without checking inData to see if the read was successful. You don't check inData again until the while control, which isn't executed until after you the values. You also ignore the first line because you read in values outside the loop and then again immediately inside the loop. If you're going to do it that way you need to move the inData >> gender >> gpa code to the bottom of the loop.

    Code:
              if(gender=='m'); 
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
               
            else if (gender=='f') 
              
                   fcounter++; 
                   fgpa=fgpa+gpa;
    In this code above, you seem to have the right idea on how to solve the problem. If you came up with it yourself congratulations. However, that code won't work the way you want it to. The structure of your if/else if loop is wrong. You need to remove the semi-colon after the if and add braces around the code that should be executed for each section.

  5. #35
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    so your saying to do this? thank you it was giving me an error in the if else and i wasn't sure why but that made it work. But do I leave this inData >> gender >> gpa; in the loop under the while stuff or its not needed?

    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, mgpa, fgpa;
        int average, mcounter, fcounter;
        char gender, m, f;
        
         
           
        
        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
         { 
             inData >> gender >> gpa;
     
              if(gender=='m')
              {
              
                   mcounter++; 
                   mgpa=mgpa+gpa; 
                   }
               
            else if (gender=='f') 
              {
                   fcounter++; 
                   fgpa=fgpa+gpa; 
              } 
              }
     
            
      
        getch ();
        return 0;
    }

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> But do I leave this inData >> gender >> gpa; in the loop under the while stuff or its not needed?

    It's not needed (and is actually bad because it will cause every other line to be ignored). The code inside the while control actually does the input. I know it's a little confusing to combine those two steps, but it works.

    The way you fixed the if/else if block looks good.

    Now all you have to do is calculate the average after the while loop and output the result. When you do, you'll realize that something is not working correctly. Make sure you always initialize your variables, and that should fix your problem. string and fstream variables are initialized automatically, but ints, doubles and chars will have some random value if you don't give them one to start with. Those random values can throw off your calculations.

  7. #37
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    lol okay if you thought was bad at the loop and if/else stuff. I'm horrible at math stuff unfortunately I have no clue how to start the calculation. can you give me a hint?

    and I think for the declared area you mean this? did i write that right?

    Code:
    int main ()
    {
       
        string name,majorname,coursename;
        ifstream inData;
        ofstream outData;
        double gpa=0;
        double mgpa=0;
        double fgpa=0;
        int average, mcounter, fcounter;
        char gender=0;
        char m=0;
    char f=0;

  8. #38
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int main ()
    {
       
        string name,majorname,coursename;
        ifstream inData;
        ofstream outData;
        double gpa=0;
        double mgpa=0;
        double fgpa=0;
        int average, mcounter, fcounter;
        char gender=0;
        char m=0;
    char f=0;
    That looks good, although you forgot to initialize the variables in red.
    As for the math, average is usually calculated by the total divided by the number of times. In your code, that would be, for example, fgpa / fcounter.

  9. #39
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    mm initialize is do this?
    Code:
    int average=0;
    int mcounter=0:
    int fcounter=0;

  10. #40
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    and like this?
    Code:
    fgpa /= fcounter; 
    mgpa /= mcounter;
    or this
    Code:
    fgpa /= fcounter++; 
    mgpa /= mcounter++;

  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by lifeis2evil View Post
    Code:
    fgpa /= fcounter++; 
    mgpa /= mcounter++;
    That code dividizes correctly and then increases the fcounter/mcounter which is not necessary. The first code is correct.

  12. #42
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    ok so now what would I have to do to display the average :P

  13. #43
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Output it to cout. You should be able to do that.

    Once you've got it working so that it outputs, then worry about stopping the decimal places at two.

  14. #44
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    using cin to get it or what?

    Code:
    fgpa /= fcounter; 
    cin>>averagef;
    mgpa /= mcounter; 
    cin>>averagem;

  15. #45
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> using cin to get it or what?
    Take a step back and think about what you're doing. cin is used for user input. Are you supposed to ask the user what the average is?

    No, you're supposed to calculate it. The way to calculate it is to follow this statement but in code:

    The average is equal to the total gpa divided by the count of how many gpa's were read in.

    The code you used above fgpa /= fcounter can work, but only if you understand what it's doing. If you don't understand, scrap it and try again with the sentence I just gave you.

    Once you've figured out how to calculate the average, then it will be stored in the averagef and averagem variables, and you can output it.

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