Do...While...Not working

Joined
Feb 15, 2023
Messages
1
Reaction score
0
I am trying to re-promt variables.

In the first variable, the question "Start Size" should be promt for the user if the user enters an number below 9. It´s not working. If for instance I enter number 5 it will ask me "End Size" instead of asking "Start Size" again.

In the second variable, "End Size" should be re-prompted if the number entered is less than or equal to "Start Size". Not working either. I have revised it and compared it to many other similar programmes and I cannot find the error. See the coding below:

Code:
#include<stdio.h>

int main(void)
{
    //Ask user start size
    {
        int x;
        do
        {
            x = get_int("Start Size: ");
        }
        while (x < 9);
    }
    //Ask user end size
    {
        do
        {
            y = get_int("End Size: ");
        }
        while (y <= x);
    }
}
 
Joined
Jan 30, 2023
Messages
108
Reaction score
13
I am trying to re-promt variables.

In the first variable, the question "Start Size" should be promt for the user if the user enters an number below 9. It´s not working. If for instance I enter number 5 it will ask me "End Size" instead of asking "Start Size" again.

In the second variable, "End Size" should be re-prompted if the number entered is less than or equal to "Start Size". Not working either. I have revised it and compared it to many other similar programmes and I cannot find the error. See the coding below:

Code:
#include<stdio.h>

int main(void)
{
    //Ask user start size
    {
        int x;
        do
        {
            x = get_int("Start Size: ");
        }
        while (x < 9);
    }
    //Ask user end size
    {
        do
        {
            y = get_int("End Size: ");
        }
        while (y <= x);
    }
}
The issue with your code is that you are defining your variables inside separate blocks, so they are not accessible to each other. Additionally, you are not checking for valid input in the second loop.

Here's how you can modify your code to achieve the desired behavior:

C:
#include <stdio.h>

int main(void) {
    int x, y;
    
    // Ask user for start size
    do {
        x = get_int("Start Size: ");
    } while (x < 9);
    
    // Ask user for end size
    do {
        y = get_int("End Size: ");
        if (y <= x) {
            printf("End size must be greater than start size.\n");
        }
    } while (y <= x);
    
    printf("Start size: %i, End size: %i\n", x, y);
    return 0;
}

In this modified code, the variables x and y are defined in the same block, so they are accessible to each other. The first loop prompts the user for the start size, and keeps doing so until the input is greater than or equal to 9. The second loop prompts the user for the end size, and checks if the input is less than or equal to the start size. If it is, it prints an error message and continues the loop until the input is valid. Finally, the program prints the values of x and y.

I hope this helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,707
Messages
2,569,344
Members
44,635
Latest member
Matt231

Latest Threads

Top