From: Mike Barnard on
Hi all.

I hope there's a few here still? These newsgroups are bleeding away.

I'm trying to use online tutorials to teach myself C. (I'm starting
with the basics before eventually trying for Objective C.) I have
Code::Blocks compiler installed on my XP machine and I'm trying to
create a few functions ATM.

One function is to be a validator of input. I want a user to enter 1
to 9 only. I want to get the character from a scanf() and compare it
with the ascii table. If it's < 49 OR it's > 57 then returns are set
else return = 0.

So my questions are:

1. What EACTLY is returned in scanf()? Is it an ascii code to start
with so no conversion of any sort needed? Google returns lots of very
detailed tech references which I don't yet understand but so far no
plain English explanations. I'll keep looking though.

2. Below is an INCOMPLETE function, but can you telL me why I'm
getting the compiler error "syntax error before '{' token" on the line
marked <<< please? Whats wrong with my declaration?

//Declaration
int Fcheckinput ( int Vnumberofns ); // Validates input

Main(){
blah blah...
}

int Fcheckinput ( int Vnumberofns ); // Validates input
{ <<<
int ns = Vnumberofns ; // put argument into internal variable.
if ( ns < 49 ) // Compare it with ascii values.
{
return 1;
}
else if ( ns > 57 )
{
return 2;
}
}
// ASCII
// 0 - 47 are misc characters
// 48 = 0
// 57 = 9
// 58 + are characters.


Many thanks.

Mike.

From: Ian Collins on
On 05/ 9/10 09:20 AM, Mike Barnard wrote:
> Hi all.
>
> I hope there's a few here still? These newsgroups are bleeding away.
>
> I'm trying to use online tutorials to teach myself C. (I'm starting
> with the basics before eventually trying for Objective C.) I have
> Code::Blocks compiler installed on my XP machine and I'm trying to
> create a few functions ATM.

Getting a copy of "The C programming Language" would be a better start
than on line tutorials of unknown quality.

> One function is to be a validator of input. I want a user to enter 1
> to 9 only. I want to get the character from a scanf() and compare it
> with the ascii table. If it's< 49 OR it's> 57 then returns are set
> else return = 0.

If you are reading a single character into a char, there's no need to
mess with ASCII values, just compare with '0' and '9'.

> So my questions are:
>
> 1. What EACTLY is returned in scanf()? Is it an ascii code to start
> with so no conversion of any sort needed? Google returns lots of very
> detailed tech references which I don't yet understand but so far no
> plain English explanations. I'll keep looking though.

That depends on the parameters you pass, which you haven't shown us.

> 2. Below is an INCOMPLETE function, but can you telL me why I'm
> getting the compiler error "syntax error before '{' token" on the line
> marked<<< please? Whats wrong with my declaration?
>
> //Declaration
> int Fcheckinput ( int Vnumberofns ); // Validates input

Get rid of the spurious leading capitals, they only serve to confuse the
reader.

> Main(){

I hope you wrote "int main(void)"! C is case sensitive.

> blah blah...
> }
>
> int Fcheckinput ( int Vnumberofns ); // Validates input

You have a spurious semicolon at the end of the line. This makes the
line above a function delaration, not the first line of it's definition.

> {<<<
> int ns = Vnumberofns ; // put argument into internal variable.

Why?

> if ( ns< 49 ) // Compare it with ascii values.
> {
> return 1;
> }
> else if ( ns> 57 )
> {
> return 2;
> }
> }

What do the values 1 and 2 mean? If they are error codes, create an
error code enum so they read better. You also need to return a success
value if the number is in range.

typedef enum { Ok, TooLow, TooHigh } ErrorCode;

--
Ian Collins
From: Paul Bibbings on
Mike Barnard <m.barnard.trousers(a)thunderin.co.uk> writes:

<snip>

> 2. Below is an INCOMPLETE function, but can you telL me why I'm
> getting the compiler error "syntax error before '{' token" on the line
> marked <<< please? Whats wrong with my declaration?
>
> //Declaration
> int Fcheckinput ( int Vnumberofns ); // Validates input
>
> Main(){
> blah blah...
> }
HERE
|
V
> int Fcheckinput ( int Vnumberofns ); // Validates input
> { <<<
> int ns = Vnumberofns ; // put argument into internal variable.
> if ( ns < 49 ) // Compare it with ascii values.
> {
> return 1;
> }
> else if ( ns > 57 )
> {
> return 2;
> }
> }

Regards

Paul Bibbings
From: Mike Barnard on
On Sun, 09 May 2010 09:35:52 +1200, Ian Collins <ian-news(a)hotmail.com>
wrote:

Ian, what a quick answer! Thanks.

>On 05/ 9/10 09:20 AM, Mike Barnard wrote:
>> Hi all.
>>
>> I hope there's a few here still? These newsgroups are bleeding away.
>>
>> I'm trying to use online tutorials to teach myself C. (I'm starting
>> with the basics before eventually trying for Objective C.) I have
>> Code::Blocks compiler installed on my XP machine and I'm trying to
>> create a few functions ATM.
>
>Getting a copy of "The C programming Language" would be a better start
>than on line tutorials of unknown quality.

It's on order for after payday. Until then I'm going with
http://www.cprogramming.com/tutorial.html#ctutorial to start with.


>> One function is to be a validator of input. I want a user to enter 1
>> to 9 only. I want to get the character from a scanf() and compare it
>> with the ascii table. If it's< 49 OR it's> 57 then returns are set
>> else return = 0.
>
>If you are reading a single character into a char, there's no need to
>mess with ASCII values, just compare with '0' and '9'.

Thanks.

>> So my questions are:
>>
>> 1. What EACTLY is returned in scanf()? Is it an ascii code to start
>> with so no conversion of any sort needed? Google returns lots of very
>> detailed tech references which I don't yet understand but so far no
>> plain English explanations. I'll keep looking though.
>
>That depends on the parameters you pass, which you haven't shown us.

scanf ( "%d", Vnumberofns ) ;

>> 2. Below is an INCOMPLETE function, but can you telL me why I'm
>> getting the compiler error "syntax error before '{' token" on the line
>> marked<<< please? Whats wrong with my declaration?
>>
>> //Declaration
>> int Fcheckinput ( int Vnumberofns ); // Validates input

>Get rid of the spurious leading capitals, they only serve to confuse the
>reader.

I'm using them to show me where I'm looking at a Function or Variable.
I'm sure I'll develop a personal style in the long term.

>> Main(){
>
>I hope you wrote "int main(void)"! C is case sensitive.

Well, yes. That was just a filler.

>> blah blah...
>> }
>>
>> int Fcheckinput ( int Vnumberofns ); // Validates input
>
>You have a spurious semicolon at the end of the line. This makes the
>line above a function delaration, not the first line of it's definition.

AHA, obvious. Thanks. This is something I MIGHT not do again.

>> {<<<
>> int ns = Vnumberofns ; // put argument into internal variable.
>
>Why?

Experimentation.

>> if ( ns< 49 ) // Compare it with ascii values.
>> {
>> return 1;
>> }
>> else if ( ns> 57 )
>> {
>> return 2;
>> }
>> }
>
>What do the values 1 and 2 mean? If they are error codes, create an
>error code enum so they read better. You also need to return a success
>value if the number is in range.

The idea is error codes of sorts. If it returns 1 tell them they
pressed below 1-9, if it returns 2 then it was a character. No useful
reason, just to make it happen. The nly way to learn is to do it, not
just read about it.

>typedef enum { Ok, TooLow, TooHigh } ErrorCode;

Again, thanks. BUT I've only been playing with C for a few hours all
in all and I have never met enum yet! I have no training, just an
interest. Google ahoy!

I'm sure I'll post again, but I'm trying to do it myself first. Thank
you.

Mike.
From: Mike Barnard on
On Sat, 08 May 2010 22:46:35 +0100, Paul Bibbings
<paul.bibbings(a)gmail.com> wrote:

>Mike Barnard <m.barnard.trousers(a)thunderin.co.uk> writes:
>
><snip>
>
>> 2. Below is an INCOMPLETE function, but can you telL me why I'm
>> getting the compiler error "syntax error before '{' token" on the line
>> marked <<< please? Whats wrong with my declaration?
>>
>> //Declaration
>> int Fcheckinput ( int Vnumberofns ); // Validates input
>>
>> Main(){
>> blah blah...
>> }
> HERE
> |
> V
>> int Fcheckinput ( int Vnumberofns ); // Validates input
>> { <<<

What, the space between the t and the V? :) See my other reply, and
thanks!

>
>Regards
>
>Paul Bibbings