Jump to content

Converting string to int in C++

Juggernautilus
Go to solution Solved by Mo5,

Use method atoi();

 

 

atoi()

Prototype: int atoi(const char *string);
Header File: stdlib.h © or cstdlib (C++)
Explanation: This function accepts a string and converts it into an integer. For example, if "1234" is passed into the function, it will return 1234, an integer. If the string contains a decimal place, the number will be truncated. Eg, "104.21" will be returned as 104.

Source: http://www.cprogramming.com/fod/atoi.html

Hi, I have been learning C++ for a while now but never I had to do this.

 

I am practising for programming contest at my school and I will have to read and write from files.

 

In the file I read, I have a string with 6 numbers, all seperated by a space. So far I got the code to get seperate strings of these numbers. For example: "1" "2" "3" "4" "5" "6" or "1213" "423412" etc.

I will not get into details what I have to do further, because I have it done already. All I need to know how to convert these strings of numbers to integers. 

 

Also, the max size of the number is 1018 so they will surely use at least one max number. Should I use long int or int is fine?

 

 

Sorry for my terrible English

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Could take the substring of the leftmost character and then examine the ASCII or whatever value for it (can be determined by assigning to an integer) and find out the actual integer value from that. Then if there is another character take what you have found out and just multiply by 10 and continue. I won't write any code for you, but that is the general idea.

Link to comment
Share on other sites

Link to post
Share on other sites

Use method atoi();

 

 

atoi()

Prototype: int atoi(const char *string);
Header File: stdlib.h © or cstdlib (C++)
Explanation: This function accepts a string and converts it into an integer. For example, if "1234" is passed into the function, it will return 1234, an integer. If the string contains a decimal place, the number will be truncated. Eg, "104.21" will be returned as 104.

Source: http://www.cprogramming.com/fod/atoi.html

Asrock 890GX Extreme 3 - AMD Phenom II X4 955 @3.50GHz - Arctic Cooling Freezer XTREME Rev.2 - 4GB Kingston HyperX - AMD Radeon HD7850 - Kingston V300 240GB - Samsung Spinpoint F3 1TB - Chieftec APS-750 - Cooler Master HAF912 PLUS


osu! profile

Link to comment
Share on other sites

Link to post
Share on other sites

The max size for int is 65,536, if that helps

 

I don't think there are any languages that implement max signed int as anything other than 2^31-1

Link to comment
Share on other sites

Link to post
Share on other sites

I don't think there are any languages that implement max signed int as anything other than 2^31-1

Yeah, what he said is an unsigned short (unsigned 16 bit int).

 

 

I though int was 16 bit?

It still isn't unsigned by default though even if it is. And I think it's 32 bit default these days but I could be wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×