Please help

This is a discussion on Please help within the C++ Programming forums, part of the General Programming Boards category; Hey guys i have a problem with my code here the problem is that the program should read in a ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Please help

    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.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    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.
    Last edited by SlyMaelstrom; 11-18-2005 at 07:13 AM.
    Sent from my iPadŽ

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Sorry has nothing to do with post, responding to his sig...

    1600x1200 4tw!
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    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 .

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    n[256]?
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    sorry for the n[256] i actually declared the 20 also as 256.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21