Switch case missing in C program - c

#include<stdio.h>
#include<conio.h>
void inversion(void);
void ways(void);
void prime(void);
void power(void);
void fibonacci(void);
void main(void)
{
char choice;
printf("Enter a choice:\n 1.Perform inversion of digits \n 2.calculate all the ways that a positive number can be get by adding \n 3.Calculate prime numbers in a range\n 4.Calculate power of a number. \n 5.generate a particular numebr of fibonacci");
choice==getche();
switch(choice)
{
case 1: inversion();
break;
case 2: ways();
break;
case 3: prime();
break;
case 4:power();
break;
case 5:fibonacci();
break;
}//switch ends
//printf("Do you want to perform this once more? (Y?N):");
//}
//while (choice=='y'||choice=='Y');
getch();
}
void inversion(void)
{
clrscr();
int num,i,x,y;
printf("Enter a 4 digit number:");
scanf("%d",&num);
for(i=1;i<=4;i++)
{
x=num%10;
y=num/10;
printf("%d",x);
num=y;
}// for ends
getch();
}
void ways(void)
{ clrscr();
int num,i,j;
printf("Enter the number:");
scanf("%d",&num);
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
if(i+j==num)
printf("%d+%d=%d",i,j,num);
getch();
}
void prime(void)
{
int num1,num2,i;
printf("Enter a range separated by space:");
scanf("%d %d",num1,num2);
for(i=2;i<num2;i++)
{ if(num1%i!=0)//is prime
printf("%d",num1);
}//for ends
getch();
}//prime ends
void power(void)
{
int num,index,i,result=1;
printf("Enter the number and its index (eg.2^3)");
scanf("%d %d",&num,index);
for(i=1;i<=index;i++)
result=num*result;
printf("%d",result);
}//power function ends
void fibonacci(void)
{
int num,x=1,y=1,i,z;
printf("Enter the term you want to find in fibonacci series:");
scanf("%d",&num);
if (num==1)
printf("it is the 1st & 2nd term");
for(i=1;i<=num+2;i++)
{
z=x+y;
if (i==num)
printf("%d",z);
x=y;
y=z;
}
printf("%d");
}//fibonacci func ends

There are a lot of problems in your code:
# I would like to suggest you to use getchar() instead of getche() for a character input.
# Now you are using a charcter for numeric options which you could have avoided by defining your choice variable as integer:
int choice;
scanf (%d, &choice);
# also if you were using char choice;
you are having cases as:
case 1: inversion();
break;
while your choice variable was char
so you should have case like:
case '1': inversion();
anyways if you will define choice as int then your code should work fine.
or you can change your cases also with case values in single quotes.

Related

Decimal to Binary in C, result printed in the wrong order

I made a code in c that converts decimal to binary and viceversa, the problem i have is that when i do the convertion from decimal to binary it prints the numbers in the wrong order, for example, if i input the number 123 as a decimal it should print 11110110 but my program converts it as 10111110
here is the code:
#include <stdio.h>
#include <stdlib.h>
main()
{
int i, no, coc, re, opt;
int dec;
int bin, p, c;
printf ("\n1. Decimal to binary \n2. binary to decimal \n choose an option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
printf("\nDecimal No.: ");
scanf("%d",&no);
if (no>2)
{
printf("Your binary No.: ");
for (i=1;1<=no; i++)
{
coc=no/2;
no=coc;
re=coc%2;
printf("%d", re);
}
}
break;
case 2: printf("\nBinary No.: ");
scanf("%d",&bin);
c=1;
dec=0;
while(bin>0)
{
p=0;
p=c*(bin%10);
dec+=p;
c*=2;
bin/=10;
}
printf("Your decimal No.: %d",dec);
break;
default:
printf("\a\n\nError");
break;
}
return 0;
}
If you want your program to work correctly, consider storing the bits in an array and printing it from the last element. But that will be overkill for a simple program. Consider the following simple program. It just uses bitwise operations (generally, considered fast).
#include <stdio.h>
#include <stdlib.h>
static void DecToBin(n)
{
int i=2;
while(i<n)
{
i=i<<1;
}
for(;i>0;i=i/2)
{
printf("%d",i&n);
}
}
main()
{
int no, opt;
int dec;
int bin, c;
printf ("\n1. Decimal to binary \n2. binary to decimal \n choose an option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
printf("\nDecimal No.: ");
scanf("%d",&no);
if (no>0)
{
printf("Your binary No.: ");
void DecToBin(n);
}
break;
case 2: printf("\nBinary No.: ");
scanf("%d",&bin);
c=1;
dec=0;
while(bin>0)
{
dec+=c*(bin%10);
c*=2;
bin/=10;
}
printf("Your decimal No.: %d",dec);
break;
default:
printf("\a\n\nError");
break;
}
return 0;
}
I have used the bitwise method. I hope it helps.
"<<" bitwise left shift
"&" bitwise and.

Lefttriangle(), Righttringle(), Pascaltriangle()

Q : Write three different functions Lefttriangle(),Righttriangle(),Pascaltriangle() in a single C-program and display the triangle as asked by the user interactively.
So I have tried this one. It is printing the options, then I am entering an option, suppose 1 then it is asking for the number "n". Then ,I am entering the number, suppose 4 and pressing enter. But then it isn't showing the left triangle corresponding to 4. It is printing the options again.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void pascaltriangle(int i,int j,int k,int n,int m)
{
for(i=0;i<n;i++)
{
for(k=1;k<=n-i;k++)
{
printf("The Pascal triangle of numbers is as follows - \n");
printf(" ");
}
for(j=0;j<=i;j++)
{
if(j==0||i==0)
m=1;
else
m=m*(i-j+1)/j;
printf(" %d",m);
}
printf("\n",m);
}
}
void lefttriangle(int i,int j,int n)
{
printf("Enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("The left triangle of numbers is as follows - \n");
printf("%d",j);
printf("\n");
}
}
void righttriangle(int i,int j,int k,int n)
{
printf("Enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
printf("The right triangle of numbers is as follows - \n");
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%d",k);
}
printf("\n");
}
}
int main()
{
int choice;
int i,j,k,n, m=1;
do
{
printf("\nEnter the choice below.\n");
printf("*************************\n");
printf("1-> Left Traingle.\n");
printf("2-> Right Triangle.\n");
printf("3-> Pascal Triangle.\n");
printf("*************************\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value of n : ");
scanf("%d",&n);
void lefttriangle( i,j, n);
break;
case 2:
printf("Enter the value of n : ");
scanf("%d",&n);
void righttriangle(i,j,k,n);
break;
case 3:
printf("Enter the value of n : ");
scanf("%d",&n);
void pascaltriangle(i, j, k, n, m);
break;
case 4:
printf("Thank you!\n");
exit(0);
default:
printf("Enter a valid number.\n");
}
}while(1);
return(0);
}
In your switch, when 4 is entered, that case corresponds to:
printf("Thank you!\n");
exit(0);
Only if the user enters 1 will the left triangle function be called.
Further, when calling these functions, you don't put the return type in front of them. You only do that when declaring and/or implementing them.

Using do loop and functions to make a menu. C

What seems to be the problem in my code? I wish to make a program that makes allows the user to choose from a list of choices/menu. The user chooses one and a function runs. After the function executes the menu appears again until the user decides to quit.
My code is as follows:
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do {
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d, &choice");
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d, &x");
int evaluate(int x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d, &x");
int binaryPrinter(int x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("&d, &x");
int hexaPrinter(int x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d, &x, &y");
int militaryTime(int x, int y);
break;
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
This is what is displayed after inputing the number of choice: Process returned -1073741819 <0xC0000005>
-EDIT-
I have recognized the mistake I have made. But the result's still the same.
here's my new code, but same error: Process returned -1073741819 <0xC0000005>
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do {
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice);
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &x);
binaryPrinter(x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("%d", &x);
hexaPrinter(x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d", &x, &y);
militaryTime(x, y);
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
Try this code
#include <stdio.h>
#include <stdlib.h>
int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);
int main(void)
{
int choice, x, y;
do
{
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice);
(void) getchar();
switch (choice){
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &x);
binaryPrinter(x);
break;
case 3:
printf("Enter a decimal number: ");
scanf("&d", &x);
hexaPrinter( x);
break;
case 4:
printf("Enter time in standard format: ");
scanf("%d:%d", &x, &y);
militaryTime( x, y);
break;
default:
printf("Invalid choice. Please choose only among the choices.");
}
}while(choice != 5);
return 0;
}
You have used wrong syntax of the scanaf()
it's not like scanf("%d,&choice");
it is like scanf("%d",&choice);
And also missed the syntax of function call
it's not like int evaluate(int x); it is like evaluate(x)
Your scanf statements are all wrong. It usually takes to parameters. A format string containing format specifiers to determine what kind of value and how many values to read and a pointer to the variable were to store the read value (multiple pointers in case of multiple specifiers).
// What you got so far
scanf("%d, &choice"); // <-- format string and pointer to variables are combined
// How it should look like
scanf("%d", &choice); // format string "%d" containing 1 format specifier to read an int
// and a pointer to the variable choice to store the read value
Ok now to the next mistake. When declaring a function you need to write down the whole function header (returnType functionName(ParameterType1 Parameter1, ParameterType2 Parameter2, ...)) however when you want to call that funtion all it needs is its name and the parameters BUT whithout theire type.
So declaring a function like this int evaluate(int num) like you did at the very beginning of your code is fine but when calling it in your switch all it needs is the name (evaluate) and the values, or variables you want to pass to the function as parameters (evaluate(x)).
So now all together:
printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
scanf("%d", &choice); // scan an integer value and store it in choice
switch (choice)
{
case 1:
printf("Enter number to be evaluated:");
scanf("%d", &x);
evaluate(x);
break;
case 2:
...
}

how to store integer number into a character pointer in c

I am trying to store character as well as integer numbers in the same region through character pointer however, I am not getting the intended result. It is displaying only characters.
Here, I have created a character pointer which should contain atleast both characters and
integers, then should display them properly.
int main()
{
char c,store[30];
char *p=malloc(30);
int choice,i=0,n;
p=store;
while(1)
{
printf("\n********Menu**********");
printf("\n1.Enter a character");
printf("\n2.Enter a Number");
printf("\n3.Enter a double");
printf("\n4.Display the values");
printf("\n0.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter a character:");
fflush(stdin);
scanf("%c",&c);
*p=c;
p++;
break;
case 2:
printf("\nEnter a Number");
scanf("%d",&n);
*p++=n;
break;
case 4:
*p--='\0';
for(i=0;store[i];i++)
printf("%c",store[i]);
break;
case 0:
exit(0);
break;
}
}
getch();
return 0;
}
You can't simply write doubles and int into a char array.
However with following you could see 0-9 :
case 2:
printf("\nEnter a Number"); //Just for 0-9
scanf("%d",&n);
*p=n+48; //Convert to ascii
p++;
break;

static declaration of ‘DisplayMenu’ follows non-static declaration

Why do I keep getting this error? HELP ME this is homeowrk. I'm obiously new to programming help.
$ gcc homework.c
homework.c: In function ‘main’:
homework.c:32:6: error: static declaration of ‘DisplayMenu’ follows non-static declaration
homework.c:11:7: note: previous declaration of ‘DisplayMenu’ was here
#include <stdio.h>
void DisplayMenu();
void numberPlus10();
void numberTimes2();
void numberMinus1();
void numberTimesnumber();
int main (void)
{
int choice;
void DisplayMenu();
scanf("%i", &choice);
switch (choice)
{
case 1:
numberPlus10();
break;
case 2:
numberTimes2();
break;
case 3:
numberMinus1();
break;
case 4:
numberTimesnumber();
break;
default:
break;
}
void DisplayMenu()
{
printf("1. Number + 10\n");
printf("2. Number * 2\n");
printf("3. Number - 1\n");
printf("4. Number * Number\n");
}
void numberPlus10()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number + 10 is %i\n", x + 10);
}
void numberTimes2()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number * 2 is %i\n", x * 2);
}
void numberMinus1()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number - 1 is %i\n", x - 1);
}
void numberTimesnumber()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number squared is %i\n", x * x);
}
}
Pengyu CHEN is ofcourse right! But! You have another error there.
int choice;
void DisplayMenu(); // You should not declare a function here.
scanf("%i", &choice);
I guess you intend to call this function - so just remove "void" from the beginning of the line.
int choice;
DisplayMenu(); // Call DisplayMenu
scanf("%i", &choice);
And ... please read language specs
In C we don't implement functions inside any blocks. Instead functions shall be implemented in global scope.
Remove the very last right bracket and put it right after end of the switch in int main(void), and there shall be no more errors.
EDITED:
First of all.. I'm sure above is why your source code fails to compile.
Also, please check David's answer, since we all believe that you made a function declaration while you're intending to call it -- although this mistake didn't trigger an compile time error.

Resources