Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6781759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:39:43+00:00 2026-05-26T16:39:43+00:00

Well i have this code to find the paint quality of a room. void

  • 0

Well i have this code to find the paint quality of a room.

void get_room_size(char room_id, int * length, int * width) {
    while (*length <= 0 && *width <= 0) {
        printf("Enter length and width of room %c in feet: ", room_id);
        if (scanf("%d,%d", length, width)) {
            if (*length <= 0) {
                printf("###Error! Length must be a positive value!\n");
            }
            if (*width <= 0) {
                printf("###Error! Width must be a positive value!\n");
            }
            printf("\n");
        } else {
            printf("bad data");
            *length = 0;
            *width = 0;
        }
    }
}

Basically, if i enter

a,1

It will go crazy and keep looping. Whats the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:39:44+00:00Added an answer on May 26, 2026 at 4:39 pm

    The reason it’s going “crazy” is as follows. When scanf fails to read in a as a number (because it’s not numeric, obviously), it won’t advance the file pointer.

    This is one reason why you shouldn’t generally use scanf operations, a failure can leave the file pointer in an indeterminate position (such as if you only scan in 3 of 12 items).

    The other reason is that scanf means “scan formatted” and you would be hard pressed finding anything more unformatted than user input.

    Anyway, back to the failure. Because the file pointer isn’t advanced, the next time you come back to do fscanf, it will try to read that a again (and again and again).

    If you want a decent function for handling user input, look no further than here:

    #include <stdio.h>
    #include <string.h>
    
    #define OK       0
    #define NO_INPUT 1
    #define TOO_LONG 2
    static int getLine (char *prmpt, char *buff, size_t sz) {
        int ch, extra;
    
        // Get line with buffer overrun protection.
        if (prmpt != NULL) {
            printf ("%s", prmpt);
            fflush (stdout);
        }
        if (fgets (buff, sz, stdin) == NULL)
            return NO_INPUT;
    
        // If it was too long, there'll be no newline. In that case, we flush
        // to end of line so that excess doesn't affect the next call.
        if (buff[strlen(buff)-1] != '\n') {
            extra = 0;
            while (((ch = getchar()) != '\n') && (ch != EOF))
                extra = 1;
            return (extra == 1) ? TOO_LONG : OK;
        }
    
        // Otherwise remove newline and give string back to caller.
        buff[strlen(buff)-1] = '\0';
        return OK;
    }
    

    This will input a line from the user, with overflow protection (unlike gets or scanf with unbounded "%s").

    It also flushes to end of line if the input was too long, which will stop the remainder of the line from affecting the next input operation.

    You can then sscanf the buffer to your heart’s content without any concerns re the file pointer.

    The following test program shows how to use this:

    int main (void) {
        int rc;
        char buff[10];
    
        rc = getLine ("Enter string> ", buff, sizeof(buff));
        if (rc == NO_INPUT) {
            // Extra NL since my system doesn't output that on EOF.
            printf ("\nNo input\n");
            return 1;
        }
    
        if (rc == TOO_LONG) {
            printf ("Input too long [%s]\n", buff);
            return 1;
        }
    
        printf ("OK [%s]\n", buff);
    
        return 0;
    }
    

    As an aside, you may want to re-examine your logic for a valid sized room. What you currently have would allow a room to be entered as 7 by -42 feet 🙂

    It’s also not usually good form to rely on the output values being set to specific values on entry. If length and width are (for example) 3 and 4 on entry, this function will exit straight away without asking the user for input.

    The first problem can be fixed by using || instead of &&. The second by initialising the variables to 0 at the start of the function so that the loop is entered.


    For completeness, if you combine that original snippet above (the include statements and the getLine() function) with the following slightly modified get_room_size() function:

    static void get_room_size(char room_id, int * length, int * width) {
        char buff[100];
        *length = *width = 0;
        while ((*length <= 0) || (*width <= 0)) {
            printf("Enter length and width of room %c in feet: ", room_id);
            int rc = getLine (NULL, buff, sizeof (buff));
    
            if (rc == NO_INPUT) {
                printf ("\nEnd of file encountered.\n");
                return;
            }
    
            if (rc == TOO_LONG) {
                printf ("\nInput too long, please retry.\n");
                continue;
            }
    
            if (sscanf(buff, "%d,%d", length, width) != 2) {
                *length = *width = 0;
                printf ("\nInput not in desired form (<number>,<number>), "
                    "please retry.\n");
                continue;
            }
    
            if ((*length <=0) || (*width <= 0)) {
                *length = *width = 0;
                printf ("\nBoth length and width must be greater than zero, "
                    "please retry.\n");
            }
        }
    }
    

    and the very simple test main(), you’ll see a complete program showing how to do it.

    int main (void) {
        int len, wid;
        get_room_size ('x', &len, &wid);
        printf ("Length is %d, width is %d.\n", len, wid);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I have this code in my Managed C++/Cli in Visual Studio 2008, I
I have this code that works well: {livre:empty_name} $.ajax({ url: sent.php, type: post, dataType:
i have this PHP code, and a DataBase with Question, answer1, answer2, Question_id well,
This may well have come up before but the following code is taken from
I have a regular expression to find :ABC:`hello` pattern. This is the code. format
I have written the following code to find the current location. It works well
Well currently I have this: <rich:fileUpload addLabel=Agregar clearAllLabel=Quitar todos clearLabel=Quitar deleteLabel=Quitar doneLabel=Completado uploadLabel=Subir archivos
I have this scenario well, i'll let the model explain. public class ScheduleMonthlyPerDayModel {
I have this routine that works well, but it messes up as it counts
I have this linq query that works well (although it may be written better,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.