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
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
Well, the best I can do is to use sprintf (since std::string lacks a format function, useless class):
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.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: %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; }
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.
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 >_<
Sure, so is the code working?
yes it isbut not having luck with the 2 decimal points >_<
That looks like a good start, how's it working?Originally Posted by lifeisevil
And why do they have to make it so complicated? Why not just a darn member function? Sheesh, CString is so much more easier.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++.
mm it gives me this error when I compile it `sprintf_s' undeclared (first use this function) , doI declare it as double?
>> 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.
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"
Ok. It's a simple mistake. For cout you want to use << not >>.
Also, check your spelling for the second error.