how to sum a string array? iam getting confused...

Jan 3, 2011 at 10:51am
i have a 2d array which contains numbers and characters loaded from a txt.
i declared the array like "string myarray[27][5]". It has a format like:
1 3 28 Steve Hole
2 1 29 Anthony Curtis
......
......

i need to sum the third collumn which means the minutes.

for(i=0;i<27;i++)
{
total += myarray[i][2];
}
cout<<total<<endl;

the main problem is that i can't convert string to int, anyway i tried atoi, atol but it didn't worked...
please help me out!
Jan 3, 2011 at 12:29pm
im a new to c++ but I would do it like this.




int total;

for(i=0;i<27;i++)
{
total += ( myarray[i][2] - 48);
}
cout<<total<<endl;


// or make function

int string2int ( int string)
{
return string - 48;

}


////////////////

int total;

for(i=0;i<27;i++)
{
total += string2int ( myarray[i][2] );
}
cout<<total<<endl







I guess you could make your own Atoi function easily enough .


you could even make your own string length function like I did . In fact throw out the entire standard lib and make your own lol .

// calculates the length of a string

int strlen ( char *string)
{
int i = 0;

while ( string[i] != '\0' ) {
i++;
}

return i;
Last edited on Jan 3, 2011 at 12:32pm
Jan 3, 2011 at 1:54pm
// or make function

int string2int ( int string)
{
return string - 48;

}
------------------------------------------------------
int total; declaration is still there in mycode. :)

I don't really understand this "-48" stuff.
i could make my own atoi function, but how?
which part of the programm will solve me the problem to count the time,
and anyway convert the string array type to int array or int anything.
i'am not able to sum minutes beacause i must have to work with string array because of the txt, and its not allowing sum.
sum.
Last edited on Jan 3, 2011 at 1:55pm
Jan 3, 2011 at 4:05pm
You can cast the string into a const char* in order to use the atoi() function. For example:

1
2
3
4
5
6
7
int convertNumericalString (string sNum)
{
    const char *cNum;
    cNum = reinterpret_cast <const char*>(sNum.c_str());
    int num = atoi(cNum);
    return num;
}


These function receives as a parameter a number save as string and returns the same number as an int. Hope I've helped you.
Jan 3, 2011 at 4:19pm
If you are using C++, use stringstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>
#include <string>

template <typename Result>
Result convert_to( const std::string& s )
  {
  Result result = Result();
  std::istringstream ss( s );
  ss >> result;
  return result;
  }

using namespace std;

int main()
  {
  int x = convert_to <int> ( "42" );

  if (x == 42) cout << "success!\n";
  else         cout << "failure!\nx == " << x << endl;

  return 0;
  }

Hope this helps.

[edit] A little code improvement for you. :-)
Last edited on Jan 3, 2011 at 4:21pm
Jan 3, 2011 at 4:45pm

Here is an example using streams

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

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    int A,C,D=10,F=3;
    double B,E=3.17;

    string a = "123", b = "34.22", c = "17";

    istringstream ( a ) >> A;
    istringstream ( b ) >> B;
    istringstream ( c ) >> C;

    cout << A << " + " << D << "=" << A+D << endl;
    cout << B << " + " << E << "=" << B+E << endl;
    cout << C << " + " << F << "=" << C+F << endl;

}


123 + 10=133
34.22 + 3.17=37.39
17 + 3=20

Jan 3, 2011 at 4:50pm
just a note, you should probably check that the string is numeric.
stream will convert up to the first non-numeric character

"123x" returns 123
"x123" returns 0


Jan 3, 2011 at 4:58pm
oh god! i really like to say thanks to all of u!
Nice team!
Jan 3, 2011 at 9:02pm
to answer your question about the -48

the character stored in each array of string is an ASCII character code which is a decimal number and it just so happens that 0 to 9 is 48 to 57 in ASCII

So 48 - 48 = 0
49 - 48 = 1
50 - 48 = 2

the following site links to a conversion table http://easycalculation.com/ascii-hex.php

this tech only works on single digit numbers . to convert 234 from string to int you would have work out the length of the numbers and the sequence and then do the following calculation :
(100 * 2) + (10 * 3) + (4) = 234
Last edited on Jan 4, 2011 at 7:05am
Topic archived. No new replies allowed.