convert string to decimal

Hi everyone , i have a text file with strings inside it.
now i need to read that file and convert the strings into a decimal value. like for example i have the string CSC now i should have the integer values for C S and C . i tried to use strtol and strtoul but it gives me error. when i use atoi it gives me 0. can someone please advise?
Please provide an example of the text file.
1
2
3
4
5
6
7
addpeer 5
addpeer 6
addpeer 10
Insert 11
Insert test
Insert name
Insert CSCI


that is the example of the text file , i just want to convert the 2nd column. i am able to read the file now successfully . i just cannot convert the string into a decimal (integer). i keep getting 0 if use atoi
Last edited on
Four of those lines have an integer in the second column. If, for whatever reason, you have read those into strings then you can use stoi()
http://www.cplusplus.com/reference/string/stoi/
to put them in int variables.

Three of those lines have a string in the second column. What did you want to do about those strings ("test", "name", "CSCI")?

paulpv278 wrote:
now i should have the integer values for C S and C
I don't know what that means - please clarify. Your question is still not clear.
Last edited on
sorry to confuse you. yes those in the 2nd column is declared as strings and i wanna convert those into integers as i need to pass them into another function which has a parameter of integer.

for the C S and C i meant that once converted my expected values are 67, 83 and 67 respectively...

im not sure if that's posible tho
With all due respect, @paulpv278, this seems a very odd thing to do.

In future questions, you may get better help if you state what you are trying to achieve at the end, not some isolated (and unlikely) bit of analysis. I still have no idea from this (or your previous posts) what you are actually trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
// ifstream in( "input.txt" );
   stringstream in( "addpeer 5\n"
                    "addpeer 6\n"
                    "addpeer 10\n"
                    "Insert 11\n"
                    "Insert test\n"
                    "Insert name\n"
                    "Insert CSCI\n" );

   string line;
   while( getline( in, line ) )        // read a line as a time
   {
      string instruction, item;
      stringstream( line ) >> instruction >> item;

      cout << instruction << '\n';
      if ( isdigit( item[0] ) )        // slightly non-robust way of checking for numerical value
      {
         int i = stoi( item );
         cout << "Integer " << i << '\n';
      }
      else                             // otherwise print out individual codes (probably ASCII)
      {
         cout << "Codes: ";
         for ( char c : item ) cout << (int)c << ' ';
         cout << '\n';
      }
      cout << '\n';
   }
}

addpeer
Integer 5

addpeer
Integer 6

addpeer
Integer 10

Insert
Integer 11

Insert
Codes: 116 101 115 116 

Insert
Codes: 110 97 109 101 

Insert
Codes: 67 83 67 73
Last edited on
in c++ characters are integers, 1 byte in length. there is no 'conversion' apart from forcing it to print the integer instead of the character value, which you can do with (int) variable cast in the print:
cout << (int)var << endl;

or for a string..
for(... string length)
cout << (int) stringvar[i] << " ";

here you would want to find the space and iterate forward of that, can you do it ?
Last edited on
Topic archived. No new replies allowed.