In case control 1 for loop is not executing. I don't understand why?
In case control 1 printf function is working but for loop is not. And turboc++ didn't get error and warning message after compiling.
Program I try :-
'''
#include<studio.h>
#include<conio.h>
void main()
{
clrscr();
int fun,inp,i;
printf ("Enter a number =
= ");
scanf ("%d",&inp);
printf ("\n");
printf ("Enter 1 for
reverse the number
\n");
printf ("ENTER = ");
scanf ("%d",&fun);
printf ("\n");
switch (fun)
{
case 1 :
printf ("\n Case 1 \n");
for (i=0;i>=inp;i++)
{
printf ("\n %d \n",inp);
inp=inp-1;
}
break;
}
getch();
}
'''
You may change the for loop to :
for (i=0; i<=inp; i++) // Note the comparison operator
{
printf ("\n %d \n",inp);
inp=inp-1;
}
Or a bit shorter with one less variable :
while (inp >= 0)
{
printf ("\n %d \n",inp);
inp--;
}
Related
The code works fine if I hide the switch case. If I enter invalid int for the switch case, it will execute the printf statements after the switch case. If I enter valid int for the switch case it will enter the switch case and ends there, skipping the printf statements afterward. It looks fine but apparently it is not?
#include<stdio.h>
#include<string.h>
#define OT 14.00
void main ()
{
char name[40];
char position[20];
char position_name[20];
int id, o_time, code;
float allowance, bonus, nett_salary, salary;
printf ("-----------------------------------");
printf ("\n PAYROLL SYSTEM");
printf ("\n-----------------------------------");
printf ("\n 1. Lecturer");
printf ("\n 2. Clerk");
printf ("\n 3. Technician");
printf ("\n-----------------------------------");
printf ("\nEnter staff name : ");
scanf("%[^\n]s",&name);
printf ("Enter staff ID : ");
scanf ("%d", &id);
printf ("Enter position : ");
scanf ("%d", &code);
printf ("Enter salary : RM ");
scanf ("%f", &salary);
switch (code)
{
case 1:
strcpy (position, "Lecturer");
printf ("Enter allowance : RM ");
scanf ("%f", allowance);
nett_salary = salary+allowance;
break;
case 2:
strcpy (position, "Clerk");
printf ("Enter bonus : RM ");
scanf ("%f", bonus);
nett_salary = salary+bonus;
break;
case 3:
strcpy (position, "Technician");
printf ("Enter overtime hours : ");
scanf ("%d", o_time);
nett_salary = salary+(o_time*OT);
break;
default:
strcpy(position,"Invalid");
nett_salary = 0;
}
printf ("\nStaff Name : %s", name);
printf ("\nStaff No. : %d", id);
printf ("\nPosition : %s", position);
printf ("\nSalary : RM%.2f", salary);
printf ("\n------------------------------------");
printf ("\nNett Salary : RM %.2f", nett_salary);
printf ("\n------------------------------------");
}
scanf ("%f", allowance); // In case 1
scanf ("%f", bonus); // In case 2
scanf ("%d", o_time); // In case 3
& is missing in these scanf statments.
The primary problems are in
scanf ("%f", allowance);
where you're missing the & operator to the supplied argument, same for all case statements.
That said, do not use scanf() for user-inputs, it has many drawbacks. Use fgets() instead.
You use this caracter & to put the variable in it's adress
#include<stdio.h>
#include<string.h>
#define OT 14.00
void main ()
{
char name[40];
char position[20];
char position_name[20];
int id=0, o_time=0, code=0;
float allowance, bonus, nett_salary, salary;
printf ("-----------------------------------");
printf ("\n PAYROLL SYSTEM");
printf ("\n-----------------------------------");
printf ("\n 1. Lecturer");
printf ("\n 2. Clerk");
printf ("\n 3. Technician");
printf ("\n-----------------------------------");
printf ("\nEnter staff name : ");
fgets(name,40,stdin);//use fgets insted of scanf
printf ("Enter staff ID : ");
scanf ("%d", &id);
printf ("Enter position : ");
scanf ("%d", &code);
printf ("Enter salary : RM ");
scanf ("%f", &salary);
switch (code)
{
case 1:
strcpy (position, "Lecturer");
printf ("Enter allowance : RM ");
scanf ("%f",&allowance);//you forget &
nett_salary = (float)salary+allowance;
break;
case 2:
strcpy (position, "Clerk");
printf ("Enter bonus : RM ");
scanf ("%f", &bonus);//you forget &
nett_salary = salary+bonus;
break;
case 3:
strcpy (position, "Technician");
printf ("Enter overtime hours : ");
scanf ("%d", &o_time);//you forget &
nett_salary = salary+(o_time*OT);
break;
default:
strcpy(position,"Invalid");
nett_salary = 0;
break;
}
printf ("\nStaff Name : %s", name);
printf ("\nStaff No. : %d", id);
printf ("\nPosition : %s", position);
printf ("\nSalary : RM%.2f", salary);
printf ("\n------------------------------------");
printf ("\nNett Salary : RM %.2f", nett_salary);
printf ("\n------------------------------------");
}
input 0,0,0,0 output no entry insert ,
input 1,1,1,1 ouput all entry insert,
input 1,1,1,0 ouput three inserted entry,
input 1,0,1,0 ouput gate 1 and gate 3/ input 1,1,0,0 output gate 1,gate 2,
input 1,0,1,0 output two entry insert
#include <stdio.h>
main()
{
int n1,n2,n3,n4;
printf ("Enter 1st number : ");
scanf ("%d",&n1);
printf ("Enter 2nd number : ");
scanf ("%d",&n2);
printf ("Enter 3rd number : ");
scanf ("%d",&n3);
printf ("Enter 4th number : ");
scanf ("%d",&n4);
if (n1==0)
{
if (n2==0)
{
printf ("no entry inserted\n");
}
}
if (n3>0)
{
if (n1%2==0)
{
if (n2%2==0)
{
printf (" three inserted entry");
}
}
if (n1%2!=0)
{
if (n3%2!=0)
{
printf ("all entry inserted");
}
}
}
if (n3>0)
{
if (n2%3==0)
{
if (n2%2==0)
{
printf ("two inserted entery");
}
}
if (n1%2!=0)
{
if (n3%4!=4)
{
printf ("gate 1 and gate 2 and gate 3");
}
}
}
}
input 0,0,0,0 output no entry inserted ,
input 1,1,1,1 ouput all entry inserted,
input 1,1,1,0 ouput three inserted,
input 1,0,1,0 ouput gate 1 and gate 3/ input 1,1,0,0 output gate 1,gate 2
input 1,0,1,0 output two inserted entry
I think this is the code, but your question is unclear so I went with my interpretation here. But this 1,0,1,0 was mention 2 times so I used i only once.
#include <stdio.h>
int main(void) {
int n1,n2,n3,n4;
printf ("Enter 1st number : ");
scanf ("%d",&n1);
printf ("Enter 2nd number : ");
scanf ("%d",&n2);
printf ("Enter 3rd number : ");
scanf ("%d",&n3);
printf ("Enter 4th number : ");
scanf ("%d",&n4);
if(!n1&&!n2&&!n3&&!n4){
printf("no entry inserted");
return 0;
}
else if(n1&&n2&&n3&&n4){
printf("all entry inserted");
return 0;
}
else if(n1&&n2&&n3&&!n4){
printf("three inserted entry");
return 0;
}
else if(n1&&!n2&&n3&&!n4){
printf("ouput gate 1 and gate 3");
return 0;
}
else if(n1&&n2&&!n3&&!n4){
printf("output gate 1,gate 2");
return 0;
}
return 0;
}
What is going wrong here? I am getting the error Use of undeclared identifier 'answer'
Here's my code:
if (CalculatorChoice == 1) do {
int a;
int b;
int sum;
char answer;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');
if (CalculatorChoice == 1)
do {
/* ... */
char answer;
/* ... */
} while(answer=='y');
You have declared the variable answer inside the loop block, but it is accessed from the while condition, which is outside the loop block.
You can move the declaration outside the loop body to fix this:
if (CalculatorChoice == 1) {
char answer;
do {
/* ... */
} while(answer=='y');
}
Your version has variable declared inside block, is not accessible out of block
This code changes position
if (CalculatorChoice == 1)
{
char answer;
do {
int a;
int b;
int sum;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');
}
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 ();
}
If my accounts[MAX] has a MAX of 10 and in a loop to prompt the user to input account #s in account[MAX] how do I make a code to see if the amount of accounts has exceeded 10, and tell the user that the max # of accounts has been inputted and no longer accepts input?
here is my do while loop
do
{
printf ("Options Available: \n");
printf ("\n 1 - Enter a transaction");
printf ("\n 2 - View the general journal");
printf ("\n 3 - View the balance sheet");
printf ("\n q - Quit the program\n");
printf ("\nPlease enter 1, 2, 3 or q: ");
option = validateoption();
if (option == '1')
{
printf ("\nEnter an account number (between 1000 and 3999): ");
accounts[i] = validateaccount();
printf ("\n");
printf ("Enter d (debit) or c (credit): ");
debcred[i] = validatedebcred();
printf ("\n");
printf ("Enter transaction amount: ");
amount[i] = validateamount();
printf ("\n");
printf ("\n");
i++;
totalinput++;
}
if (option == '2')
journal(accounts, debcred, amount, &totalinput);
if (option == '3')
balancesheet(accounts, debcred, amount, &totalinput);
} while (option != 'q');
And lets just say it works if you input 1, 2, 3 or q. Now in the accounts[i], if the # of accounts has exceeded 10, what can i write to tell the user that the max number of acounts has been entered and to not accept more input?
my validateaccount function:
long validateaccount() { // VALIDATE INPUT FOR ACCOUNT # IN TRANSACTION FUNCTION
int keeptrying = 1, rc;
long i;
char after;
do
{
rc = scanf ("%ld%c", &i, &after);
if (rc == 0)
{
printf (" **Invalid input try again: ");
clear();
}
else if (after != '\n')
{
printf (" **Trailing characters try again: ");
clear();
}
else if (i < 1000 || i > 3999)
{
printf (" **Invalid input try again: ");
}
else
{
keeptrying = 0;
}
} while (keeptrying == 1);
return i;
}
Replace
if (option == '1')
with
if (option == '1' && totalinput < MAX)
And option 1 is disabled when the maximum number has been reached.
You might want to add the check earlier as well, so you do not printf() option 1 if it has been disabled.
Note: You have both i and totalinput. I think it would be better (more understandable) if you remove i and only use totalinput. (as totalinputs is more descriptive than i)