I want to make switch case in the switch case and make back switch. if remember we can use if else to make switch case move to the first switch case. thk
this is my current code now thk
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
int a,b;
system("cls");
printf("Cara Membuat daftar Pertanyaan!\n");
printf("1. Matematika\n");
printf("2. Bahasa indonesia\n");
printf("3. Bahasa Inggris\n");
printf("4. Kewarganegaraan\n");
printf("0. Exit \n");
printf("Masukan no dari 1-4 (0) : ");
scanf("%d", &a);
switch(a)
{
case 1 : system("cls");
printf("1. Perkalian\n");
printf("2. Pertambahan\n");
printf("3. Perkurangan\n");
printf("4. Pembagian\n");
printf("5. Kembali \n");
printf("Masukan no dari 1-5 : ");
scanf("%d",&b);
if( b == 5)
{
switch(a);
}
else {
break;
}
break;
default : printf("Error");
}
return 0;
}
Put everything in a while loop, and only allow the loop to run for the Kembali option. You could also implement this as a recursive function, but that can get complicated.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int
main (void)
{
int a;
int b;
int loop = 1;
while (loop) {
loop = 0;
system ("cls");
printf ("Cara Membuat daftar Pertanyaan!\n");
printf ("1. Matematika\n");
printf ("2. Bahasa indonesia\n");
printf ("3. Bahasa Inggris\n");
printf ("4. Kewarganegaraan\n");
printf ("0. Exit \n");
printf ("Masukan no dari 1-4 (0) : ");
scanf ("%d", &a);
switch (a) {
case 0: {
printf ("Exited\n");
break;
}
case 1: {
system ("cls");
printf ("1. Perkalian\n");
printf ("2. Pertambahan\n");
printf ("3. Perkurangan\n");
printf ("4. Pembagian\n");
printf ("5. Kembali \n");
printf ("Masukan no dari 1-5 : ");
scanf ("%d", &b);
if (b == 5) {
loop = 1;
}
break;
}
default: {
printf ("Error\n");
break;
}
}
}
return 0;
}
Related
int main()
{
int choice;
printf("Enter the size of array: ");
scanf("%d", &size);
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
do
{
printf("\n******** Main Menu ********\n");
printf("1. Display\n");
printf("2. Sort\n");
printf("3. Reverse\n");
printf("4. Search\n");
printf("5. Exit\n");
printf("Enter Your Choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
display(size);
break;
case 2:
sort(size);
break;
case 3:
reverse(size);
break;
case 4:
printf("Enter the value to search: ");
scanf("%d", &val);
search(val, size);
break;
case 5:
break;
default:
printf("Invalid Choice\n");
break;
}
} while (choice != 5);
return 0;
}
I was expecting it to show 4 choices and I do the user input, but its running on loop with input 'Invalid Choice'. This is just the part of long code.
Your program doesn't compile due to missing function and variable definitions, and it's also missing includes.
When you ask scanf("%d", &choice) to read an integer but pass something else, like 'a', it will fail, not set choice and it will also leave the unexpected input in the buffer. Below I handle the failure by giving setting choice = 0 which is invalid input, and flush the unexpected data from the input buffer. As we only have 5 valid choices use a char choice.
#include <stdio.h>
#include <stdlib.h>
void display(size_t size) {
printf("display\n");
}
void reverse(size_t size) {
printf("reverse\n");
}
void search(int val, size_t size) {
printf("search\n");
}
void sort(size_t size) {
printf("sort\n");
}
void flush() {
for(;;) {
switch(getchar()) {
case EOF:
case '\n':
return;
}
}
}
int main() {
printf("Enter the size of array: ");
size_t size;
if(scanf("%zu", &size) != 1 || !size) {
printf("invalid size\n");
return 1;
}
int arr[size];
for (size_t i = 0; i < size; i++)
if(scanf("%d", &arr[i]) != 1) {
printf("invalid input\n");
return 1;
}
char choice;
do {
printf("\n******** Main Menu ********\n");
printf("1. Display\n");
printf("2. Sort\n");
printf("3. Reverse\n");
printf("4. Search\n");
printf("5. Exit\n");
printf("Enter Your Choice: ");
choice = 0;
scanf("%hhd", &choice);
switch (choice) {
case 1:
display(size);
break;
case 2:
sort(size);
break;
case 3:
reverse(size);
break;
case 4: {
printf("Enter the value to search: ");
int val;
if(scanf("%d", &val) != 1) {
printf("invalid val\n");
return 1;
}
search(val, size);
break;
}
case 5:
break;
default:
printf("Invalid Choice\n");
break;
}
flush();
} while (choice != 5);
return 0;
}
sample session:
Enter the size of array: 3
1
2
3
******** Main Menu ********
1. Display
2. Sort
3. Reverse
4. Search
5. Exit
Enter Your Choice: 1
display
******** Main Menu ********
1. Display
2. Sort
3. Reverse
4. Search
5. Exit
Enter Your Choice: x
Invalid Choice
******** Main Menu ********
1. Display
2. Sort
3. Reverse
4. Search
5. Exit
Enter Your Choice: 5
The first screen, gotoxy code is working. But in second screen. Nothing happens, like, it does not read the gotoxy code at all. Please enlighten me about the problem.
Here is the 1st screen :
Here is the 2nd screen :
Here is the code. I would love to learn more about gotoxy.
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
void gotoxy( int column, int line );
int main();
int addProduct();
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int choice;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22,8);
printf("1. Add Product\n\n");
gotoxy(22,10);
printf("2. Display Product\n\n");
gotoxy(22,12);
printf("3. Search Product\n\n");
gotoxy(22,14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22,16);
printf("5. Update Product\n\n");
gotoxy(22,20);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
switch(choice)
{
case 1 : addProduct();
break;
case 2 : displayProduct();
break;
case 3 : searchProduct();
break;
case 4 : reorderProduct();
break;
case 5 : updateProduct();
break;
default : printf("Wrong input. Please try again.");
system("cls");
main();
}
return (0);
}
int addProduct()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
do
{
fp = fopen("inventory.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
main();
}
else
{
do{
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
}while(checker != 'Y' && checker != 'N');
if(checker == 'Y'){addProduct();}
if(checker == 'N'){
system("cls");
main();}
}
return(0);
}
I have a stack where I want to push distinct elements. I wrote the following code. But it always checks only first element. If I enter a duplicate value first value, it doesn't accept that, but if I enter a duplicate value of 2nd or 3rd value it accepts it.
For example if I enter 20 and again enter 20 it will not accept the second,
but if I enter 20, 22, 22 it accepts the duplicate. How can I prevent this?
#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int pop(void);
void display(void);
void main ()
{
int choice;
int option = 1;
s.top = -1;
printf ("STACK OPERATION\n");
while (option)
{
printf ("------------------------------------------\n");
printf (" 1 --> Bus Status \n");
printf (" 2 --> Enter Bus \n");
printf (" 3 --> Exit Bus \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");
printf ("Enter your choice\n");
scanf ("%d", &choice);
switch (choice)
{
case 1:
display();
break;
case 2:
push();
break;
case 3:
pop();
break;
case 4:
return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &option);
}
}
/* Function to add an element to the stack */
void push ()
{
int num,i,j,status=0;
if (s.top == (MAXSIZE - 1))
{
printf ("Terminal is Full\n");
return;
}
else
{
printf ("Enter the Bus Number\n");
scanf ("%d", &num);
if(num<1 || num>30) {
printf("This bus Does not Exist.\n");
}
for(i=0;i<5;i++){
if(s.stk[i]==num){
printf("Bus Already in the Terminal\n");
break;
}
else {
s.top = s.top + 1;
s.stk[s.top] = num;
status=1;
break;
}
}
if(status==1)
printf("Bus %d Successfully Entered\n", num);
}
return;
}
You're bus insert loop is looping through and adds a bus the first time it finds s.stk[i] != num. You need to check to see if the bus is in the station ie search the whole stack before inserting a new bus.
for(i=0;i<5;i++) {
if(s.stk[i]==num){
printf("Bus Already in the Terminal\n");
break;
}
else {
s.top = s.top + 1;
s.stk[s.top] = num;
status=1;
break;
}
}
I would like to ask why am i getting Run-time Check Failure #2 When i am doing my program?
I'm very new to C programming.
I'm trying to make a Console application that have some option after they key in Y/N,
But whenever i reach the end of all the option i get that error.
Could anyone tell me how i could solve it & what is the proper way of doing this kind of programming?
#define _CRT_SECURE_NO_WARNINGS // To allow Visual studio to use "scanf" function
#include <stdio.h> // Standard Input output . header
#include <Windows.h>
void codername() {
printf("Coder: Rong Yuan\n");
}
void projectname() {
printf("Project name: NPoly Learning\n");
}
void loadcurrentdate() {
SYSTEMTIME str_t;
GetSystemTime(&str_t);
printf("Date: %d . %d . %d \n"
, str_t.wDay, str_t.wMonth, str_t.wYear);
}
int main() {
char option;
int input;
int mincome, fmember, total;
printf("Do you like to see our option? Y/N \n");
scanf("%s", &option);
if (option == 'y' || option == 'Y') {
printf("1. Display Coder Detail\n");
printf("2. Display Project Name\n");
printf("3. Load Current Date\n");
printf("4. Calculator PCI\n");
printf("5. Exit\n");
scanf("%d", &input);
}
else
exit(1);
switch (input) {
case 1:
codername();
printf("Do you like to return to main?");
break;
case 2:
projectname();
break;
case 3:
loadcurrentdate();
break;
case 4:
printf("Enter your house monthly income: ");
scanf("%d", &mincome);
printf("Enter total family member: (INCLUDING YOURSELF) ");
scanf("%d", &fmember);
total = mincome / fmember;
printf("Total PCI: %d / %d = %d \n", mincome, fmember, total);
system("pause");
break;
case 5:
exit(0);
}
}
scanf("%s", &option);
is wrong as option is a char . So replace %s with %c there.%s should be used for strings (array of characters) and %c is the format specifier used for a character.
Hi I'm trying to make an interactive menu with switch statement in C.
Though I'm unsure of how to trigger a function that has certain arguments.
I'm a total beginner and I'm stumped how to do this.
The function in the switch statement needs the arguments though I would like the function to ask for the numbers. I'm doing this as an assignment and cannot provide the actual code so I made this mock up. Thank you for your help.
Here is an example of code I might use.
#include <stdio.h>
void printMenu()
{
int choice;
do
{
printf("Main Menu:\n");
printf("1) do this\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
function(); /* though this needs the arguments */
break;
}
} while (choice != 7);
int main(void)
{
printMenu();
return 0;
}
void function(int number1, float number2)
{
/*calculation*/
printf("enter your numbers");
/* Not sure how to read the numbers in here */
printf("%d + %d = %d", number1, number2, number1 + number2);
return;
}
If you want the switch to be as minimal as possible then just call another function which takes in input and then calls the function...
case 1:
read_input_and_function()
break;
...
void read_input_and_function(void)
{
printf("Enter your numbers: ");
/* scanf number1, number2 */
function(number1, number2);
}
The function in the switch statement needs the arguments though I
would like the function to ask for the numbers.
How about asking the arguments first , and then calling the function. This way the two arguments can be declared once and be used in other functions of the same switch , but be defined according to the chosen case.
void function1(int, float);
void printMenu()
{
int choice = 0 , num1 = 0;
float num2 = 0;
do
{
printf("Main Menu:\n");
printf("1) do this\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter number 1\n");
scanf("%d",&num1);
printf("\nEnter number 2\n");
scanf("%f",&num2);
function1(num1,num2);
break;
}
} while (choice != 7);
}
#include <stdio.h>
#include <stdlib.h>
#define Pi 3.14159216
/*
*small program of how to create a menu
*/
main ()
{
float degree,radians;
int input;
/*degrees to radians */
float degreesToRadians (float deg)
{
return ((Pi * deg) / 180.0);
}
/*radians to degrees*/
float radiansToDegrees (float rad)
{
return rad * (180 / Pi);
}
void menu ()
{
printf ("\n");
printf ("1. degrees\n");
printf ("2. radians\n");
printf ("3. quit\n");
do
switch (input) {
case 1:
printf ("\n");
printf ("\n");
printf (" Enter value of degrees: ");
scanf ("%f", °ree);
printf ("RADIANS = %f \n\n", degreesToRadians (degree));
menu ();
break;
case 2:
printf ("\n");
printf ("\n");
printf (" Enter value of radians: ");
scanf ("%f", &radians);
printf ("DEGREES = %f \n\n", radiansToDegrees (radians));
menu ();
break;
case 3:
printf (" quiting app \n");
exit (0);
break;
default:
printf ("wrong option\n");
break;
}
while (input != 3);
getchar ();
}
}
menu ();
}