| | |
char+int?????
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 2
Reputation:
Solved Threads: 0
I am very new at this, and I am sure this is a really basic question but if I don't know, then I have been taught to ask .... even if it is dumb.
Can a character be added to an interger. I feel confident I would get a syntax error, but wanted to make sure.
Can a character string like "215" be converted to an integer?
Charlie
Can a character be added to an interger. I feel confident I would get a syntax error, but wanted to make sure.
Can a character string like "215" be converted to an integer?
Charlie
It depends what language (C or C++) and what kind of error-checking you need.
A basic function which has no error-checking is atoi():
A better C function in regards to error-checking is strtol. For C++, you'll want to pop it in and out of a stream:
A basic function which has no error-checking is atoi():
c Syntax (Toggle Plain Text)
int myNumber; myNumber = atoi(myString); // myString had better contain a number!
A better C function in regards to error-checking is strtol. For C++, you'll want to pop it in and out of a stream:
C++ Syntax (Toggle Plain Text)
istringstream myStream; myStream.str(myString); myStream >> myNumber;
"Technological progress is like an axe in the hands of a pathological criminal."
Just to nitpick a li'l: a char and a character string are very different. For a character string, you'll need to do like joeprogrammer says, which would be correct for your example. You can also add the value of a single character (char) to an integer, but remember that a char value is not necessarily it's integer representation. That is '0' != 0, '1' != 1, etc...
•
•
•
•
Just to nitpick a li'l: a char and a character string are very different. For a character string, you'll need to do like joeprogrammer says, which would be correct for your example. You can also add the value of a single character (char) to an integer, but remember that a char value is not necessarily it's integer representation. That is '0' != 0, '1' != 1, etc...
However, Infarction is correct, for a char conversion, subtraction is all that is necessary:
c Syntax (Toggle Plain Text)
int myNumber; myNumber = myChar - '0'; // won't work for a string!
"Technological progress is like an axe in the hands of a pathological criminal."
>can the expression A+C*B work?
Sure. Convert C to an integer first.
If you didn't convert the char to an integer first, you'd get a much higher value than you expect.
Sure. Convert C to an integer first.
c Syntax (Toggle Plain Text)
float result; int anotherInteger; anotherInteger = C - '0'; result = A+anotherInteger*B;
If you didn't convert the char to an integer first, you'd get a much higher value than you expect.
"Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
- how to convert char to int (C++)
- Conversion from Char* to int ? (C++)
- Convertings strings to numbers (C++)
- fstream to char and int array (C++)
- Converting Int to Const Char (C)
- how to change char into int (C++)
Other Threads in the C++ Forum
- Previous Thread: word count
- Next Thread: Array and File Help
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






