473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Converting string to int

Hi,



I'm trying to read in a string of the N=348, and convert it into an integer. I have already split the string down to remove the non numeric values and can print the correct value to the screen. When I then try to convert the string of numeric values to an int using the atoi function the code won't compile and comes up with the error below. A small bit of the code is included below the error message, any help would be appreciated. I recognise that there are probably nicer ways of doing this but I am still a novice.



Thanks

Paul



SkinFrition.c++:156: error: cannot convert `std::string' to `const char*' for argument `1' to `int atoi(const char*)'



Expand|Select|Wrap|Line Numbers
  1. void countpoints(int npz[], char *argv[])
  2.  
  3. {
  4.  
  5.   ifstream raw(argv[1]);
  6.  
  7.   string word1, nums;
  8.  
  9.   char wordsplit, charr[10];
  10.  
  11.   bool found = false;
  12.  
  13.   int nnum, nzone = 0;
  14.  
  15.  
  16.  
  17.   while(!raw.fail())
  18.  
  19.   {
  20.  
  21.         getline(raw, word1);
  22.  
  23.         raw >> wordsplit;
  24.  
  25.         string nums;
  26.  
  27.         if (wordsplit == 'N')
  28.  
  29.         {
  30.  
  31.           raw >> wordsplit; // Reads the equals sign
  32.  
  33.           nnum = 0;
  34.  
  35.  
  36.  
  37.           while(wordsplit != ',')
  38.  
  39.           {
  40.  
  41.             raw >> wordsplit;
  42.  
  43.             if (wordsplit != ',')
  44.  
  45.             {
  46.  
  47.               charr[nnum] = wordsplit;
  48.  
  49.               nnum++;
  50.  
  51.             }
  52.  
  53.           }
  54.  
  55.  
  56.  
  57.           for(int i = 0; i < nnum; i++)
  58.  
  59.           {
  60.  
  61.             nums = nums + charr[i];
  62.  
  63.           }
  64.  
  65.         }
  66.  
  67.         npz[nzone] = atoi(nums);
  68.  
  69.         nzone++;
  70.  
  71.       }
  72.  
  73. }
Feb 19 '07 #1
1 2797
Ganon11
3,652 Expert 2GB
OK, in order to satisfy atoi(), you will need to give the function a character array, rather than a string. You can to this very easily by using the .c_str() function in std::string - Basically, instead of calling

Expand|Select|Wrap|Line Numbers
  1. atoi(myString);
use

Expand|Select|Wrap|Line Numbers
  1. atoi(myString.c_str());
This will take the string and return a character array representing the string - atoi() should be happy with this.
Feb 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.