Results 1 to 8 of 8

Thread: C++ Programming Help

  1. #1
    Variable Bitrate egeekial's Avatar
    Join Date
    May 2004
    Posts
    393

    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

  2. #2
    Variable Bitrate egeekial's Avatar
    Join Date
    May 2004
    Posts
    393
    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.

  3. #3
    Raw Wave god_of_cpu's Avatar
    Join Date
    Jan 2004
    Location
    SilverSpring Maryland
    Posts
    2,960
    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.

  4. #4
    Super Moderator xBrady's Avatar
    Join Date
    Apr 2004
    Location
    USA
    Posts
    3,687
    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.

  5. #5
    Variable Bitrate egeekial's Avatar
    Join Date
    May 2004
    Posts
    393
    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 >:/

  6. #6
    Variable Bitrate egeekial's Avatar
    Join Date
    May 2004
    Posts
    393
    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;
    }

  7. #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!

  8. #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

Similar Threads

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •