Thread: how to convert integers into strings.

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    15

    how to convert integers into strings.

    Hi, I am having a problem to convert integers into strings. For example, (I got this one from the book) it first input $1920.85, and then it converts into One thousand nine hundred twenty and 85 cents. How can I do that? Can anyone guide me how to do this, and show some simple examples? Thank you.

    By the way, here is the question:
    Write a program that displays a simulated paycheck
    The program should ask the user to enter the date, the payee's name, and the amount of the check (as a floating point number). It should then display a simulated check with the dollar amount spelled out, as shown here:

    Date: 12/04/2011


    Pay to the order of: Charlie Fontuli $1920.85

    One thousand nine hundred twenty and 85 cents

    Be sure to format the numeric value of the check in fixed point notation with two decimal places of precision. Be sure the decimal place always displays, even when the number is zero or has no fractional part. Use either C-strings or string class objects in this program.

    Input validation: Do not accept negative dollar amounts, or amounts over $10,000

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First off, your title is unclear. In the most direct sense, a numeric value can be converted to a string of number characters.

    Code:
    float x = 1234.56;
    char y[8] = "1234.56";
    
    printf("%.2f\n%s\n",x,y);
    
    // output:
    //
    // 1234.56
    // 1234.56
    Also, your example shows floating point values, not integers.

    A better title might have been "How can I write out numeric values in words?"

    ---

    Second of all, it is considered bad form here (and against the forum Homework Policy) to dump an assignment without showing any attempts to code it on your own.

    How can I do that? Can anyone guide me how to do this, and show some simple examples?
    The questions you ask are vague and unlikely to get a meaningful response. I recommend reading How To Ask Questions The Smart Way.

    ---

    Think about the assignment, and write down on paper how you might go about solving it. Only then should you begin to write code. When you hit a snag, post the code you have written so far with specific questions about the problems you are facing. You are much more likely to get a helpful response if you heed this advice.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    >_<

    I hate when homework or examples delve poorly into localization and internationalization issues.

    Soma

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    Hi sorry about that, I just confused on those things and I don't know how to write it down in sentences. (ay yes, I should show the code earlier. I am just too lazy to do the copy and past codes.)
    Here is the code:
    Code:
    .
    #include<iostream>
    #include<cstring>
    #include<iomanip>
    usingnamespace std;
    
    int main()
    {
    constint NAME_SIZE = 20; // Size of an array.
    constint DATE_SIZE = 20; // Size of the date array.
    constint WAGE = 7; // Size of the wage array.
    char payDate[DATE_SIZE]; // To hold the size of pay date input.
    char payeeName[NAME_SIZE]; // To hold the size of payee's name input. 
    char input[WAGE]; // To hold the size of dollar amount input.
    double payCheck;
        
    cout << "This program can display a simulated paycheck.\n";
    cout << "Enter the pay date in (mm/dd/yy): ";
    cin.getline(payDate, DATE_SIZE);
    cout << "Enter the payee's name: ";
    cin.getline(payeeName, NAME_SIZE);
    cout << "Enter the dollar amount in the form nnnn.nn: ";
    cin.getline(input, WAGE);
    payCheck = atof(input); // Convert to double.
    
    // Input Validation.
    
    while (payCheck <= -0 || payCheck > 9999.99)
    {
    cout << "This program does not accpet negative dollar amounts, "
    << "or amounts over $10000.\n";
    cout << "Please enter a valid number again: ";
    cin >> payCheck;
    }
    // Formating the numeric output.
    
    cout << setprecision(2) << fixed << showpoint;
    cout << right << setw(45) << "Date: " << payDate << endl;
    cout << endl << endl;
    cout << "Pay to the Order of: " << payeeName << setw(16) << "$" << payCheck << endl;
    cout << endl;
     
    system ("PAUSE");
    
    return 0;
    }
    
    .
    Last edited by inobProgram; 07-16-2012 at 06:44 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by inobProgram View Post
    Hi sorry about that, I just confused on those things and I don't know how to write it down in sentences. (ay yes, I should show the code earlier. I am just too lazy to do the copy and past codes.)
    Of course you do.
    One thing you must know is that a computer is dumb, so you have to explain to it in simple instructions how to achieve things. But your brain does not need that.
    Anyway, what you should do it just think about how you would do it yourself with pen and paper if you were given such a problem. That mostly explains the logical structure your program needs to follow. After that, it's just a matter of syntax, and that, we can help you with.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    15
    Okay, thanks for your tips ^_^. I will try that to do some programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hot to convert Strings to Integers
    By brian_90 in forum C Programming
    Replies: 4
    Last Post: 04-08-2011, 07:32 PM
  2. convert alphabet to integers
    By anaer0bic in forum C Programming
    Replies: 5
    Last Post: 10-09-2010, 08:05 PM
  3. Strings as integers
    By unknown_ in forum C Programming
    Replies: 3
    Last Post: 03-19-2010, 09:40 PM
  4. Convert array of characters in file to integers
    By millsy5 in forum C Programming
    Replies: 4
    Last Post: 10-02-2009, 11:41 AM
  5. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 09:40 PM

Tags for this Thread