Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi All, I am having problem with 1

Status
Not open for further replies.

praywin

Programmer
Feb 6, 2003
31
IN
Hi All,
I am having problem with this piece of C code. I want to input two values. One is a char and another is a char * and I want to keep doing this in a while loop. However after the first iteration the program doesn't stop at the first scanf.
Please help me out here.
Thanks
Pravin.


----------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <ctypes.h>

int main(int, char*[]);

int main(int argc, char * argv[])
{
char *input;
char type;
int exit=0;

while (exit != 1) {
printf(&quot;Enter the type: &quot;);
fflush(stdin);
scanf(&quot;%c&quot;,&type);

if ((type == 'Q') || (type == 'q')) {
printf(&quot;\nThank you for using this program\n&quot;);
exit = 1;
} else {
printf(&quot;Please enter the input&quot;);
fflush(stdin);
scanf(&quot;%s&quot;,input);
}
}
return 0;
} //End of the line.
 
I think you should #include<conio.h> and change your first scanf to:

type = getch();


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
are you under Unix?

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
There are quite a few problems here

1. int main(int, char*[]);
There is no need to prototype main prior to its declaration

2. char *input;
There is no space associated with this pointer - you're just storing characters in some random memory location
Code:
char input[100];
allocates some space you can use.

3. int exit=0;
exit is also the name of a function in stdlib.h. Should you need to call the exit function, you would need to change the name of this variable.

4. fflush(stdin);
This is undefined - fflush() is defined for output streams, and following writes to update streams.
So whilst it may seem to work for old DOS compilers, it almost certainly does nothing in a unix environment.

5. scanf(&quot;%s&quot;,input);
Simple scanf string conversions have no buffer overflow prevention. scanf() also leaves the input stream in some very odd conditions, which is why you see a lot of attempts at flushing stdin.

Anyway, here's my stab at the problem
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char * argv[])
{
   char buff[100];
   char input[100];
   char type;
   int done=0;

  while (done != 1) {
   printf(&quot;Enter the type: &quot;);
   fflush( stdout );            /* ensure prompt appears */
   if ( fgets( buff, sizeof(buff), stdin ) == NULL ) break;
   type = buff[0];

   if ((type == 'Q') || (type == 'q')) {
     printf(&quot;\nThank you for using this program\n&quot;);
     done = 1;
   } else {
     printf(&quot;Please enter the input: &quot;);
     fflush( stdout );
     if ( fgets( buff, sizeof(buff), stdin ) == NULL ) break;
     sscanf( buff, &quot;%s&quot;,input);
     printf( &quot;You entered '%s'\n&quot;, input );
   }
  }
  return 0;
}
The primary input routine is fgets(), which prevents buffer overflow, and has a simple return result at end-of-file or stream error (it returns NULL).
Having got some user input in a buffer, you can do whatever you want with it.

--
 
Thanks,
It works fine now.
Cheers,
Pravin.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top