Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Converting a string of integers to integers

Posted on 2003-03-18
Medium Priority
1,040 Views
Hi,
I have the following say string
char *chars = "1 2 3 4 5";

I need to be able to take those integers out and put them in a queue.. void test_sidea(char *cars)
void myFunction(char* cars)
{
unsigned int counter = 0;

cars = strtok(cars, " ");

while (cars != NULL)
{
cars = strtok(NULL, " "); // tokenaizes it...
queueSideA.enqueue (?????) // how can I convert from string to int and add as i go through the loop ?

counter++; // keeps track of tokens
}
}

If anyone can help me that would be great.. Thank You in advance
0
Question by:originalrobby

LVL 1

Expert Comment

ID: 8160715
since you have spaces in between the different numbers the easiest way that I can think to do it is:

have a loop that copies the characters from the original string into a temp-string, just until it hits a space:

int i;  // i will have to travel through the whole string, so don't reset to 0
int k = 0;
while(chars[i] != ' ')
{
temp[k] = chars[i];
i++;
k++;
}
temp[k] = '\0';

then you just turn it into a integer by using atoi

int num;
num = atoi(temp);

Then you can add them up
0

LVL 18

Expert Comment

ID: 8160725
queueSideA.enqueue (atoi(cars));
0

LVL 12

Accepted Solution

Salte earned 150 total points
ID: 8160886
I would think the easiest way is to use strtol():

const char * str = "1 2 3 4 5";

char * s;
char * t;
int x = strtol(str,& s, 10);
// x is now 1, s = " 2 3 4 5";
// push x in queue.
x = strtol(s, & t, 10);
// x is now 2, t = " 3 4 5";
// push x in queue.
x = strtol(t, & s, 10);
// x is now 3, s = " 4 5";
// push x in queue.
x = strtol(s, & t, 10);
// x is now 4,t = " 5"
// push x in queue
x = strtol(t, & s, 10);
// x is now 5, s = "";
// push x in queue.

// stop since s is empty string.

In a loop this looks like this:

void put_in_queue(Queue & q, const char * s)
{
char * t = const_cast<char *>(s);
char * u;

while (string t is not empty string) {
int x = strtol(t, & u, 10);
q.enqueue(x);
t = u;
}
}

Exactly which method you use to put x in queue depends on the queue type.

Exactly how to test if t is empty depends partly on how robus you want it and how your input string is. In a simple case it can be as simple as doing:

while (*t != '\0') {

but this isn't very robust and require that the string has no blanks after the last number.

Another way is to skip blanks originally in the string (before the main while loop):

while (isblank(*t)) ++t;
and then do the test above:

while (*t != '\0') {

and then skip blanks again after you set t = u;

t = u;
while (isblank(*t)) ++t;

at the end of the while loop.

#include <cctype> to get isblank() function.

Alf
0

LVL 6

Expert Comment

ID: 8162257
#include <sstream>
#include <string>
using std::stringstream;
using std::string;
//For a string
int to_int(const string& s)
{
int i=0;
stringstream ss;
ss << s;
ss >> i;
return i;
}

For a char array (char *)

int to_int(const char* s)
{
int i=0;
stringstream ss;
ss << s;
ss >> i;
return i;
}

0

Expert Comment

ID: 8162312
if you're using chars you can just use the atoi command
0

LVL 12

Expert Comment

ID: 8165458
KyleG,

atoi() doesn't give you the pointer to the "position after the number" which he needs.

if a string is a string of numbers with blanks between:

s = "1 2 3 4 5";

Then atoi() will just tell you that the first number is 1, it won't tell you that the string after is " 2 3 4 5".

Of course, you can then skip digits but that means you have to do the job that strtol() already does for you.

Alf
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month13 days, 11 hours left to enroll