I am learning about loops such as for and while loops, so I decided to put myself to the test and write a program which you can see the code for below. The program gives the user a range of options to enter an option, but the problem I have is that i want to be able to continuously ask the user to "Enter a command" after an operation has completed.
For example, if I entered 1, the necessary code would be executed but then the whole program just ends. How can I enhance this program so it continuously asks the user to enter new commands until the user forcibly exits by entering 0?
#include <stdio.h>
int main()
{ int n; int credit = 0; int YN; printf("Welcome to Cash booking software Version 2.145\n"); printf("--------------------------------------------------------\n"); printf("Use the following options:\n"); printf("0 -- Exit\n"); printf("1 -- Display Credit\n"); printf("2 -- Change Credit\n"); printf("3 -- Remove Credit\n"); printf("\n"); for ( ; ; ) { printf("Enter a command: "); scanf("%d", &n); if (n == 0) { return 0; } else if (n == 1) { printf("Your credit is £%d", credit); } else if (n == 2) { printf("Enter a new credit value: \n"); scanf("%d", &credit); printf("Your new credit value is %d", credit); } else if (n == 3) { printf("Are you sure you want to remove your Credit value? (Y=1/N=2)"); scanf("%d", &YN); if (YN == 1) { credit = 0; } else ; } return 0; }
} 6 2 Answers
As other users explained return 0; inside of the loop is what is causing the problem and moving it out would solve it, But since you're learning about loops I think this is a great example to teach you something.
Usually you should only use a for loop when you have some parameter that defines de number of times the loop will be executed. The fact that you just used for( ; ; ) is a huge red flag for ther is a betther way to do this.
For example the proper way to write an infinite loop in c is while(1){//code in the loop}. So you could change your for loop with this and it will work fine (relocating return 0; in the correct location).
But since in this code you dont really want an infinite loop (usually they're a bad idea), but you want the loop to run until is pressed 0, the best solution is to use a do{} while(); loop where inside of do you check if either 1, 2 or 3 is pressed and perform their functionality, and then in the while condition you check if 0 has been pressed, and only in that case the program exits.
This is how the code would look like:
do{ printf("Enter a command: "); scanf("%d", &n); if (n == 1){ printf("Your credit is £%d\n", credit); // \n added } else if (n == 2){ printf("Enter a new credit value: \n"); scanf("%d", &credit); printf("Your new credit value is %d\n", credit); // \n added } else if (n == 3){ printf("Are you sure you want to remove your Credit value? (Y=1/N=2):"); scanf("%d", &YN); if (YN == 1){ credit = 0; } }
} while(n != 0);
return 0;Also note that I added \n in some printf() commands for better visualization.
use continue statement at the end of each condition , and you can use break statement instead of return 0, so the code will be :
if (n == 0) { break; } else if (n == 1) { printf("Your credit is £%d ", credit); continue; } else if (n == 2) { printf("Enter a new credit value: \n"); scanf("%d", &credit); printf("Your new credit value is %d ", credit); continue; } else if (n == 3) { printf("Are you sure you want to remove your Credit value? (Y=1/N=2) : "); scanf("%d", &YN); if (YN == 1) { credit = 0; continue; } else { continue; } 1