Question: Well I have to input a number correctly with comma's and a decimal. My input would be 243,111.11 and the output would come out as 2,43,,111.11
Do I have to create additional for loops statements for each digits and check for comma's/decimal? I almost scrapped this code for attempting to try switch statements. All I seek if if i'm on the right track, and any advice/tip is appreciated. Thus far:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

int main() {
    using namespace std;

    char myStr[256];    
    cout << "Enter Any integers ";
    cin.getline(myStr,256);


    int numDigits = strchr(myStr, '.') - myStr;
    int i, distance;

    for(i=0; myStr[i] != '\0'; i++) {
        putchar(myStr[i]);
        distance = numDigits - i - 1;
        if(distance > 0 && distance%3 == 0) putchar(',');

    }

    return 0;
}

Recommended Answers

All 10 Replies

How do you get that output from that input??? Your teacher must have given you some instructions on how to do it because the example you posted makes no sense.

Do I have to create additional for loops statements for each digits and check for comma's/decimal?

If you want to do it manually, yes. However, input streams already support this feature by imbuing the stream with a locale that can recognize a thousands separator. The default "C" locale won't, but if you use the current locale (represented by a name with an empty string), you can read numbers using your system's locale settings:

#include <iostream>
#include <locale>
#include <sstream>
#include <stdexcept>
#include <string>
#include <typeinfo>

using namespace std;

template <typename T>
T parse_formatted(const string& s, locale loc)
{
    stringstream parser(s);
    T result;

    // Set input formatting to the specified locale
    parser.imbue(loc);

    // Attempt to extract a value
    if (!(parser >> result)) {
        throw std::invalid_argument("Unrecognized format for " + string(typeid(T).name()));
    }

    return result;
}

int main() try
{
    string line;

    if (getline(cin, line)) {
        // Parse values using the current system locale
        double value = parse_formatted<double>(line, locale(""));

        cout << "Raw: " << fixed << value << '\n';
    }
}
catch (std::exception& ex) {
    cerr << ex.what() << '\n';
}
commented: Nice example +9

Thanks for thre replies. As for my teacher, I can say with certainty that he did not give us any example on how to start off with this project, where as my java prof actually does help our lab projects.

This code is about C-String processing. where I Prompt the user to enter a value as a C string in the form: xxx,xxx,xxx,xxx,xxx.xx, where x can be any digit. Anywho, i'll still rework on this code, and always appreciate any tips/help.

Edit: thanks for that code information, along with your explanation about it

Still doesn't explain how you get a crazy looking string like 2,43,,111.11 from 243,111.11. Why so many commas??

Still doesn't explain how you get a crazy looking string like 2,43,,111.11 from 243,111.11. Why so many commas??

He's not conditionally ignoring commas and always treating them as digits. The distance calculation is also off due to including commas in it rather than just digits, so commas are misplaced.

Since people have to enter the commas in the number from the keyboard, what's the purpose of the program? Just to verify the commas are in the right place?

I'd wager that the ultimate goal is to extract a double from the string, but I'll defer to the OP to confirm my assumption.

Yes, to extract a double from the string(I think atof function converts a string to a double). So a valid input from the user would be "240,000.00". My code, if I input the exact same number the output it just misplacesa lot of comma's. So i'm going to have to work on each digit. Was not sure if I was heading in the right direction, so just wanted advice/tips. Thanks for the replies, I will continuosly rework my loops.

If that's the goal, you might consider simply removing all commas from the string rather than trying to parse and validate it as a double. Then you can use strtod to do the validation and conversion.

commented: For helping/tips +2

Alright thanks, will take your advice in consideration and do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.