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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with interactive program...

Status
Not open for further replies.

NatMinx

Programmer
Jun 24, 2008
10
CA
Ok here's my code...

Code:
for(;;) {
    printf("Please input a command: ");
    fscanf(stdin, "%s %s",cmd,infilename);

    if(strcmp(cmd,"exit")==0) {
        printf("--> Bye!\n");
        exit(0);
    }
    else if(strcmp(cmd,"remove")==0) {
        ..................................
        ..code that needs the infilename..
        ..................................
    }
    else {
        ..more code..
    }
}

Now my problem is, when I'm prompted for a command & I enter "exit" and hit enter, its still waiting for me to type in the infilename. So it doesnt exit right away.

How do I get around this?
Any help is much appreciated...

Thanx! :)
 
Naturally, you ask TWO words in a single fscanf call. Use separate (f)scanf's for cmd name and parameter (infilename).

Always check fscanf (and scanf too) returned value. It returns number of successfully scanned fields. For example:
Code:
if (scanf("%s",cmd) != 1)
{
   /* No cmd value assigned: EOF or i/o error */
   ...
}
...
 
Also after the printf, add
Code:
fflush(stdout);
printfs are buffered and unless they are flushed, you won't see the next one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top