943,991 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1502
  • C++ RSS
Feb 24th, 2007
0

char+int?????

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DummyChuck is offline Offline
2 posts
since Feb 2007
Feb 24th, 2007
0

Re: char+int?????

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():
  1. int myNumber;
  2. 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)
  1. istringstream myStream;
  2. myStream.str(myString);
  3. myStream >> myNumber;
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 25th, 2007
0

Re: char+int?????

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...
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 25th, 2007
0

Re: char+int?????

Click to Expand / Collapse  Quote originally posted by Infarction ...
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...
I assumed when he said, "Can a character string like "215" be converted to an integer" that he meant character string, regardless of the thread title.

However, Infarction is correct, for a char conversion, subtraction is all that is necessary:
  1. int myNumber;
  2. myNumber = myChar - '0'; // won't work for a string!
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 25th, 2007
0

Re: char+int?????

I am seeking clues in C
I'm Learning here ...... Don't give up on me yet.

How about in this case;

[
int A;
float B;
char C;

can the expression A+C*B

work?

I don't see how I can do it.

Charlie
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DummyChuck is offline Offline
2 posts
since Feb 2007
Feb 25th, 2007
0

Re: char+int?????

>can the expression A+C*B work?
Sure. Convert C to an integer first.
  1. float result;
  2. int anotherInteger;
  3. anotherInteger = C - '0';
  4. 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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: word count
Next Thread in C++ Forum Timeline: Array and File Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC