Thread: Need help on Reading from a file

  1. #76
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    ooo yes I do remember the include <iomanip> but I am pretty sure she never showed us how to use it for decimal points and she didnt really cover much on that because she was in a hurry -_- i'm looking it up in my book now

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

  3. #78
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, the best I can do is to use sprintf (since std::string lacks a format function, useless class):

    Code:
    	char output_string[100];
    	if (show_gender == 'm')
    	{
    		averagem = mgpa / mcounter; // Average GPA
    		sprintf_s(output_string, 100, "The average for the male gpa is: &#37;0.2f.\n", averagem);
    		cout << output_string;
    	}
    	else if (show_gender == 'f')
    	{
    		averagef = fgpa / fcounter; // Average GPA. Divide the total GPA by the number of entries found in the file.
    		sprintf_s(output_string, 100, "The average for the female gpa is: %0.2f.\n", averagef);
    		cout << output_string;
    	}
    Of course, I also had to restort to C-style strings because, again, std::string lacks any functions for getting a buffer from it and using it for the format (AFAIK anyway). Useless class.
    It's the best I can suggest. Take it as a last restort only. Although sprintf_s is quite handy in formating strings. You should look it up sometime.
    Well, any C++ programmer should learn to use a Format function since it's quite useful when formating strings and it used far more often than cout.
    Last edited by Elysia; 10-24-2007 at 06:07 PM.

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

  5. #80
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by lifeis2evil View Post
    When I add this code here, it gives me the f average is -1 something and does the same for if I pick m, is that the wrong spot to put it? i'm pretty sure thats after it opens and before it gets read

    Code:
        inData.open("input.txt");
        outData.open("outData.txt"); //under this stuff
        
    
        inData.open("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;
    	}
    Sigh. You already opened it once, no need for it twice.
    Other than that, it's in the right place.
    Make sure your input file is in place too.

  6. #81
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    o I didn't see that, lol sorry its just that I'm really tired so my brain is working a lot slower than usual >_<

  7. #82
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, so is the code working?

  8. #83
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    yes it is but not having luck with the 2 decimal points >_<

  9. #84
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by lifeisevil
    is it?
    Code:
    cout >> fixed >> showpoint;
    cout >> setprecison(2)
    That looks like a good start, how's it working?

  10. #85
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    On the contrary, formatting is done via stringstreams, which can be safer and more powerful than a format function. Manipulators, while somewhat cumbersome, are available for the formatting and should be preferred in C++.
    And why do they have to make it so complicated? Why not just a darn member function? Sheesh, CString is so much more easier.

  11. #86
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    mm it gives me this error when I compile it `sprintf_s' undeclared (first use this function) , doI declare it as double?

  12. #87
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> mm it gives me this error when I compile it `sprintf_s' undeclared (first use this function) , doI declare it as double?
    Do not use sprintf_s. Go back to the iomanip stuff, that's what your instructor wants you to use.

    >> And why do they have to make it so complicated? Why not just a darn member function? Sheesh, CString is so much more easier.
    I removed that comment to avoid another thread tangent. In short, a stringstream is safer. Fewer bugs that are difficult to track down also makes it "easier" IMO.

  13. #88
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    well lol for the 1st code I posted it said
    no match for 'operator>>' in 'std::cout >> std::fixed'
    `setprecison' undeclared (first use this function)
    expected `;' before "if"

  14. #89
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    I've never seen sprintf befores >_<

  15. #90
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok. It's a simple mistake. For cout you want to use << not >>.

    Also, check your spelling for the second error.

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