Hello! I am trying to code a program that can solve quadratic equations. The problem I seem to be having is that when I try to compile it, it comes up with errors leading to the math.h file. This is the code:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
usingnamespace std;
void clearBuffer()
{
cin.clear();
cin.ignore( numeric_limits< streamsize >::max(), '\n' );
}
int a;
int b;
int c;
bool choice= "Nothing.";
int quadratica (int a, int b, int c)
{
int z;
z=(-b)+pow(sqrt(b)-(4*a*c));
return(z);
}
int quadraticb (int a, int b, int c)
{
int x;
x=(-b)-pow(sqrt(b)-(4*a*c));
return(x);
}
int main(){
cout << "Welcome to the Maths Problem solver, this program has been designed to work out answers for you." << endl;
cout << "Pick which kind of formulae do you want to use:" << endl;
cout << "1) ax^2 + bx + c = 0" << endl;
cin >> choice;
{
clearBuffer();
}
if(choice="1"){
cout << "Input your a:" << endl;
cin >> a;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Input your b:" << endl;
cin >> b;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "input your c:" << endl;
cin >> c;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
{
int x;
x = quadratica (a,b,c);
cout << "Your first answer is " << x << endl;
}
{
int y = quadraticb (a,b,c);
cout << "Your second answer is " << y << endl;
}
system("pause");
return 0;
}
and this is the error log(that was found on the math header file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Compiler: Default compiler
Executing g++.exe...
g++.exe "E:\c++mathsmonster122PROTOTYPE.cpp" -o "E:\c++mathsmonster122PROTOTYPE.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:/Dev-Cpp/include/math.h: In function `int quadratica(int, int, int)':
C:/Dev-Cpp/include/math.h:150: error: too few arguments to function `double pow(double, double)'
E:\c++mathsmonster122PROTOTYPE.cpp:25: error: at this point in file
E:\c++mathsmonster122PROTOTYPE.cpp:25: warning: converting to `int' from `double'
C:/Dev-Cpp/include/math.h: In function `int quadraticb(int, int, int)':
C:/Dev-Cpp/include/math.h:150: error: too few arguments to function `double pow(double, double)'
E:\c++mathsmonster122PROTOTYPE.cpp:32: error: at this point in file
E:\c++mathsmonster122PROTOTYPE.cpp:32: warning: converting to `int' from `double'
Execution terminated
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
usingnamespace std;
void clearBuffer()
{
cin.clear();
cin.ignore( numeric_limits< streamsize >::max(), '\n' );
}
double a;
double b;
double c;
bool choice;
double quadratica (double a, double b, double c)
{
return -b + sqrt(b*b - 4*a*c);
}
int quadraticb (double a, double b, double c)
{
return -b - sqrt(b*b - 4*a*c);
}
int main(){
cout << "Welcome to the Maths Problem solver, this program has been designed to work out answers for you." << endl;
cout << "Pick which kind of formulae do you want to use:" << endl;
cout << "1) ax^2 + bx + c = 0" << endl;
cin >> choice;
{
clearBuffer();
}
if(choice="1"){
cout << "Input your a:" << endl;
cin >> a;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Input your b:" << endl;
cin >> b;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "input your c:" << endl;
cin >> c;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
{
int x;
x = quadratica (a,b,c);
cout << "Your first answer is " << x << endl;
}
{
int y;
y = quadraticb (a,b,c);
cout << "Your second answer is " << y << endl;
}
system("pause");
return 0;
}
But I still get these errors:
1 2 3 4 5 6 7 8 9 10 11 12
Compiler: Default compiler
Executing g++.exe...
g++.exe "E:\c++mathsmonster122PROTOTYPE.cpp" -o "E:\c++mathsmonster122PROTOTYPE.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
E:\c++mathsmonster122PROTOTYPE.cpp: In function `int quadraticb(double, double, double)':
E:\c++mathsmonster122PROTOTYPE.cpp:29: warning: converting to `int' from `double'
E:\c++mathsmonster122PROTOTYPE.cpp: In function `int main()':
E:\c++mathsmonster122PROTOTYPE.cpp:58: warning: converting to `int' from `double'
E:\c++mathsmonster122PROTOTYPE.cpp:70: error: expected `}' at end of input
Execution terminated
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
usingnamespace std;
void clearBuffer()
{
cin.clear();
cin.ignore( numeric_limits< streamsize >::max(), '\n' );
}
double a;
double b;
double c;
char choice;
double quadratica (double a, double b, double c)
{
return -b + sqrt(b*b - 4*a*c);
}
double quadraticb (double a, double b, double c)
{
return -b - sqrt(b*b - 4*a*c);
}
int main(){
cout << "Welcome to the Maths Problem solver, this program has been designed to work out answers for you." << endl;
cout << "Pick which kind of formulae do you want to use:" << endl;
cout << "1) ax^2 + bx + c = 0" << endl;
cin >> choice;
{
clearBuffer();
}
if(choice == "ax^2 + bx + c = 0"){
cout << "Input your a:" << endl;
cin >> a;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Input your b:" << endl;
cin >> b;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "input your c:" << endl;
cin >> c;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
{
int x;
x = quadratica (a,b,c);
cout << "Your first answer is " << x << endl;
}
{
int y;
y = quadraticb (a,b,c);
cout << "Your second answer is " << y << endl;
}
}
system("pause");
return 0;
}
which still seems to return this error log:
1 2 3 4 5 6 7 8 9 10 11 12
Compiler: Default compiler
Executing g++.exe...
g++.exe "E:\c++mathsmonster122PROTOTYPE.cpp" -o "E:\c++mathsmonster122PROTOTYPE.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
E:\c++mathsmonster122PROTOTYPE.cpp: In function `int main()':
E:\c++mathsmonster122PROTOTYPE.cpp:46: error: ISO C++ forbids comparison between pointer and integer
E:\c++mathsmonster122PROTOTYPE.cpp:58: warning: converting to `int' from `double'
E:\c++mathsmonster122PROTOTYPE.cpp:63: warning: converting to `int' from `double'
Execution terminated
choice is a char. You are trying to compare it with a string of 23 characters.
From the compiler's perspective, you are trying to compare a char with a pointer to a string, which is not valid.
I can only repeat my previous recommendation: if (choice == '1')
I don't understand, it doesn't matter whether I switch choice to "1", whether I change the type of 'choice', it always keeps up coming with the same error. :(
What's the point of if( choice == ... )? You should be using string choice instead if char choice (on line 20) if you want to compare to a string, and 'single quotes' (not "double quotes") if you want to compare to a charecter. But why bother with the comparison at all?
I fixed this problem by converting choice back to bool!
@Lynx876
Thank you for helping me again! :D I didn't notice that :S Kept using double until I realized what did you and Chervil mean! Thanks :D
@Mathead200
because I want to add more functions in the future, this was just the first one that I wanted to add.