![]() |
| ||
| class Hi guys I'm stll playing around with classes. I have some simple class, the program itself works but when I enter the value for your_account as a negative number it doesn't pass the right numbers to the private variables. Why is that? How may I fixed it Thanks
|
| ||
| Re: class void Money::input (istream& ins) cin is a global istream object that is predeclared for you. You don't need to pass an istream object reference to this function. Just use cin within the function as desired. You don't give first_character a value before you try to compare it to '-' using the equals operator. This is wrong. first_char will have an unknown value under these circumstances, and it will probably never be '-' based on chance. Assuming user input is something like: 100.00 then this line ins>>dollar_amount>>decimal_point>>cent_amount is going to cause the istream object to either go into a fail state or go into an infinite loop when it encounters the decimal point. Either way the program is doomed to fail unless the problem is corrected. My advise would be to change dollar_amount to type double and forget about all the hassle you are creating for yourself, unless this is an assignment and the current program structure is required. |
| ||
| Re: class this is not an assignment. I'm just playing around ,trying to do many things on classes. I just trying to find a way to do excutly as I did but in a correct way. thanks anyway |
| ||
| Re: class Hi again. I think I found a good way to approach the problem. I think :) . Look at the code:
What that function return is the number I entered : -34.56 th eoutput was : -34.560.00 Now I have a new question. How may I delete the last for characters including the dot. thanks |
| ||
| Re: class Never mind. What a stupid ass I am :). I got it. Thanks |
| ||
| Re: class Actually one more question I did that so far:
what i'm loosing right now is just the first digit of the positive number. How may I add that lost number which is a character to the dollar_amount so it will hold an whole number. thanks |
| ||
| Re: class >>what i'm loosing right now is just the first digit of the positive number. How may I add that lost number which is a character to the dollar_amount so it will hold an whole number My first advice is to redo the class by changing the member variable types so you don't have to go through all this other hassle. If, for whatever reason you wish to persist in your current approach, then I would convert the char to a string, convert the integer to a string, append the integer string on to the char string and then convert the concatenated string back to an integer type. To do this I first suggest looking into use of string streams. However, since you're using conio.h anyway, I believe you could use itoa() (which I believe is in most versions of conio.h) to convert the integer value to a string, and strtol() or atoi() (or similar function) to turn the concatenated string back to integer value. |
| All times are GMT -4. The time now is 7:17 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC