The MP3car.com Store The MP3car.com Blog    

Sponsored links

Go Back   MP3Car.com > Mp3Car Technical > General MP3Car Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2004, 05:34 PM   #1
Variable Bitrate
 
egeekial's Avatar
 
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
egeekial is offline   Reply With Quote
Advertisement
 
Advertisement
Sponsored links

Old 10-10-2004, 06:22 PM   #2
Variable Bitrate
 
egeekial's Avatar
 
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.
egeekial is offline   Reply With Quote
Old 10-10-2004, 06:34 PM   #3
Raw Wave
 
god_of_cpu's Avatar
 
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.
__________________
StreetDeck.com Developer (I am Chuck)
Get StreetDeck at http://www.streetdeck.com
The Official StreetDeck Forums have moved, please visit us at http://www.streetdeck.com/forum for official support for Streetdeck.
god_of_cpu is offline   Reply With Quote
Old 10-10-2004, 06:45 PM   #4
Super Moderator
 
xBrady's Avatar
 
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.
xBrady is offline   Reply With Quote
Old 10-10-2004, 06:57 PM   #5
Variable Bitrate
 
egeekial's Avatar
 
Join Date: May 2004
Posts: 380
This is what I'm going to do:
void readMoney(int &dollars, int &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 is offline   Reply With Quote
Old 10-10-2004, 07:00 PM   #6
Variable Bitrate
 
egeekial's Avatar
 
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 &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=='.' && 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;
}
egeekial is offline   Reply With Quote
Old 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!
[iG] is offline   Reply With Quote
Old 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
bbrother is offline   Reply With Quote
Sponsored links
Advertisement
 
Advertisement
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wanting to learn some programming, help me pick a language! RS3RS Software & Software Development 32 10-15-2004 05:38 PM
C++: GUI Programming with >NET Framework [iG] Software & Software Development 3 05-24-2004 07:57 PM
A bit off Topic on Programming xanimal Software & Software Development 11 04-21-2004 09:27 PM
batch file programming LCD's mosh LCD/Display 6 05-19-2002 01:55 AM
Need to start somewhere with Hardware programming 168db Software & Software Development 7 01-11-2002 09:35 AM


All times are GMT -5. The time now is 12:25 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 1999 - 2008 Mp3Car.com Inc.Ad Management by RedTyger
Message Board Statistics