Thread: Need help on Reading from a file

  1. #61
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by matsp View Post
    Since this is the C++ forum, I would suggest that using fread() and related function isn't quite the right thing to do.

    --
    Mats
    last i checked fread is included with all C/C++ compilers. If it wasnt cin/cout would break since most compilers implement those using fread and other functions. C is a subset of C++, ergo anything that is C is also C++. perhaps you mean that fread isnt MFC, which isnt really c++ either, so i submit for everyone consideration that if its written in C/C++ then it IS C/C++.

  2. #62
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    last i checked fread is included with all C/C++ compilers. If it wasnt cin/cout would break since most compilers implement those using fread and other functions. C is a subset of C++, ergo anything that is C is also C++. perhaps you mean that fread isnt MFC, which isnt really c++ either, so i submit for everyone consideration that if its written in C/C++ then it IS C/C++.
    Yes, of course, they are still valid - but someone who is on a course to learn C++ isn't supposed to use "classic" C functions to solve the problem - at least not if you want to get a good "score" for your excercises - and that's probably what the original poster would like in this case - I would personally mark someone on a C++ class as "failed" if they come up with a pure C solution - the fact that it works [or maybe not] would be secondary to showing an understanding of the language.

    If on the other hand, this was about solving the problem "in any form that works", the aforementioned solution is fine. But we have to look at what the poster is asking for, not just find "any solution".

    --
    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.

  3. #63
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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?

    Show your current code. You were on the right track by making the variable genderFromFile, you just didn't implement it correctly.

  4. #64
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    --
    Last edited by lifeis2evil; 10-25-2007 at 04:10 PM.

  5. #65
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Does that code work for you?

  6. #66
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What's actually wrong with the code? I don't understand. I get:
    3.35643 (Female)
    3.33357 (Male)
    They're not the same!

  7. #67
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    hmm when I compile and run it , it gives me -1.#IND for m and when I run it and pick f it gives me the sane answser

  8. #68
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    I'm going to try making another program next week like this to learn it but right now I can't really think cause I haven't really been sleeping, yesterday I slept 1 hr and I've really been trying hard to get this program to work because I really need it >_<

  9. #69
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then there has to be something that breaks your code, either the input file or compiler optimization, because it works fine for me both on debug and release with the input file you mentioned in the beginning of the thread.
    EDIT: Why don't you try the following code and reply with what output you get:

    Code:
    int main()
    {
        string name, majorname, coursename;
        ifstream inData;
        ofstream outData;
        double gpa = 0, mgpa = 0, fgpa = 0, averagef = 0, averagem = 0;
        int mcounter = 0, fcounter = 0;
        char gender = 0, show_gender = '\0';
        //char genderFromFile = 0;
        //char m = 0;
            
    	inData.open("C:\\input.txt");
        //outData.open("outData.txt");
        
        cout << "Hello there, What is your name?" << endl;
        getline(cin, name);
        cout << "Welcome " << name << ".\n";
        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 << " ?\n";
        getline(cin, coursename);
        cout << "What is your gender? Enter m for male or f for female." << endl;
        cin >> show_gender;
    
        //outData << "Person's name is " << name << ". And there degree major is " << majorname << ". The class course name for there degree major is " << coursename << ".\n";
    
    	while(inData >> gender >> gpa) 
    	{
    		cout << "DEBUG INFO: gender = " << gender << ", gpa = " << gpa << endl;
    		if (gender == 'm')
    		{
    		   mcounter++; 
    		   mgpa += gpa; 
    		}
    		else if (gender == 'f') 
    		{
    		   fcounter++; 
    		   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;
    }
    I added debug info to see if your program screws up somewhere, so post the outpost from the console window.
    Last edited by Elysia; 10-24-2007 at 05:38 PM.

  10. #70
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    omg! yes lol I have like a bunch of different program codes cpp files because every time I tried something new I would save it in a new folder and so I made this into a new folder and haden't moved the input file ..Thank you lol

  11. #71
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    sorry >_< lol I tried debugging it too ..then I reread the sentence you posted and then it hit me . danm! I forgot the file >_<

  12. #72
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So, when you put in the input file, does it work?
    EDIT: It might also be a good idea to check if the file is open after trying to open it. That way you check for errors, if the opening failed for some reason:
    Code:
    	inData.open("C:\\input.txt");
        outData.open("outData.txt");
    	if ( !inData.is_open() )
    	{
    		cout << "ERROR: Could not open input file for reading!\n";
    		return 1;
    	}
    	if ( !outData.is_open() )
    	{
    		cout << "ERROR: Could not open output file for writing!\n";
    		return 1;
    	}
    Last edited by Elysia; 10-24-2007 at 05:45 PM.

  13. #73
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    ok now I think this how you format your number to 2 decimal places and show .00?
    changing the declaration part to 0.00 instead of 0? or am I close?

  14. #74
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    yes it worked . should I put that code before the if else stuff?

  15. #75
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> changing the declaration part to 0.00 instead of 0? or am I close?
    Not really. You need to use an io manipulator in <iomanip>. Have you learned about any of those?

    >> should I put that code before the if else stuff?
    Checking the is_open() function should be done after opening the file and before using 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