I had programmed first an easy calculator. Now I would like to outsource the individual program components in Functions. The Problem is the switch-part.The program always gives me the default message:Bad operator. Please take a look and give me some tipps.Is something wrong with the pointers and double-pointers?
Here is my code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
void newcalc(char*,double*,char*,double*);
double switchfunk(double**, char**, double**);
double readcalc(double**,char**,double**);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
newcalc(&restart,&a,&op,&b);
fflush(stdin);
getchar();
return 0;
}
double switchfunk(double** x, char** opp, double** y)
{
printf("\n ---CALCULATOR--- \n\n\n");
switch (**opp)
{
case '+':printf("%lf + %lf = %lf\n", **x, **y, addition(**x, **y)); break;
case '-':printf("%lf - %lf = %lf\n", **x, **y, subtraction(**x, **y)); break;
case '*':printf("%lf * %lf = %lf\n", **x, **y, multiplication(**x, **y)); break;
case '/':printf("%lf / %lf = %lf\n", **x, **y, division(**x, **y)); break;
default:printf("bad operator!");
}
return 0;
}
void newcalc(char* restart,double* x, char* opp, double* y)
{
while (restart != (char*)'N')
{
readcalc(&x, &opp, &y);
switchfunk(&x, &opp, &y);
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", &restart);
if (restart != (char*)'Y'&&restart != (char*)'N')
{
printf("Bad input!");
}
}
}
double readcalc(double** x,char** opp,double** y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf",x,opp,y);
return 0;
}
double addition(double a,double b)
{
double c = 0;
c = a + b;
return c;
}
double subtraction(double a, double b)
{
double c = 0;
c = a - b;
return c;
}
double multiplication(double a, double b)
{
double c = 0;
c = a*b;
return c;
}
double division(double a, double b)
{
double c = 0;
c = a / b;
return c;
}
Best regards!
You have so many problems in your code, most of them related to your use of pointers. Here's one problem:
In the newcalc function you have the following condition in a loop:
restart != (char*)'N'
That will not work as you expect, in fact it will always be true and give you an infinite loop.
The reason is that restart is a pointer which points to the location of the local restart variable in the main function. It will never be the same as (char *) 'N' (which is a pointer pointing to address 78).
How to solve this specific problem? To start with, don't have it as an argument, declare it as a local (non-pointer!) variable:
char restart = 'y';
Then use it normally in the loop condition
while (restart == 'y' || restart == 'Y') { ... }
And to hint to more pointer problems, remember that scanf want a pointer to the variable where to store the value?
But in e.g. the readcalc function the variables x, opp and y are pointers to pointers to where the data should be stored, and yet you pass these pointers-to-pointer to scanf:
scanf("%lf%c%lf",x,opp,y);
Here, you should use pointers as argument to readcalc function, but not pointers-to-pointers.
In other words, it should be declared as
double readcalc(double*,char*,double*);
You most likely have many other problems with your (unnecessary) use of pointers and pointers-to-pointers, but these were the ones that really stood out.
I think you are misusing pointers and using addresses rather than the values themselves. Also, you are using pointers to pointers while you are not in need to do so. Take a look at the following code.
#include <stdio.h>
#include <stdlib.h>
void newcalc (char*,double*,char*,double*);
double switchfunk(double*, char*, double*);
double readcalc(double*,char*,double*);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
newcalc(&restart,&a,&op,&b);
fflush(stdin);
getchar();
return 0;
}
double switchfunk (double* x, char* opp, double* y)
{
printf("\n ---CALCULATOR--- \n\n\n");
switch ( *opp )
{
case '+':
printf("%lf + %lf = %lf\n", *x, *y, addition(*x, *y));
break;
case '-':
printf("%lf - %lf = %lf\n", *x, *y, subtraction(*x, *y));
break;
case '*':
printf("%lf * %lf = %lf\n", *x, *y, multiplication(*x, *y));
break;
case '/':
printf("%lf / %lf = %lf\n", *x, *y, division(*x, *y));
break;
default:printf("bad operator!");
}
return 0;
}
void newcalc(char* restart,double* x, char* opp, double* y)
{
while ( *restart == 'Y' || *restart == 'y')
{
readcalc(x, opp, y);
switchfunk(x, opp, y);
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", restart);
// if (restart != (char*)'Y'&&restart != (char*)'N')
// {
// printf("Bad input!");
// }
}
}
double readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf",x,opp,y);
return 0;
}
double addition(double a,double b)
{
return a + b;
}
double subtraction(double a, double b)
{
return a - b;
}
double multiplication(double a, double b)
{
return a*b;
}
double division(double a, double b)
{
return a / b;
}
Related
i got problem with this calculator which i need to use the previous result of previous operartion (if i want to ) but when i do it it gives correct if only give it the same previous operation
here is the code :
#include<stdio.h>
#include<math.h>
double addition (double a, double b)
{
double add;
add=a+b;
return add;
}
double soustraction (double a, double b)
{
double sous;
sous=a-b;
return sous;
}
double multiplication (double a,double b)
{
double multi;
multi=a*b;
return multi;
}
double division (double a, double b)
{
double div;
div=a/b;
return div;
}
double reste_de_division (int a,int b)
{
double rest;
rest=a%b;
return rest;
}
double puissance( double a,double b)
{
int i;
double r;
i = 0;
r = 1;
while (i < b){
r = r * a;
i ++;
}
return r;
}
double factorielle (double a)
{
int i;
double facto;
facto=1;
for(i=0;i<a;i++)
{facto=(a-i)*facto;
}
return facto;
}
double PGCD (int a,int b)
{
int i;
double pgcd;
for(i=1; i <= a && i <= b; ++i)
{
if(a%i==0 && b%i==0)
pgcd = i;
}
return pgcd;
}
double PPCM (int a,int b)
{
int ppcm;
ppcm = (a > b) ? a : b;
while(ppcm%a !=0 || ppcm%b !=0)
ppcm=ppcm+1;
return ppcm;
}
double inverse (double a)
{
double inv;
inv=1/a;
return inv;
}
int main(){
double a,b,r;
char ch;
printf("\n***************************************\n");
printf (" \n + Addition \t \t - Subtraction \n * Multiplication \t / Division \n Reste de division \t c Puissance \n ! Factorielle \t \t g PGCD \n p PPCM \t \t i Inverse \n q Quitter \n");
printf("\n***************************************\n");
printf("Donner les deux nombres et l'operation:\n");
do
{b=r;
scanf("%lf %lf %c",&a,&b,&ch);
switch (ch)
{case '+':
r=addition(a,b);
printf("%lf\n",r);
break;
case '-':
r=soustraction(a,b);
printf("%lf\n",r);
break;
case '*':
r=multiplication(a,b);
printf("%lf\n",r);
break;
case '/':
r=division(a,b);
printf("%lf\n",r);
break;
case '%':
printf("%lf\n",reste_de_division(a,b));
break;
case 'c':
printf("%lf\n",puissance(a,b));
break;
case '!':
printf("%lf\n",factorielle(a));
break;
case 'g':
printf("%lf\n",PGCD(a,b));
break;
case 'p':
printf("%lf\n",PPCM(a,b));
break;
case 'i':
printf("%lf\n",inverse(a));
break;
}
}while(ch !='q');
return 0;
}
Sorry for my bad english
example:
5 6 +
11
6 +
17
3 -
20 //this is error, it's supposed to give 14 not 20
Your problem is here:
scanf("%lf %lf %c",&a,&b,&ch);
You expect the user to always input two doubles and one character. If the user only gives one double and one character, the value of ch (and b) is unchanged and the program will do the same operation as it did the last time.
Example:
First input:
5 6 +
This will make a equal 6, b equal 5 and ch equal +. Consequently the program will add the two numbers 5 and 6 and produce the result 11 which is stored in b.
Next input:
1 -
This will make a equal one but leave both b and ch unchanged, i.e. b is still equal 11 and ch is still equal +. Consequently the program will add the two numbers 1 and 11 and produce the result 12 which is stored in b.
This is just one example of things that can go wrong when doing scanf("%lf %lf %c",&a,&b,&ch); There are other input sequences that will lead to (even more) bizar output.
You need a much more strict parsing of user input. The scanf functions isn't good for that. Consider using fgets to read a whole line and the parse it afterwards. With care that can be done using sscanf.
I'm creating a calculator that adds, subtracts, and multiplies complex numbers, but I keep getting errors on my displayComplexNumber. Each time I try to compile, it says, "error: expected expression before ‘double’" or "error: too few arguments to function ‘displayComplexNumber’"
#include <stdio.h>
#include <stdlib.h>
int getMenuChoice();
void getComplexNumber(double* num, double* imagine);
void addComplexNumber(double num1, double imagine1, double num2, double imagine2, double* num, double* imagine);
void displayComplexNumber(double* num, double* imagine);
int main()
{
double num1,imagine1, num2, imagine2, num, imagine;
int choice;
do
{
choice = getMenuChoice();
switch (choice)
{
case 1: // addition
getComplexNumber(&num1, &imagine1);
getComplexNumber(&num2, &imagine2);
addComplexNumber(num1, imagine1, num2, imagine2, &num, &imagine);
displayComplexNumber(double* num, double* imagine)
break;
case 0: // display
break;
default:
break;
}
}while (choice != 0);
return 0;
}
int getMenuChoice()
{
int choice;
printf("1 - addition\n");
printf("0 - EXIT\n");
scanf("%d",&choice);
return choice;
}
void getComplexNumber(double* num, double* imagine)
{
printf("Enter the real component\n");
scanf("%lf", num);
printf("Enter the imaginary component\n");
scanf("%lf", imagine);
}
void addComplexNumber(double num1, double imagine1, double num2, double imagine2, double* num, double* imagine)
{
*num = num1 + num2;
*imagine = imagine1 + imagine2;
}
void displayComplexNumber(double* num, double* imagine)
{
printf("*RESULT*\n");
printf("%.2lf + %.2lfi\n", num, imagine);
}
displayComplexNumber() shouldn't take pointers, it should just take doubles.
void displayComplexNumber(double num, double imagine)
You need to fix this in the prototype at the beginning, and in the function definition.
Then when you call it, you don't put the parameter types. It should just be:
displayComplexNumber(num, imagine);
Ok so I have written this code with four different functions, and the main purpose of it is to display in a table from angles 0-90 what the angle, time, distance of a velocity is. the velocity is inputed from the user.
But when I call the void function that is making the function I get an error "undefined reference to `create_table'" Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define G 9.8 /* gravitation acceleration 9.8 m/s^2 */
#define PI 3.141592654
void create_table(double v);
double Projectile_travel_time(double a, double v);
double Projectile_travel_distance(double a, double v);
double degree_to_radian(double d);
int main(void)
{
int n;
double velocity;
printf ("please enter the velocity at which the projectile is launched (m/sec): ");
n = scanf("%lf" ,&velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
while (velocity < 0 )
{
printf ("please enter a positive number for velocity: ");
n = scanf("%lf", &velocity);
if(n != 1)
{
printf("Invlid input. Bye...");
exit(1);
}
}
create_table(velocity);
return 0;
}
void create_table(double v)
{
printf("Angle t d\n");
printf("(deg) (sec) (m)\n");
double a,i;
for( a=0; a<=90; a+=5)
{
for(i=0; i<=2; i++)
{
double t = Projectile_travel_time(a, v);
double s = Projectile_travel_distance(a, v);
printf("%d %d %d\n", a, t, s);
}
}
}
double Projectile_travel_time(double a, double v)
{
double t = ((2*v*sin(degree_to_radian(a)))/(G));
return t;
}
double Projectile_travel_distance(double a, double v)
{
double d = ((v*v)/G)*sin(2*degree_to_radian(a));
return d;
}
double degree_to_radian(double d)
{
double r = d*atan(1) * 4 / 180;
return r;
}
any help would be appreciated.
thanks
edit I have edited the code but now have encountered another problem with my outputs being completely off. Any suggestions how my functions are incorrect?
You must keep the functions you create outside of the main function
Try to implement your functions outside the main()
You need to move the function definitions out of main. C does not support nested functions.
Edit: That is, in GCC they are, but it's not portable.
I have edited the code but now have encountered another problem with my outputs being completely off.
Change
printf("%d %d %d\n", a, t, s);
to
printf("%lf %lf %lf\n", a, t, s);
You can use %7.3f to align all the values.
Write all the function definitions create_table, Projectile_travel_time, Projectile_travel_distance and degree_to_radian outside the main.
Linker is not able to find the definition of create_table at the point at which you are calling create_table.
I am new to both this site and C programming and I am attempting to make a quadratic formula but I cannot get the roots to work. I believe I am not calling a function or perhaps there is something else wrong. Any help would be appreciated, thank you!
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
float root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is |n%fx^2%+fx%+f=0", a, b, c);
return 0;
}
float root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f,x1 , x2");
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f,x");
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2f,x3i and");
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2f,x4i");
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
You never call the function root() so it will never be executed. Put the function call somewhere before return 0; of main():
root(a, b, c);
Also, fix the printf() calls as mentioned above.
I'm not sure if this will fix everything but in your code you have your print statements formatted incorrectly. For example, this line:
printf("%.2f and %.2f,x1 , x2");
Should be printf("%.2f and %.2f", x1, x2);
The variables whose values you are trying to use should be outside of the quotation marks. Hopefully this helps.
You have several errors.
You never call root().
root is declared to return float, but it doesn't return anything, it just prints the roots. It should be declared void.
You have mistakes in many of your printf() calls. When you're displaying the equation, you have |n instead of \n, and %+f instead of +%f. When you're displaying the roots, you have the variables that you want to print inside the format string, instead of as separate arguments to printf.
Here's the corrected code.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
float userinput(char prompt[]); //Function Prototype
void root(float a, float b, float c);
int main()
{
float a,b,c;
a=userinput("Enter the value for a:"); //Function Call
b=userinput("Enter the value for b:");
c=userinput("Enter the value for c:");
printf("The Equation you entered is \n%fx^2+%fx+%f=0\n", a, b, c);
root(a, b, c);
return 0;
}
void root(float a, float b, float c)
{
float D,x,x1,x2,x3,x4;
D = b*b - 4*a*c;
if(D>0)
{
printf("There are two real roots, the roots are: ");
x1 = ((-b+(sqrt(D)))/(2*a));
x2 = ((-b-(sqrt(D)))/(2*a));
printf("%.2f and %.2f" ,x1 , x2);
}
if(D==0)
{
printf("There is one real root, the root is: ");
x = ((-b)/(2*a));
printf("%.2f",x);
}
if(D<0)
{
printf("There are two imaginary roots. The roots are: ");
x3 = ((-b/2*a)+(sqrt(fabs(D))/(2*a)));
printf("%.2fi and ", x3);
x4 = ((-b/2*a)-(sqrt(fabs(D))/(2*a)));
printf("%.2fi", x4);
}
}
float userinput(char prompt[]) //Function definition
{
float answer;
int status;
do
{
printf("%s",prompt);
status=scanf("%f", &answer);
if(status!=1)
{
fflush(stdin);
printf("INPUT ERROR!\n");
}
}
while(status!=1);
return answer;
}
DEMO
I've a problem with a function in my calculator-program. The function returns me back only 0 values. I want that:
Input:3+4
Output:7
I have worked with pointers to use the call by reference method. Please give me some tips.
The mistake is in the function readcalc. If I write the whole syntax in the main program it works.
Here is my code.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
double readcalc(double*,char*,double*);
double addition(double,double);
double subtraction(double,double);
double multiplication(double,double);
double division(double,double);
int main()
{
double a=0, b=0;
char op='R', restart = 'Y';
while (restart != 'N')
{
readcalc(&a,&op,&b);
printf("%lf%lf", a, b);
printf("\n ---CALCULATOR--- \n\n\n");
switch (op)
{
case '+':printf("%lf + %lf = %lf\n", a, b, addition(a, b)); break;
case '-':printf("%lf - %lf = %lf\n", a, b, subtraction(a, b)); break;
case '*':printf("%lf * %lf = %lf\n", a, b, multiplication(a, b)); break;
case '/':printf("%lf / %lf = %lf\n", a, b, division(a, b)); break;
default:printf("bad operator!");
}
printf("New Calc? (Y,N) \n");
fflush(stdin);
scanf("%c", &restart);
if (restart != 'Y'&&restart != 'N')
{
printf("Bad input!");
}
}
fflush(stdin);
getchar();
return 0;
}
double readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", &x, &opp, &y);
return 0;
}
double addition(double a,double b)
{
double c = 0;
c = a + b;
return c;
}
double subtraction(double a, double b)
{
double c = 0;
c = a - b;
return c;
}
double multiplication(double a, double b)
{
double c = 0;
c = a*b;
return c;
}
double division(double a, double b)
{
double c = 0;
c = a / b;
return c;
}
What can I change?
The problem with readcalc is that you pass the pointer to the pointer as argument to scanf. The variables are already pointers, so you don't have to use the address-of operator to get a pointer, as then you get pointers to the pointers.
Also be careful with the scanf format, as the "%c" format doesn't skip leading whitespace, so if you enter e.g. 1 +2 then the scanf call will not be able to read the operator or the second number.
void readcalc(double* x,char* opp,double* y)
{
printf("\n Type your calculation!(z.B.4+7)\n");
scanf("%lf%c%lf", x, opp, y); // <== no &'s
}
Your readcalc function invokes
scanf("%lf%c%lf", &x, &opp, &y);
on pointers, int *x, etc...
When you place it inline in main, it works on the addresses of the variables.
Change it to
scanf("%lf%c%lf", x, opp, y);
At a suitable warning level, you may have seen warning.
While you are there, readcalc returns a double, which you never use. Perhaps it should simply be void?