#menu selection infinite loop

40 messages · Page 1 of 1 (latest)

misty locust
#

When i try putting a letter in the area meant for numbers it kept infinite looping the error message and returning to the menu

sterile agateBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@misty locust

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

misty locust
#

int menu(struct info data[], long int count){
int X;
printf("What do you want to do?\n");
printf("1. Display Data\n");
printf("2. Search Data\n");
printf("3. Sort Data\n");
printf("4. Export\n");
printf("5. Exit\n");
printf("Your choice: ");
scanf("%d", &X);

if(X == 1)
    display(data, count); 
else if(X == 2)
    search(data, count);
else if(X == 5)
    return 0;
else{
    printf("error, not found!");
    return menu(data, count);
}
return menu(data, count);

}

simple shore
#

https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

The first argument to scanf() is a format string, describing what scanf() should parse. The important thing is: scanf() never reads anything it cannot parse. In our example, we tell scanf() to parse a number, using the %d conversion. Sure enough, abc is not a number, and as a consequence, abc is not even read. The next call to scanf() will again find our unread input and, again, can't parse it.

misty locust
#

what is a parse ;-;

kind sundial
#

yes, do something like this:

while (scanf("%d", &x) != 1)

simple shore
#

Also it looks like your file in c++. If you're in c++, you have easier options for user input

misty locust
#

im on C

#

not c++ ;-;

simple shore
#

Your file ext is cpp

misty locust
#

the name of the app is just dev c++

simple shore
#

Oh

misty locust
kind sundial
#

it will make sure the user enters valid input

#

because scanf returns an int

#

if it is 1 it means it failed

misty locust
#

but the scanf will input 1 - 5

#

oooh

misty locust
kind sundial
#

index of what?

misty locust
#

for X?

#

like how else it would know if it is 1 or not

kind sundial
#

no, scanf takes in the address of x

misty locust
#

ye

kind sundial
#

so whatever you input, x will get replaced with it

#

and if scanf sees that you entered in a character for example

#

it will return 1 indicating a failure

#

which is why while (scanf("%d", &x) != 1) checks for that

misty locust
#

where do i put it tho

kind sundial
#

where youre calling scanf

misty locust
#

wait...

#

so...
while (scanf("%d") != 1){
scanf("%d", &X);
}???

simple shore
kind sundial
#

do something like this:

while (scanf("%d", &x) != 1)
{
while (getchar() != '\n')
{ }
printf("FAILED\n");
}

#

the inner loop of "getchar" will clear the input buffer

misty locust
#

what if i just do X != 1, 2, 3, 4, 5 then print error

sterile agateBOT
#

@misty locust Has your question been resolved? If so, type !solved :)

misty locust
#

!solved