Adding "continue Y/N" in a loop

Hello guys! New guy coming right up! :)

Just atarted learning C and I am experimenting with a simple "calculator"-program.

Now I want to add a question at the end to ask if the user wants to go again.
The code below compiles fine and calculations work but when its done it just prints the "do you want to continue" without letting the user decide and then just continues from the top.
Any tips?

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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "functions.h"
#define PI 3.14

int main()
{
    int menu;
    char g;

for (; ;)
    {
        // Welcoming the user and asks for input
        printf("\n\t\tWELCOME TO THE GEOMETRYPWNER\n");
        printf("\nPlease choose a shape to calculate:\n\n");
        printf("1.Area of circle\n2.Surface of cylinder\n
        printf("3.Square Root of a number\n4.Multiplication\n\n");
        printf("Enter input: ");
        scanf("%d", &menu);

        int j;
            switch (menu)
        {
                // input chooses what function to call, else print error message.
                case 1: circle(); break;
                case 2: cylinderArea(); break;
                case 3: squareRoot(); break;
                case 4: multiplication(); break;
                default: printf("Error in input"); break;

        }

        // Asks user if he/she wants to try again. Y continues, N quits the program
        char g;
        printf("\nWant to try again? Y/N");
        scanf("%c", &g);

        if (g == 'y' || g == 'Y')
            continue;
        else if (g == 'n' || g == 'N')
            exit(EXIT_SUCCESS);


    }
return 0;
}
 
Last edited on
Please rewrite your code anew that there will be indents. Did you see such unformatted code in any book on C++?
Hello vlad and thank you for your reply!
I did not learn the indentation from a book :) hehe. Sorry about that.
I hope my edit makes the code more readable.
should work, I did not give me problems
on line 17: you forgot something at the end of the line

the scanf() on line 20 leaves a new line in the stream. The scanf on line 37 reads the newline ('\n') to g and continues. Since '\n' is neither 'n' nor 'N' the loops starts again

Topic archived. No new replies allowed.