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:
...
}
Related
I want this program to repeat every time when users wants to continue.
I want user to choose "yes" to continue the program using other options but it's not executable. I used 'if else' statement for that but I am not getting any applicable or satisfying results. Can someone help me on this matter?
#include <stdio.h>
int main() {
float a, b;
int p;
char q;
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo u want to continue:(y for yes and n for no) :");
scanf("%c", &q);
if (q == 'y')
return main();
else
return 0;
}
You need to use a loop for this. Explanations in the comments:
int main() {
float a, b;
int p;
char q;
do // use a loop here
{
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo you want to continue: (y for yes and n for no) :");
scanf(" %c", &q); // use " %c" instead of "%c", the white space
// will absorb any blank space (including newlines)
} while (q == 'y'); // continue loop if user has entered 'y'
return 0;
}
I am writing a program that can calculate the areas of a square, cube, and circle. The program needs to present an error message and allow the user to enter a new choice if they enter something not included in the menu. My problem is that if they type anything includes my menu options then the program still executes. (i.e. -1, 23, 344) I was wondering how to get it to ignore anything after the first character or to read the whole string. Or if there is something better than getchar(). I'm open to any solutions! Thank you!
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int choice;
int lengthsq;
int areasq;
int lengthcube;
int areacube;
int radius;
double circlearea;
printf("Area Calculation\n");
printf("(1) Square\n");
printf("(2) Cube\n");
printf("(3) Circle\n");
fputs("Please make a selction: ", stdout);
while((choice = getchar()) != '\n')
switch (choice) {
case '1':
printf("\nPlease enter the length: ");
scanf("%d", &lengthsq);
while(lengthsq <= 0){
printf("Error! Please enter a positive number: ");
scanf("%d", &lengthsq);
}
areasq = lengthsq * lengthsq;
printf("The area of the square is %d.", areasq);
return 0;
case '2':
printf("\nPlease enter the length: ");
scanf("%d", &lengthcube);
while (lengthcube <= 0) {
printf("Error! Please enter a positive number: ");
scanf("%d", &lengthcube);
}
areacube = 6 * lengthcube * lengthcube;
printf("The surface area of the cube is %d.\n", areacube);
return 0;
case '3':
printf("\nPlease enter the radius: ");
scanf("%d", &radius);
while(radius <= 0){
printf("Error! Pleae enter a postive number: ");
scanf("%d", &radius);
}
circlearea = 3.14159 * radius * radius;
printf("The area of the circle is %.2f.\n", circlearea);
return 0;
case '\n':
case '\t':
case ' ':
break;
default:
printf("\nInvalid choice entered.\n");
fputs("Enter a new choice: ", stdout);
break;
}
}
You could add another switch case for the dash, which would toggle some kind of negative flag and then read a number as you're already doing. If you do not like introducing such a flag, then the best option would be using fgets, which returns the entire input line. But that has the downside that you need to parse the input. I.e. do some string manipulation, which may be slightly more complex than a simple flag parameter.
On the other hand, from the code you attached, I deduct that the only valid input consists of mere numbers (integers). You could just read an integer then with scanf.
I am trying to compile a small bank program in C in visual studio 2012 express. It shows me this error "undeclared identifier" for almost all variables and this one too "syntax error: missing ';' before 'type'".Please tell me the correct syntax.Thank you.
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Welcome to skybank\n");
int deposit,withdraw,kbalance;
char option;
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf_s("%c",option);
int decash,wicash;
switch(option)
{
int balance;
printf("Enter your current Balance\n");
scanf_s("%d",&balance);
case 1:
printf("Enter the amount you want to deposit\n");
scanf_s("%d",&decash);
printf("Thank You\n");
printf("%d have been deposited in your account\n",decash);
break;
case 2:
printf("Enter the amount you want to withdraw\n");
scanf_s("%d",&wicash);
int wibal;
wibal=balance-wicash;
printf("Thank You\n");
printf("%d have been withdrawed from your account\n",wicash);
printf("Your balance is %d\n",wibal);
break;
case 3:
printf("Your balance is Rs.%d\n",balance);
break;
default:
printf("Invalid Input\n");
break;
}
getchar();
}
The Microsoft C compiler only supports a 25 year old version of the language. And one of the limitations is that all variables must be declared before any other statements. So move all your variable declarations to the top of the function.
The next error I can see is the use of scanf_s with the %c format string. You must pass a pointer to the variable, and pass the number of characters to read.
scanf_s("%c", &option, 1);
And likewise you need to pass an address for the read of balance.
You also need to change the switch statement so that it just contains cases. Move the bare instructions outside.
Your reading of option won't work. Because when you check for 1 you are checking for the character with ASCII code 1. Change option to be an int and read using %d.
Perhaps you are looking for something like this:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int deposit,withdraw,kbalance;
int option;
int decash,wicash;
int balance;
int wibal;
printf("Welcome to skybank\n");
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf_s("%d", &option);
printf("Enter your current Balance\n");
scanf_s("%d", &balance);
switch(option)
{
case 1:
printf("Enter the amount you want to deposit\n");
scanf_s("%d", &decash);
printf("Thank You\n");
printf("%d have been deposited in your account\n", decash);
break;
case 2:
printf("Enter the amount you want to withdraw\n");
scanf_s("%d", &wicash);
wibal=balance-wicash;
printf("Thank You\n");
printf("%d have been withdrawed from your account\n", wicash);
printf("Your balance is %d\n", wibal);
break;
case 3:
printf("Your balance is Rs.%d\n", balance);
break;
default:
printf("Invalid Input\n");
break;
}
getchar();
}
Regarding the unidentified variables, try putting all declarations of variables at the top of the main block, something like:
int main()
{
int deposit, withdraw, kbalance, decash, wicash, wibal;
char option;
printf("Welcome to skybank\n");
Older variants of C frown upon mixing variable declarations with code. To my knowledge the C standard of Microsoft's C implementation is pre-C99 so perhaps this could be the issue.
A few other issues that you should look into:
scanf_s("%c",option); - option should be &option as you are taking a pointer to that variable.
Also here: case 1:
You want '1' (as in case '1') instead of plain 1 as it is a char, not an int you want.
Same for the other case checks.
With regards to the scanf_s problems, try compiling with warnings, it should have been pointed out by the compiler.
Finally, you might want to rid your code of the variables you're not using such as kbalance, withdraw and deposit.
do at the beginning of the block in the declaration of the variable for visual c.
E.g.
int main()
{
int deposit,withdraw,kbalance;
char option;
int decash,wicash
int balance;
int wibal;
...
try this code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Welcome to skybank\n");
int deposit,withdraw,kbalance;
char option;
printf("Press 1 to deposit cash\n");
printf("Press 2 to Withdraw Cash\n");
printf("Press 3 to Know Your Balance\n");
scanf("%c",&option);
int decash,wicash;
switch(option)
{
int balance;
printf("Enter your current Balance\n");
scanf("%d",&balance);
case 1:
printf("Enter the amount you want to deposit\n");
scanf("%d",&decash);
printf("Thank You\n");
printf("%d have been deposited in your account\n",decash);
break;
case 2:
printf("Enter the amount you want to withdraw\n");
scanf("%d",&wicash);
int wibal;
wibal=balance-wicash;
printf("Thank You\n");
printf("%d have been withdrawed from your account\n",wicash);
printf("Your balance is %d\n",wibal);
break;
case 3:
printf("Your balance is Rs.%d\n",balance);
break;
default:
printf("Invalid Input\n");
break;
}
getchar();
}
Move this:
int balance;
printf("Enter your current Balance\n");
scanf_s("%d",&balance);
Before the switch statement.
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.
#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.