•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 363,517 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,386 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 389 | Replies: 6
![]() |
•
•
Join Date: Jan 2007
Posts: 61
Reputation:
Rep Power: 2
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
#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,050
Reputation:
Rep Power: 7
Solved Threads: 136
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: 61
Reputation:
Rep Power: 2
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:
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: 61
Reputation:
Rep Power: 2
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:
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,050
Reputation:
Rep Power: 7
Solved Threads: 136
>>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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
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



Linear Mode