I have a program in were you enter numbers. each different # set you enter goes into an array ( section[] )
but now what I'm trying to do is add 2 different sections together.

for(int x = 0; x < stoppoint; ++x)
{
                       track = track + 1;
 if(section[x] == "*")
 {
    answer = section[track - 1] + section[track + 1];

               }
      }     
      cout << answer;
      
      }

as you see, im trying to add the numbers before the "*" and after the "*". but its not working. what happens is, if the #'s before and after the * are 1 and 3, it will display 13. I want it to display 4.

what am I doing wrong? thanks.

I think that the problem is that section is a char array and you have
the problem that you have to convert them to integers first, otherwise they have their ascii value. e.g. 1 is 49 (I think)

So isolate the number part of section and convert to an integer/double and then add.

however, all of that is a guess, you will need to post a little more code, in particular the type definitions of track, section and answer. Additionally,
while you are looking at it does track become to big... i.e. bigger than the array section?

I think that the problem is that section is a char array and you have
the problem that you have to convert them to integers first, otherwise they have their ascii value. e.g. 1 is 49 (I think)

So isolate the number part of section and convert to an integer/double and then add.

however, all of that is a guess, you will need to post a little more code, in particular the type definitions of track, section and answer. Additionally,
while you are looking at it does track become to big... i.e. bigger than the array section?

As an extention to this assumption, i want to add the following. Indeed the ascii Value '1' has the numerical value 49. Thus we are given a way to convert a character to its numerical value, by subtracting 48 or '0'.

However this will not solve all of your problems. You may find that strtol() & strtod() are helpful in what you are looking for (sting header) you may find that a more practical solution is to use stringstreams.

If you want more info on these just ask, but i would recomend having a read from google links. And also search Daniweb for string/char to int conversion.

Chris

With that code, I'm pretty sure your program is adding the char to the char array holding the answer(please someone correct me if I'm wrong), you will have to convert the chars to numbers and add the two from there. atoi(const char) will convert a char or char array to an int. It is included in iostream.

With that code, I'm pretty sure your program is adding the char to the char array holding the answer(please someone correct me if I'm wrong), you will have to convert the chars to numbers and add the two from there. atoi(const char) will convert a char or char array to an int. It is included in iostream.

Although i made a suggestion about strtol() and strtod(), Which are much better than atoi()...you should read some comparisons about strtol() and atoi() you will soon realise why we no longer use atoi()

Chris

I think that the problem is that section is a char array and you have
the problem that you have to convert them to integers first, otherwise they have their ascii value. e.g. 1 is 49 (I think)

So isolate the number part of section and convert to an integer/double and then add.

however, all of that is a guess, you will need to post a little more code, in particular the type definitions of track, section and answer. Additionally,
while you are looking at it does track become to big... i.e. bigger than the array section?

track is an integer. I simply use it to keep track of the number of times loop has run. section is an string. string section[10];
and answer is a string, but as you see, nothing is predeclared. its empty till it comes to this point in the code.

I suggest having a read about this

and looking over this

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(void){
     string a = "3";
     int b = 12;
     int answer = 0;
              
     answer = strtol (a.c_str(), NULL, 10) + b;
     
     cout << answer;
     
     cin.get();
     return 0;
}

Chris

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.