I know you can use atoi() to convert a char to int but what happens if you are using a char array so that you can involve the '-' sign to make a negative int... or does it not work like that?
Or can you do it using strings instead of char arrays?
Basically i need the option for a user to input a negative value, which is orginally stored as a char variable to check whether the input is a 'r' or a 'q'(restart/quit) and then later covert it to an integer so I can use it.
atoi() does not convert a char to an int. It converts a C string (NUL-terminated character array) to an int. I recommend that you reread the atoi() documentation. Carefully. And then don't use it. Use the Boost Conversion Library's lexical_cast() instead.
But I am not even sure that is what you need. Your post does not make much sense.
Grr i typed everything to post and then closed the browser on accident. but anyways just quick make a simple function to do the job for you... or just insert the int value of r and q from the ascii chart in place of looking for r and q it will look for in this case 114 for f and 82 for R and 113 for q or 81 for Q. the latter of the two is what i would do because easy enough to do.
As for the function. I would have it loop through the function checking char pos 0 first and if thats a '-' an 'r' or a 'q' thats asuming they dont input a different char but yeah. after that just chop of the '-' however you choose and typecast the remaining number :D
But I am not even sure that is what you need. Your post does not make much sense.
Sorry, let me be more specific. Basiclly, I'm creating a quadratic factoriser so obviously the user needs to be allowed to input positive/negative integers. However, my problem is that because I'm allowing the user to restart/quit the program at any time, all number inputs from the user are stored as a chars first to check whether the input is an 'r'/'q'. Hence why I need the conversion.
So my question is, can a C-String be converted to a negative int or is it only possible to do it to posible values but i've just realised whilst wiriting this that you told me to read the documentation and i've just found
Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible,
I guess my next question is what should i use instead? Can you convert string objects to integers?