Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Two questions about a program in C

Reply
Thread Tools

Two questions about a program in C

 
 
Eoghan
Guest
Posts: n/a
 
      02-08-2008
Hi there! I have two simple questions about a program i'm doing:
1) if I write:
enum logic {VERO=1, FALSO=0};
logic trovato=VERO;

Is this correct according to the ANSI '89 standard?

2) I have these lines:
void GetInfo(void)
{
int i=0;
char Dato[256];
printf("Inserire i dati numero %i: ", ++i);
scanf("%[^\n]", Dato);
fflush(stdin);

while( strcmp(Dato, "FINE") )
{
Riempi_Info(Dato); //Fills an array called Estremi
printf("Sigla: %s \t Inizio: %f \tFine: %f\n", Sigle[Nstr],
Estremi[Nstr][0], Estremi[Nstr][1] );
i++;
Nstr++;
printf("Inserire i dati numero %i: ", i);
scanf("%[^\n]", Dato);
fflush(stdin);
}
return;
}

Why does the program crash if I don't call the function fflush after
each scanf? And why in windows can't I use the function fpurge instead?
 
Reply With Quote
 
 
 
 
Dave Hansen
Guest
Posts: n/a
 
      02-08-2008
On Feb 8, 1:31 pm, Eoghan <(E-Mail Removed)> wrote:
> Hi there! I have two simple questions about a program i'm doing:
> 1) if I write:
> enum logic {VERO=1, FALSO=0};
> logic trovato=VERO;
>
> Is this correct according to the ANSI '89 standard?


No. This isn't C++. You need to either add

typedef enum logic logic;

before the definition, or change the definition to

enum logic trovato=VERO;

>
> 2) I have these lines:

[...]
> scanf("%[^\n]", Dato);
> fflush(stdin);

[...]
>
> Why does the program crash if I don't call the function fflush after
> each scanf? And why in windows can't I use the function fpurge instead?


Don't do that. http://c-faq.com/stdio/stdinflush.html

Regards,

-=Dave
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-08-2008
Eoghan wrote:
> Hi there! I have two simple questions about a program i'm doing:
> 1) if I write:
> enum logic {VERO=1, FALSO=0};
> logic trovato=VERO;
>
> Is this correct according to the ANSI '89 standard?


No. Change `logic' to `enum logic' and it will be correct.

> 2) I have these lines:
> void GetInfo(void)
> {
> int i=0;
> char Dato[256];
> printf("Inserire i dati numero %i: ", ++i);
> scanf("%[^\n]", Dato);
> fflush(stdin);
>
> while( strcmp(Dato, "FINE") )
> {
> Riempi_Info(Dato); //Fills an array called Estremi
> printf("Sigla: %s \t Inizio: %f \tFine: %f\n", Sigle[Nstr],
> Estremi[Nstr][0], Estremi[Nstr][1] );
> i++;
> Nstr++;
> printf("Inserire i dati numero %i: ", i);
> scanf("%[^\n]", Dato);
> fflush(stdin);
> }
> return;
> }
>
> Why does the program crash if I don't call the function fflush after
> each scanf?


Probably because of something in Riempi_Info, or in the
declarations of Estremi, Sigle, and Nstr. Unfortunately, you
have not shown any of these, so we cannot tell.

fflush(stdin) invokes undefined behavior. Perhaps the
error of fflush(stdin) somehow cancels out the other error(s).

> And why in windows can't I use the function fpurge instead?


There is no fpurge() function in the Standard C library.
If it's a Windows thing, ask about it in a Windows forum.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      02-08-2008
In article <(E-Mail Removed)>,
Eoghan <(E-Mail Removed)> wrote:

>2) I have these lines:
>void GetInfo(void)
>{
> int i=0;
> char Dato[256];
> printf("Inserire i dati numero %i: ", ++i);
> scanf("%[^\n]", Dato);


Output is not certain to appear before the next input
(especially when the output does not end in '\n') unless
the output stream is fflush()'d.

You did not put a size constraint on the scanf(), so if the
user enters more characters than you have alloted (255) then
your program will have undefined behaviour.

> fflush(stdin);


fflush() is only defined for output and update streams.

--
"I was very young in those days, but I was also rather dim."
-- Christopher Priest
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-08-2008
Eoghan <(E-Mail Removed)> writes:

> Hi there! I have two simple questions about a program i'm doing:
> 1) if I write:
> enum logic {VERO=1, FALSO=0};
> logic trovato=VERO;
>
> Is this correct according to the ANSI '89 standard?


No, you need to say: 'enum logic trovato = VERO;' and if you are not
getting a diagnostic from your version you should review what compiler
options you are using.

>
> 2) I have these lines:
> void GetInfo(void)
> {
> int i=0;
> char Dato[256];
> printf("Inserire i dati numero %i: ", ++i);
> scanf("%[^\n]", Dato);
> fflush(stdin);


You simply are not allowed to do this in a portable program. Your
system is allowed to define some meaning for this code, but it means
nothing in standard C. fflush is for output streams.

To replace this, write a look that skips to EOF or newline (as a
function, of course!). Maybe:

int c;
do {
c = getc(stdin);
} while (c != EOF && c != '\n');

>
> while( strcmp(Dato, "FINE") )
> {
> Riempi_Info(Dato); //Fills an array called Estremi
> printf("Sigla: %s \t Inizio: %f \tFine: %f\n", Sigle[Nstr],
> Estremi[Nstr][0], Estremi[Nstr][1] );
> i++;
> Nstr++;
> printf("Inserire i dati numero %i: ", i);
> scanf("%[^\n]", Dato);
> fflush(stdin);
> }
> return;
> }
>
> Why does the program crash if I don't call the function fflush after
> each scanf?


My guess is that problem is be caused by other parts of your program
(not shown) doing something wrong when the scanf reads an empty
string.

> And why in windows can't I use the function fpurge instead?


Why would you want to? Stick to standard C and your program will run
almost anywhere.

--
Ben.
 
Reply With Quote
 
Eoghan
Guest
Posts: n/a
 
      02-08-2008
On the web fpurge is said to be included in stdio.h

I'm sure the string has less than 256 characters, however here's the
code of RiempiInfo:
void Riempi_Info(char* str)
{
int k=0;

while(str[k]!=' ' && str[k]!='\0') Sigle[Nstr][k]=str[k++];

str=strchr(str, (int)' ');
Estremi[Nstr][0]=atof(&str[1]);

/*Parto da str[1] perche' str[0] e' gia uno spazio*/
str=strchr(&str[1], (int)' ');
Estremi[Nstr][1]=atof(&str[1]);
return;
}

where the variables are:
char Sigle[256][10]; (I'm way sure Nstr is less than 256)
float Estremi[100][2];

 
Reply With Quote
 
Eoghan
Guest
Posts: n/a
 
      02-08-2008
If I don't use fflush the program behaves liek this:

Inserire i dati numero 1:

I write:One 0.0 0.3 and I press return

Then the programs writes:

Sigla: ewf Inizio: 0.000000 Fine: 0.300000
Inserire i dati numero 2: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 3: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 4: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 5: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 6: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 7: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 8: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
Inserire i dati numero 9: Sigla: ewf Inizio: 0.000000 Fine:
0.300000
.....
and so on

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-08-2008
Eoghan wrote:
> On the web fpurge is said to be included in stdio.h
>
> I'm sure the string has less than 256 characters, however here's the
> code of RiempiInfo:
> void Riempi_Info(char* str)
> {
> int k=0;
>
> while(str[k]!=' ' && str[k]!='\0') Sigle[Nstr][k]=str[k++];
>
> str=strchr(str, (int)' ');
> Estremi[Nstr][0]=atof(&str[1]);


And if strchr() returned NULL ...?

Note that strchr() *will* return NULL (see code in
original post) if you don't do something to get rid of
the '\n' that stopped the scanf() -- because the next
scanf() will stop on the same '\n', and the buffer will
then hold a '\0' in its first position.

It seems likely that fflush(stdin), whose behavior
is undefined by C, may have the effect of swallowing the
'\n' *on your system* and *under today's circumstances*,
which is why inserting fflush(stdin) fixes your problem.
But that's not a "fix," it's a "kludge."

See Questions 12.26a and 12.26b in the comp.lang.c
Frequently Asked Questions (FAQ) list, http://www.c-faq.com/.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-08-2008
Eoghan wrote:

> On the web fpurge is said to be included in stdio.h


It is not a standard C function. Whether or not some implementation
includes a declaration in a standard header doesn't change that.



Brian
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-08-2008
Eoghan wrote:

> If I don't use fflush the program behaves liek this:


You're probably leaving newlines in the input buffer. See the FAQs:

<http://c-faq.com/stdio/stdinflush.html>
<http://c-faq.com/stdio/stdinflush2.html>


You can't portably use fflush() on an input stream.




Brian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Two ISP -Two Routers - 1 PIX James Parks Cisco 5 12-11-2003 08:55 PM
Cisco 2611 with Two T1 from two ISPs Adam Embrey Cisco 3 07-24-2003 10:22 PM



Advertisments