C++ gurus check in :)

Jay59express

Senior member
Jun 7, 2000
481
0
0
Hey guys, got a little question here.
Here is the deal, I am writing a constructor called polynomial that accepts as its sole parameter a char* list. Included in this list is a variable amount of coefficients and there exponent.
example:
polynomial::polynomial(char* p) {...}
It would get called in a way like this: polynomial("2.4 5 4 5.6 4.5 0 1.2 2.3") = 2.4^5, 4^5.6, 4.5^0, 1.2^2.3

I have 2 data members,
float coeff, exp;
Now, I want to be able to read in the first "word" in the char string as coeff, then the next as exp, then repeat until p == NULL.

I think it should be something like this:
while( p != NULL ) {
// Read in the first "word" as the float variable, coeff. Probably going to need an atof( )
// Read in the 2nd "word" as exp, same as above;
// Put into a linked list (this is not important in regards to my trouble)
}

So does anyone have any idea how to read from my char* stream into a float variable?
Thanks!!!
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Build a little string / char[] from the big string for each "word," then run atof() on the little string.

I assume you know how to copy characters between 2 strings and how to "move" p?
 

alisajid

Member
Jun 29, 2001
194
0
0
Check out <strstream>

Why couldn't you use istrstream to read floating point numbers from a string just like cin can be used to read from the console?
 

dimagog

Junior Member
Feb 18, 2002
9
0
0
What you need to do is copy from one char* to another up to the nearest space and then update the char* pointer to point to the character after the string.
I think there is a function which does just that. Look into strtok() function. It looks for a token in a string.

Another way to accomplish your tasd woud be to use

char*dummy;
i = 0;
for (dummy = p; *p != ' '; p++)
i++;
// this sets i to be the offset of the first space character in your p string

//now you may be able to say:
char number = p;
//or:
char* number
memcopy(number, p, i) // copy i characters from p to number
//note, wherever I put "i". you may actually have to use i + 1 because of NULL character at the end

I'm not 100% sure, but I hope this gives some ideas.

See if you can help me with my ?? See "2 questions"
 

dimagog

Junior Member
Feb 18, 2002
9
0
0
sorry, it's kinda late and i noticed I've made a few blunders
the loop should read:

for (dummy = *p; dummy != ' '; dummy++)
i++;

and then:

char number = p;
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
If you have a token function it will be cleaner

Psuedo example


string input_string
string coef, exp
float f_coef, f_exp
loop
while input_string NOT NULL loop
input_string := token(0x20, // separator - blank
coef); // parsed token is returned in this variable
input_string := token(0x20, // separator - blank
exp); // parsed token is returned in this variable
f_coef = atof(coef)
f_exp = atof(exp)

// stuff it into list

end loop
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
ANSI-C of course supports tokens, and therefore C++ does too.
The function you are looking for is strtok().

Basically, it does like this:

char value[10]; //make this dimension large enough for your biggest numbers


value = strtok (yourstring, ' '); //USE single quotes, as a character is expected. alt: value=strtok(yourstring, 0x020);
while (value != NULL)
{
coeff = atof(value);
value = strtok (NULL, ' ');
if (value == NULL) Put an error message here, as there is no matching exponent
exp = atof(value); //maybe just atoi here, dunno if you want floating exponents
value = strtok (NULL, ' ');
}


There.