Need help with menu control 'C'

How to avoid terminating the program if someone type 0 (zero)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <windows.h>

unsigned temp, status, options;

void add_sub();
void mult_dev();
void increm_decrem();
void average();

void menu()
{
    printf("\n\t\t\t    * Calculator *\n\n");
    printf("\n\tChoose an option"
           "\n\n\n 1. Addition and subtraction operations"
           "\n 2. Multiply and divide operations"
           "\n 3. Increment and Decrement operations"
           "\n 4. Average of n numbers using arrays"
           "\n 5. Exit program"
           "\n\n Select: ");
    status = scanf("%d", &options);
    while(status != 1)
    {
        while((temp = getchar()) != EOF && temp != '\n')
        {
            printf("\n\n Invalid option..");
            Sleep(1500);
            printf("\n\n Choose 1, 2, 3, 4 or 5\n");
            Sleep(1500);
            system("cls");
            printf("\n\t\t\t    * Calculator *\n\n");
            printf("\n\tChoose an option"
                   "\n\n\n 1. Addition and subtraction operations"
                   "\n 2. Multiply and divide operations"
                   "\n 3. Increment and Decrement operations"
                   "\n 4. Average of n numbers using arrays"
                   "\n 5. Exit program"
                   "\n\n Select: ");
            status = scanf("%d", &options);
        }
    }
}

int main()
{
    menu();
    int i;
    for(i = 0; i < options;)
    {
        if(options == 1)
        {
            printf("\n\n\t 1. Addition and subtraction operations");
            add_sub();
        }
        else if(options == 2)
        {
            printf("\n\n\t 2. Multiply and divide operations");
            mult_dev();
        }
        else if(options == 3)
        {
            printf("\n\n\t 3. Increment and Decrement operations");
            increm_decrem();
        }
        else if(options == 4)
        {
            printf("\n\n\t 4. Average of n numbers using arrays");
            average();
        }
        else if(options == 5)
        {
            printf("\n\n\t 5. Exit program");
            printf("\n\n\t\tPress any key to close the console !");
            Sleep(1500);
            system("cls");
            break;
        }
        else
        {
            printf("\n\n Invalid option..");
            Sleep(1500);
            printf("\n\n Choose 1, 2, 3, 4 or 5\n");
            Sleep(1500);
            system("cls");
            menu();
        }
    }
    return 0;
}
.
.
.
.


If anything else is type the program run correctly and does what I have write to do, but pressing 0(zero) the program terminates.
How to avoid that ?
Ok.. got it.. while I was checking the code I found this..

 
 while((temp = getchar()) != EOF && temp != '\n')


I had to change temp != '\0' the escape sequences instead of '\n'.

Ty everyone..
Last edited on
> while((temp = getchar()) != EOF && temp != '\n')
Given that EOF is guaranteed to be a negative, having temp as an unsigned is going to lead to surprises at some point.

Also, try to return results rather than relying on global variables.
Topic archived. No new replies allowed.