|
 |
10-10-2004, 05:34 PM
|
#1
|
|
Variable Bitrate
Join Date: May 2004
Posts: 380
|
C++ Programming Help
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 or ICQ me, that would be great It's all very basic stuff, but I'm just a newbie to programming.
AIM: morgan196
ICQ: 43581290
|
|
|
|
|
|
Advertisement
|
Sponsored links
|
10-10-2004, 06:22 PM
|
#2
|
|
Variable Bitrate
Join Date: May 2004
Posts: 380
|
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=='.' && 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.
|
|
|
10-10-2004, 06:34 PM
|
#3
|
|
Raw Wave
Join Date: Jan 2004
Location: SilverSpring Maryland
Posts: 2,957
|
I won't tell you exactly how to do it, but I'll give you your first clue, first convert you double for dollars into cents represented as an integer. It should then be an easier problem to solve. YOu just have to figure out how many cents are in a dollar then and how many remaining cents you have after you subtract out the whole dollars.
Hint: The modulus operator (%) is your friend.
|
|
|
10-10-2004, 06:45 PM
|
#4
|
|
Super Moderator
Join Date: Apr 2004
Location: USA
Posts: 4,578
|
Are you asking how your function can return two seperate values? As in this case, two integers?
__________________
AMD XP 2600+/512MB RAM/120GB hard drive
Opus 150W/DVD/GPS/7" Lilliput TS/802.11g/Bluetooth
Installed.
-GPSSecure- - GPS Tracking
-AltTabber2.2.2- - Handy touchscreen utility.
|
|
|
10-10-2004, 06:57 PM
|
#5
|
|
Variable Bitrate
Join Date: May 2004
Posts: 380
|
This is what I'm going to do:
void readMoney(int &dollars, int ¢s)
Now I just have the problem of trying to input as one variable and break it down into 2... man, I hate doing this >:/
|
|
|
10-10-2004, 07:00 PM
|
#6
|
|
Variable Bitrate
Join Date: May 2004
Posts: 380
|
I can't get the cents to work right...
My code so far:
#include <iostream>
#include <stdlib.h>
using namespace std;
void readMoney(int &dollars, int ¢s)
{
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=='.' && 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;
}
|
|
|
10-10-2004, 07:17 PM
|
#7
|
|
Maximum Bitrate
Join Date: Oct 2003
Location: Toronto, Canada
Posts: 485
|
'0' is a character. I dont think you want to perform arithmetic with ints and chars
__________________
----------------------
VIA Epia II
256MB 266DDR Ram
90GB Wester Digital
Bluetooth/Deluo GPS
Lilliput 7" Touchscreen
----------------------
Mazda 6 Carputter > Click Me!
|
|
|
10-10-2004, 08:00 PM
|
#8
|
|
Low Bitrate
Join Date: Jul 2004
Location: Rancho Cucamonga, CA
Posts: 64
|
I think you are making this harder than it looks once you check to see if the first character is a dollar sign then you should be able to call your getdouble function and it can do the rest of the work of getting the number input and converting.
Hope this helps
__________________
EPIA MII-1200, 512 MB Ram, Seagate 7200 RPM 160GB hard drive, Orinoco Gold with 5.5 dB external antenna, Lilliput 7" TS, DVD-ROM, M2-ATX, Rikaline 6010 GPS, Running Frodo Player
My Truckputer
|
|
|
|
Sponsored links
|
|
Advertisement
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:25 PM.
| |