| | |
class
![]() |
•
•
Join Date: Jan 2007
Posts: 67
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> using namespace std; class Money { public: Money (long dollars,int cents); Money (long dollars); Money ( ); void input (istream& ins); void output (ostream& out); private: long dollar_amount; int cent_amount; void check (long dollars,int cents); void Money::check(long dollars); double Money::get_balance ( ); }; int main ( ) { Money your_account,my_account(10,9),our_account; cout<<"\nYour account is equall to: "; your_account.output(cout); cout<<"\nMy account is equall to: "; my_account.output(cout ); your_account.input (cin); your_account.output(cout); getch(); return 0; } Money::Money (long dollars,int cents) { dollar_amount=dollars; cent_amount=cents; check(dollar_amount,cent_amount); } Money::Money (long dollars) { dollar_amount=dollars; cent_amount=0; check (dollar_amount); } void Money::check(long dollars,int cents) { if (dollars<0||cents<0) { cout<<"Illigal values"; getch(); exit (1); } } Money::Money ( ) { dollar_amount=0; cent_amount=0; } void Money::check(long dollars) { if (dollars<0) { cout<<"Illigal values"; getch(); exit (1); } } void Money::output (ostream& out) { out.setf(ios::fixed); out.setf(ios::showpoint); out.precision(2); out<<get_balance( ); } double Money::get_balance ( ) { return (dollar_amount+(cent_amount*0.01)); } void Money::input (istream& ins) { cout<<"\n\nYou are going to set up the amount for your_account"<<endl; cout<<"Enter the amount to your_account: "; bool negative; char first_character; char decimal_point; if (first_character=='-') { negative=true; ins>>first_character; } else negative=false; if(negative) { ins>>dollar_amount>>decimal_point>>cent_amount; dollar_amount=-dollar_amount; cent_amount=cent_amount; } else ins>>dollar_amount>>decimal_point>>cent_amount; }
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
C++ Syntax (Toggle Plain Text)
void Money::input (istream& ins) { cout<<"\n\nYou are going to set up the amount for your_account"<<endl; cout<<"Enter the amount to your_account: "; bool negative; char first_character; char decimal_point; if (first_character=='-') { negative=true; ins>>first_character; } else negative=false; if(negative) { ins>>dollar_amount>>decimal_point>>cent_amount;
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.
•
•
Join Date: Jan 2007
Posts: 67
Reputation:
Solved Threads: 0
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
. Look at the code: C++ Syntax (Toggle Plain Text)
void Money::input (istream& ins) { cout<<"\n\nYou are going to set up the amount for your_account"<<endl; cout<<"Enter the amount to your_account: "; bool negative; char first_character; char decimal_point; int first_number,second_number,all_cents; if (first_character=='-') negative=true; else negative=false; if(negative) first_number=-first_number; ins>>first_number>>decimal_point>>second_number; cout<<first_number<<"."<<second_number; }
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
•
•
Join Date: Jan 2007
Posts: 67
Reputation:
Solved Threads: 0
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
I did that so far:
C++ Syntax (Toggle Plain Text)
void Money::input (istream& ins) { cout<<"\n\nYou are going to set up the amount for your_account"<<endl; cout<<"Enter the amount to your_account: "; bool negative; char first_character; char decimal_point; int first_number,second_number,all_cents; ins>>first_character; if (first_character=='-') negative=true; else negative=false; if(negative) { ins>>first_number>>decimal_point>>second_number; //cout<<first_number<<"."<<second_number; dollar_amount=-first_number; cent_amount=-second_number; } if(isdigit(first_character)) { ins>>dollar_amount>>decimal_point>>cent_amount; //second_number=static_cast<int>(first_character); } }
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
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
>>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.
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.
![]() |
Similar Threads
- java and using observable class (Java)
- Accessing functions from base class (C)
- Circle class (C++)
- "missing storage-class or type specifiers" error (C++)
- first math class in uni (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: What Next?
- Next Thread: Using functions with arrays
| Thread Tools | Search this Thread |
api array based binary bitmap build c++ c++intmain() c/c++ char class classes client code coding compile console conversion copyanyfile count counttheoccurenceofanintegerinthe10inputs delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption environment error file forms fstream function functions game givemetehcodez graph gui homeworkassignment homeworkhelp homeworkhelper i/o iamthwee ifstream input int integer java jni lib linkedlist linker loop looping loops map math matrix memory multiple multipledimensionarray news node numbers output pointer problem program programming project python radix random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video vtk win32 windows winsock wordfrequency wxwidgets






