?
Solved

Converting a string of integers to integers

Posted on 2003-03-18
6
Medium Priority
?
1,038 Views
Last Modified: 2008-02-01
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
Comment
Question by:originalrobby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:Intern
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

by:bobbit31
ID: 8160725
queueSideA.enqueue (atoi(cars));
0
 
LVL 12

Accepted Solution

by:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:Mafalda
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

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

Expert Comment

by:Salte
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question