Hello,

For my homework I have to do about 14 functions and I have figured out all but two of them.
This one;
int toNumber( char c )

If the character passed in is a numeric digit, then the
integer value (not the ASCII code) of that digit is returned.
If the character is not a digit, -1 should be returned.

I have tried about 10 different things and can not seem to figure out

it either puts out c or some random number. Also I can not use isdigit unfort.

``````int toNumber (char ch)
{

char c = ch;

if(c>=48&&c<=57)
cout << 'c' << endl;
else
cout << '-1' << endl;
return 0 ;
}``````

And the other one i am having problems with.

bool isSpace( char c )

Returns true if the character passed in is a space. If it
is any other character, false is returned.

With vb.net I understand how to verify a space is being entered, but with C++ everytime I try to verify a space it looks at me like im stupid.

``````bool isSpace (char ch)
{
char c = ch;
if (isSpace(c)) c='\n'

{
return true;
}
else
{
return false;
}

}``````

Thanks
Aaron

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Bench

For your first function try something like below:

``````int toNumber(char c)
{
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -1;
}
return 0;
}``````

For your second function, what constitutes a space?

Edited by gerard4143: n/a

Im guessing a blank character. Something like char c = " ". But I tried this and it wouldnt work.

``c = " "``

Edited by personathome: Forgot to enter code

Im guessing a blank character. Something like char c = " ". But I tried this and it wouldnt work.

Well " " is not a blank character, ' ' is a blank character.

If your trying to mimic the C function isspace() then maybe you should consider these as spaces as well.

From the man pages

``````isspace()
checks  for  white-space characters.  In the "C" and "POSIX" locales,
these are: space, form-feed ('\f'), newline ('\n'),  carriage  return
('\r'), horizontal tab ('\t'), and vertical tab ('\v').``````

So for the case solution you showed. When i put in something other thna a number it gives me back that item. for example if i put in the letter c it outputs c instead of -1

``````int toNumber(char c)
{
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -1;
}
return 0;

}

//And this is what I use to call the function

do
{
cout  <<  endl  <<  "Enter a character (q to quit): ";
cin  >>  ch;
if  ( toNumber( ch ) )
cout  <<  ch <<  endl;
} while( ch != 'q' );``````

So for the case solution you showed. When i put in something other thna a number it gives me back that item. for example if i put in the letter c it outputs c instead of -1

``````int toNumber(char c)
{
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -1;
}
return 0;

}``````

Really I don't see how that's possible?

You have a number of problems with this code.

``````do
{
cout  <<  endl  <<  "Enter a character (q to quit): ";
cin  >>  ch;
if  ( toNumber( ch ) )
cout  <<  ch <<  endl;
} while( ch != 'q' );``````

toNumber() returns a number 0 - 9 and -1.

0 is false and everything else is true..How do you think that works in your if statement?

Also your displaying the character passed to toNumber() and your surprised its displayed!

Edited by gerard4143: n/a

it either puts out c or some random number. Also I can not use isdigit unfort.

``````char c = ch;

if(c>=48&&c<=57)
cout << 'c' << endl;``````

A character inside single-quotes denotes a character value, so all you're doing here is printing out 'c'. This value has absolutely nothing to do with your variable `c` .

Also, the intent of your code might be a bit clearer if you used character representations in your comparison instead of those "magic" numbers. (It also makes your code a bit more robust for character sets other than ASCII) i.e.

``if( c >= '0' && c <= '9' )``

With vb.net I understand how to verify a space is being entered, but with C++ everytime I try to verify a space it looks at me like im stupid.

Does VB.NET have recursion? What you've got in your code below is an infinite loop which will most likely crash your program when it overruns the stack

``````bool isSpace (char ch)
{
char c = ch;
if (isSpace(c)) c='\n'``````

Calling a function from within itself (recursion) is legal, although unnecessary most of the time; and you can't just do it un-checked like that.
In this particular case, what you're actually trying to do doesn't need recursion anyway. All you should need to do is check whether your character is equal to a whitespace character, which is usually one of `' '` , `'\t'` , `'\r'` , `'\n'`

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.