• 11-18-2005
wolverine123
Hey guys i have a problem with my code here the problem is that
the program should read in a text string containing a list of numbers separated by + or - and output the sum. The program should also tell how many positive numbers and how many negative (subtracted) numbers there are.
Example:
input: .. 8.4 + 2.6 - 3.5 + 1
output: result = 8.5, positive numbers: 3, negative numbers: 1.
I used strings in it and used "atof" to use numbers in strings too but i dont know how to calculate the result. cos the user has to input where he wants the plus and minus should be. Please anyhelp would be great.
• 11-18-2005
SlyMaelstrom
atof() is difficult to use to manipulate strings because don't forget you have to make sure you get the whole number before you convert it to a float or you have to manipulate your float mathematically while you're changing it.

Your real key here should be the spaces. This should be your delimiter for each value in the string. Read one character at a time until you reach a space and consider that whole an single value. Then you decide whether it's a number or a operator.

Also, depending on how well you program, you could consider converting the eqution to postfix notation. Then getting that output would be simple.

Post some code so we have an idea of what you've done so far.
• 11-18-2005
Shamino
Sorry has nothing to do with post, responding to his sig...

1600x1200 4tw!
• 11-18-2005
wolverine123
Code:

```#include <iostream.h> #include <string.h> #include <stdlib.h> int main () {   int i;   char n [20];   cout<<"Input: ";   cin.getline (n,20,'\n');   i = atoi (n);     cout<<"Value entered is "<<n<<"and its square is "<<(n[256])*(n[256]);   return 0; }```

I have done this for an integer first. i can get it to cout what i have written but i dont know how to code it to give me a result. I know wat has to be done, like it should keep on writting integers until it gets to a + or - sign then it should calculate but i dont know how to code this one.. PLEASE HELP .
• 11-18-2005
SlyMaelstrom
n[256]?
• 11-18-2005
wolverine123
sorry for the n[256] i actually declared the 20 also as 256.