Stan Stan - 3 months ago 5
C Question

Read Input In An Array, Store -1 Value, And Exit When -1 Is Entered?

I am trying to make a program that first prompts the user with a menu with menu options starting from 0 to 7.

I have a switch statement to check for what numbers are entered. This program is not finished I am starting on the "build list option" where I was the user to enter value to have it stored into an array, when the user enters a "-1" I want it to store the -1 in the array as well store the numbers the user entered.

The problem is: the program does not keep prompting the user to enter a number until a '-1' is entered. Instead it will display the next menu option and exits.

#include <stdio.h>
#include <stdlib.h>
#define SIZE 50

void main ()
{
int i = 0;
int userinput[SIZE];
int x = 0;

do
{
printf("===DSCI0====\n");
printf("0. Build List\n");
printf("1. Display List Forward\n");
printf("2. Display List Backwards\n");
printf("3. Insert into list\n");
printf("4. Append into list\n");
printf("5. Obtain from List\n");
printf("6. Clear List\n");
printf("7. Quit\n");
printf("What is your menu option?\n");
scanf("%d", &i);

} while(i < 0 || i > 7);

switch(i)
{
case(0) :
for (x = 0; x < SIZE; x++);
{
printf("Enter a value for ther array (-1 to quit): \n");
scanf("%x", &userinput[x]);
if(userinput[x] == -1)
{
break;
}
}

case(1) : printf("Display List Fwd\n");
break;
case(2) : printf("Display List Bkwd\n");
break;
case(3) : printf("Insert into list\n");
break;
case(4) : printf("Append into list\n");
break;
case(5) : printf("Obtain from list\n");
break;
case(6) : printf("Clear List\n");
break;
case(7) : printf("Good-Bye\n");
exit(0);
break;
default : printf("Enter a value 0-7\n");
exit(0);
break;
}

}


Update:

The solution I was given helped me with entering digits into my array. However when I try to read it out I am not getting the inputted values.

switch(i)
{
case(0) :
for (x = 0; x < SIZE; x++)
{
printf("Enter a value for ther array (-1 to quit): \n");
scanf("%x", &userinput[x]);
if(userinput[x] == -1)
{
break;
}
}

case(1) :
for (x = 0; x < SIZE; x++)
{
printf("[%i.] %i\n", i + 1, &userinput[x]);
if(userinput[x] == -1)
{
break;
}


My output in this example is:

What is your menu option?
0
Enter a value for the array (-1 to quit):
0
Enter a value for the array (-1 to quit):
1
Enter a value for the array (-1 to quit):
2
Enter a value for the array (-1 to quit):
3
Enter a value for the array (-1 to quit):
-1

1. 698667216
1. 698667216
1. 698667216
1. 698667216
Display List Bkwd


What could be causing this?

Answer

Remove the last semicolon in

for (x = 0; x < SIZE; x++);

in order to put the block after the loop inside the loop and have the system iterate the block.

Then, add break; after the block to iterate in order not to let the system proceed to display the next menu.

Also note that you should use standard int main(void) in hosted environment instead of void main(), which is illegal in C89 and implementation-defined in C99 or later, unless you have some special reason to use non-standard signature.

To answer updated question:

printf("[%i.] %i\n", i + 1, &userinput[x]);

will invoke undefined behavior because data having wrong type is passed to printf(): %i calls for int, but &userinput[x] is type int*. Use userinput[x] without & to print the contents of the array.

Comments