char+int?????

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 2
Reputation: DummyChuck is an unknown quantity at this point 
Solved Threads: 0
DummyChuck DummyChuck is offline Offline
Newbie Poster

char+int?????

 
0
  #1
Feb 24th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: char+int?????

 
0
  #2
Feb 24th, 2007
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:
  1. istringstream myStream;
  2. myStream.str(myString);
  3. myStream >> myNumber;
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: char+int?????

 
0
  #3
Feb 25th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: char+int?????

 
0
  #4
Feb 25th, 2007
Originally Posted by Infarction View Post
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!
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2
Reputation: DummyChuck is an unknown quantity at this point 
Solved Threads: 0
DummyChuck DummyChuck is offline Offline
Newbie Poster

Re: char+int?????

 
0
  #5
Feb 25th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: char+int?????

 
0
  #6
Feb 25th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC