Ok so I am stuck it's homework so I am not looking for anyone to do it for me i just need some help getting my mind on the right track.

what i am doing is pretty much having the user enter a string of "$234,324,324.66" and have it print out 234324324.66 as a long double

so my thought process is this
I have the user enter in the money into a char array
parse the char array
and any numbers 1-9 or decimal are put it in a string
then use _atold() to convert the string into a long double
pretty much need help with the parsing the char array i seem to have the process on the tip of my brain and have been searching for answers but am running around in circles

any help muchly appreciated.

Do something like this:

Assume the user inputs something like this, "234,324,324.66". Then you can do the following,

1) Replace all instance of ',' with ' '. That is replace a comma with space.
2) Load the string into a stringstream object
3) Read the money one by one.
Here is a sample program.

string input =  "234,324,324.66";
input = replaceAll(input,',',' '); //replace all instance of comma with space.
stringstream stream(input);
float val = 0.0f;
//extract value, that is convert the string into a floating value
while(stream >> val){
 cout << val << endl;
}

Note if your input is like this "$234,324,324.66" then you can just remove the '$' and work on from there

you could better select* all numbers and save them into a string. then convert the string to a float with atof.

*there's a function called 'strpbrk'

firstperson will i have to use another header for your example code? and could you explain the stringstream
teo236 I was thinking of something behind those lines but i still need the decimal but thanks for the function

ok so i swore i've done something like this before probably thinking of java though
where you go through the array box by box(sorry that's how i think of it) checking to see if it's 1-9 or a . and if it is copy it into another char array or if possible a string

to copy array to anther array i've figured out how but that's an exact copy

or could i completely do this all with strings forgoing the char arrays and just going through the string char by char and only putting 1-9 or . into another string?

>> firstperson will i have to use another header for your example code?
>> ok so i swore i've done something like this before probably thinking of java though

You WERE thinking of Java. There is no replaceAll in C++. You have to write your own. Since you have to write your own, you can use string stream if you want, but I thing regular old "find" and "erase" will strip the characters easily enough. I like stringstreams too, but I'm not sure here is the proper place. "find" and "replace" are both in the string library. No need for character arrays/C style strings.

string input =  "234,324,324.66";
    int pos;
    while((pos = input.find(',', 0)) != string::npos)
    {
        input.erase(pos, 1);
    }

>> or could i completely do this all with strings forgoing the char arrays

Yes. See above.

>> and just going through the string char by char and only putting 1-9 or . into another string?

Yes, but again, see above. No need for a second string. To strip the dollar sign, just add a second loop and replace that comma with a dollar sign.

Here is a complete sample, instead of rolling your own replaceAll use std::replace_if algorithm.

#include <algorithm> //for replace_if algorithm
#include <sstream>  //for stringstream object
#include <cctype> //for isspace function

//function checks is the passed argument is a comma
bool isComma(const char c){ return c == ','; }

int main(){
 //the sample string we are going to demonstrate this demo on
 string sample = "124,567,789.123";

 cout << "Sample string = " << sample << endl;

 //remove all  instance of ',' with ' '
 std::replace_if(sample.begin(),sample.end(),isComma,' ');

 cout << "Adjusted string = " << sample << endl;

 //extract all values into a float from sample string
 stringstream stream( sample ); //give the stream a string to work with
 float val = 0; //holds the value that will be extract into from out 'stream'

 //while there are things left to extract and there is no extraction failure, extract the value in the stream into our variable val
 while( stream >> val ){
  cout << "Extracted value =  " << val << endl;
 }

 return 0;
}

>> Here is a complete sample, instead of rolling your own replaceAll use std::replace_if algorithm.


How do you propose getting 124567789.123 into val? val contains 789.123 after the loop is over. You have that pesky white space, so it takes three >> operations, then somehow you need to combine all three into one number. It's far easier to get rid of the commas altogether rather than replacing commas with spaces.

Huh? Oh wait I though he wanted each value separated by ',' into a different value. My mistake then, misread his post.

You can use pretty much what firstPerson suggested but get rid of the dollar sign and commas with one function. And replace_if by itself doesn't actually delete the characters you do not want, it just moves them to the end of the string, so use erase() to trim the string and then call atof() to convert the string to a double.

#include <iostream>
#include <algorithm>

using namespace std;

bool notWanted( char in )
{
	if( in == ',' || in == '$' )
		return true;
	return false;
}

int main()
{
	string str = "$123,345,567.12";
	double val = 0;

	str.erase( remove_if(str.begin(),str.end(), notWanted), str.end() );
	val = atof(str.c_str());

	cout << fixed << val << endl; //use fixed otherwise it will output in scientific notation

	return 0;
}

I always chringe when I see a predicate coded like this

bool notWanted( char in )
{
	if( in == ',' || in == '$' )
		return true;
	return false;
}

when it could be more concise

bool notWanted( char in ){
 return in == ',' || in == '$'	
}

or if you have a lot of identifiers:

bool notWanted(char in){
 return std::string(",$abcdegfhijklmnopqrstuvwxyz").find(std::tolower(in)) != std::string::npos;
}

I always chringe when I see a predicate coded like this

Why? Sure, you can make it more concise, but it doesn't strike me as cringe worthy.

Why? Sure, you can make it more concise, but it doesn't strike me as cringe worthy.

I guess, since its not really harming anything. But idk why, its just does. Maybe not cringe but more frustrated.

Never really thought about doing it that way but that kinda seems like code that you would use and not show people that are just learning C++ since it just adds complexity to something they don't really understand in the first place.

For example when I was just starting to get into programming I saw people using a '?' with if statements and I had no clue what was going on, especially when variable names aren't 100% clear.

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.