Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,070 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,589 people online right now. Registration is fast and FREE... Join Now!




Problems with my input buffer (C programming)

 
Reply to this topicStart new topic

Problems with my input buffer (C programming)

koji126
11 Apr, 2008 - 10:23 PM
Post #1

New D.I.C Head
*

Joined: 28 May, 2007
Posts: 11


My Contributions
This is for a homework assignment and it is done, but I have this little bug which I just can't figure out. I'm guessing it has to deal with the input buffer. So here goes:

My program generates an 8x8 map containing random values from 0 - 9 and also finding the shortest path from one point to another. It's supposed to take user input to either generate a map with new values, specify new points to find the shortest path for, and quitting the program. It continually loops through until the user enters the command to quit.

r - to generate new map
s followed by 4 single spaced numbers - to specify 2 points
q - to quit the program

Here is an example of the problem:

Please enter a command: r
Generating new height map
Please enter a command: error
Please enter a command:

Here is the code with the problem isolated.

CODE

#include <stdio.h>

int main() {
    char command;
    int x0, y0, x1, y1;

    /*
    'q' to quit

    'r' to generate a new random height map

    's x0 y0 x1 y1' to find the cheapest path from (x0,y0) to (x1,y1)
    */
    while(true){
        printf("Please enter a command: ");
        scanf("%c", &command);

        if((command != 'q') && (command != 'r') && (command != 's')){
            printf("error\n");
        }
        else if(command == 'q'){
            printf("quiting...\n");

            return 0;
        }
        else if(command == 'r'){
            printf("generating random height map\n");
        }
        else if(command == 's'){
            scanf("%d %d %d %d", &x0, &y0, &x1, &y1);

            if(((int) x0 > 7) || ((int) y0 > 7) || ((int) x1 > 7) || ((int) y1 > 7) ||
               ((int) x0 < 0) || ((int) y0 < 0) || ((int) x1 < 0) || ((int) y1 < 0)){
                printf("error, values not within range\n");
                return 0;
            }

            printf("x0 = %d\n", (int) x0);
            printf("y0 = %d\n", (int) y0);
            printf("x1 = %d\n", (int) x1);
            printf("y1 = %d\n", (int) y1);
        }
    }
}

User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Problems With My Input Buffer (C Programming)
12 Apr, 2008 - 07:53 AM
Post #2

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
I like to use

command = getch() ; // get the key

and

while(kbhit()) { getch() ; } // flush the keyboard buffer

instead of scanf. you might want to give them a try, they put control back n your hands.
they are in <conio.h>

This post has been edited by jeronimo0d0a: 12 Apr, 2008 - 07:53 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Problems With My Input Buffer (C Programming)
12 Apr, 2008 - 08:46 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
...you really should avoid conio.h if you are writing programs that you would like to be cross platform capable. conio is not standard and really should be avoided unless you only want your program to work on the platform you develop it on.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:04PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month