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?
#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;
}
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.
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