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?
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
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.
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...
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.
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 ?