Sunday, July 12, 2009

Can someone help me with a c++ program please?

hey i have this program thats supposed to read in info about students from a file organize and print it out, then read in some more info and calculate the gpa and print that out as well.





I have the program done, but for some reason it doesnt work, when i run it it outputs this:





record #0 from readdata:


from SetAll





it prints that out 20 times from numbers 0-19, the only error that the compiler gives me is


warning C4244: '=' : conversion from 'double' to 'int', possible loss of data





Im going to post my program in chunks since it spretty big someone help plz





#include%26lt;iostream%26gt;


#include%26lt;string%26gt;


#include%26lt;fstream%26gt;





using namespace std;





class Course{


string name;


char grade;


int hours;


public:


Course(){


name="";


grade=' ';


hours=0;


}


Course(string n, char g, int h){


name=n;


grade=g;


hours=h;


}

Can someone help me with a c++ program please?
*looking up*


That is nice of you to try to help this guy, but you are providing Java solutions to a C++ problem. Doh!





EDIT:





This is fairly hopeless right now. If you look closer, you have pasted a lot of your code with lines that are truncated. (Search for the lines ending in "..."). I made a quick attempt to copy and paste your program, but it won't even compile (of course) as posted. If you address this there is a strong possibility that I will be able to help you fix this once and for all.





Edit #2 - Too funny! I offer to give a guy help, and someone gives me a "thumbs down". Beautiful!


Oh, and looking upwards again...


You consider it anal when I question when a guy puts out code in C++, and Java solutions that aren't necessarily even pertinent to his problem were provided? It is my opinion that you might be dwelling on the compiler "warning" that he incurred, which might not even be remotely related to his *problems*. He is apparently suffering far worse complications than some loss of precision from what he printed out. It looks like he is printing empty gibberish without any real results at this point. You are correct in what you have written, but this doesn't seem to be his worst issue from what he has written.


By the way, no offense or personal malice toward the guy on top, OK? I can now see that you are competent and not just some rube that copied and pasted from Sun's website.
Reply:DISCLAIMER: I use Java syntax and code in my answer, but it should be relatively the same in C++ (except for when i use parseDouble(). I write this disclaimer because some people are anal (*cough* the answerer below me *cough*). I know all too well that the program is in C++. Not stupid.





***************





In general, the way to fix this is to find the line of code throwing the error and cast (as an integer) the double variable you're attempting to assign to an integer. The rule of thumb here is that you can't assign an int to a double without some modification, or without a loss of precision.





Like this... (the (int) is where I'm doing the casting)





int test = (int) variable





(where variable is any double number).





Example:





THIS IS WRONG.


*******************


double testDouble = 2.3;


int test = testDouble;


*******************





THIS IS RIGHT.


*******************


double testDouble = 2.3;


int test = (int)testDouble;


*******************





You might also have to do something more complex, like





*******************


double testDouble = 2.3;


int test = Int.parseInt(testDouble);


*******************





(that was just an example, in that case, you wouldn't need to use parseInt(), a (int) caste would be sufficient.)





It all depends. Basically, though, you need to get the left side (integer) to be of the same as the right side. The way you have it, the right side is too complex for a much less complex integer, explaining why the compiler is yelling at you.

covent garden

No comments:

Post a Comment