C++ Programming Help

egeekial

Member
Jan 1, 2002
164
0
0
Ok, I need some help with my C++ homework. I'm not asking anyone here to do it for me... that would defeat the purpose of taking the class. I just need a little bit of help understanding and doing some things. If someone could AIM, ICQ, or PM me, that would be great :D It's all very basic stuff, but I'm just a newbie to programming.

AIM: morgan196
ICQ: 43581290
 

egeekial

Member
Jan 1, 2002
164
0
0
It's a bit lengthy... but here it goes.

Here is the problem:
Using the getDouble function as an example write your own function readMoney, which reads amount of dollars and cents from the user. The user input should be in the form:
$123.34
That is, it should start with the dollar sign, and then it should have the amount of dollars, then the decimal point, and then 2-digit amount of cents. It should end with any space symbol or a dot. Your function should return 2 integer values (one for dollars and another for cents).



Here is getDouble:
#include <iostream.h>
#include <ctype.h>
#include <stdlib.h>


// function: getDouble
// goal: The function reads character by character from the
// standard input (cin) and converts them into a floating
// point number. It stops when it reads any character
// but +, -, decimal point or a digit.
// As the result it returns the number read.
double getDouble(void)
{
char ch;
int sign = 1;
double res = 0;
double factor = 10;

cin.get(ch); // read the very first symbol from the user
// check if the first symbol is a sign
if( ch=='-' || ch=='+' ){
if( ch == '-' ) sign = -1;
cin.get(ch); // read the next symbol
}

// repeat the loop while it's a correct symbol
while( isdigit(ch) || (ch=='.' &amp;&amp; factor>0.1) ){
if( isdigit(ch) ){
if( factor > 0.1 ){
// we haven't read the decimal point yet
res = factor * res + (ch - '0');
}
else{
// we have read the decimal point by this time
res += (ch - '0') * factor;
factor /= 10;
}
}
else{ // we just read the decimal point
factor = 0.1;
}
cin.get(ch); // read the next symbol
}

return res*sign;
}


int main(void)
{
double num;

cout<<"Enter a floating point number ";
num = getDouble(); // using the new function to read the user's input
cout<<"You entered: "<<num<<endl;

return 0;
}




My question is how do I output dollars and cents from the function as separate variables.
 

mattg1981

Senior member
Jun 19, 2003
957
0
76
I have a massive (1000+ lines) program which I wrote in java that does exactly what your looking for (and much more ... mine is more assignments related such as x = 200.3; y = x * 3.2; etc. But it reads the number as a string and then converts it. I was not allowed to use the function Double.getValue(), so I had to manually convert the strings to doubles also).

Anyways, since it is java, you should be able to convert it very easily to c++. You could look at just the portions that you needed/wanted to and see how I did it. LMK if you want it and I will send it to you.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> Your function should return 2 integer values (one for dollars and another for cents).
> My question is how do I output dollars and cents from the function as separate variables.

There are 2 common ways to do this:

1. passing by reference
bool getMoney (int *d, int *c ) ; // sets d,c returns FALSE if invalid

2. creating a class that holds 2 integers and returning a temporary object of that class for assignment, or a pointer to an object, to be freed by the caller.

CMyMoneyClass getMoney( )

Without being in the class I have no idea which is "right" for what you've been taught so far.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I don't know much about c++ but for such a simple return type, wouldn't a struct be a better idea than an entire class?

egeekial: I didn't see you accounting for the '$' anywhere in your code.
 

egeekial

Member
Jan 1, 2002
164
0
0
Originally posted by: kamper
I don't know much about c++ but for such a simple return type, wouldn't a struct be a better idea than an entire class?

egeekial: I didn't see you accounting for the '$' anywhere in your code.

That code is the "example". It's not mine...

This is what I'm going to do:
void readMoney(int &amp;dollars, int &amp;cents)

Now I just have the problem of trying to input as one variable and break it down into 2... man, I hate doing this >:/
 

egeekial

Member
Jan 1, 2002
164
0
0
I can't get the cents to work right...

My code so far:

#include <iostream>
#include <stdlib.h>

using namespace std;

void readMoney(int &amp;dollars, int &amp;cents)
{
char money;
double factor = 10;

cout<<"Enter amount of money$: ";

cin.get(money); // read the very first symbol from the user
// check if the first symbol is a dollar sign

if( money == '$' ) cin.get(money); // read the next symbol
else cerr<<"You did not enter a dollar sign ($).";

// repeat the loop while it's a correct symbol
while( isdigit(money) || (money=='.' &amp;&amp; factor>0.1) ){
if( isdigit(money) ){
if( factor > 0.1 ){
// we haven't read the decimal point yet
dollars = factor * dollars + (money - '0');
}
else{
// we have read the decimal point by this time
cents = (money - '0') * factor;
cout<<cents;
factor /= 10;
}
}
else{ // we just read the decimal point
factor = 0.1;
}

cin.get(money); // read the next symbol
}

cout<<"Amount in the function: "<<dollars<<"."<<cents<<endl;
}

int main(void)
{
int dollars=0, cents=0;

readMoney(dollars, cents);
cout<<"Amount in the main: "<<dollars<<"."<<cents<<endl;


system("PAUSE");
return 0;
}
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
when the reply window pops up...see the button that says attach code? couldn't be easier...