Hello, I am new to C++ and decided to try to create a program that would have a set of formulas where you would input the known data and the program would solve the answer. I started off by trying to add a quadratic formula (ax^2 + bx + c = 0) but when I tried to compile the program, I keep getting errors:
#include iostream
#include stdio.h
#include math.h
#include string
usingnamespace std;
int a = 0;
int b = 0;
int c = 0;
int answer1 = -b + sqrt((b * b)-(4 * a * c));
int answer2 = -b - sqrt((b * b)-(4 * a * c));
string choice= "Nothing.";
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 << "ax^2 + bx + c = 0" << endl;
cout << "" << endl;
cin >> choice;
if(choice="ax^2 + bx + c = 0"){
cout << "Input your a:" << endl;
cin >> a;
cout << "Input your b:" << endl;
cin >> b;
cout << "input your c:" << endl;
cin >> c;
cout << "Your first answer is " << answer1 << "." << endl;
cout << "Your second answer is " << answer2 << "." << endl;
}
}
And this is the error log:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
:1:10: #include expects "FILENAME" or <FILENAME>
:2:10: #include expects "FILENAME" or <FILENAME>
:3:10: #include expects "FILENAME" or <FILENAME>
:4:10: #include expects "FILENAME" or <FILENAME>
:12: error: `sqrt' was not declared in this scope
:13: error: `sqrt' was not declared in this scope
:14: error: `string' does not name a type
: In function `int main()':
C::18: error: `cout' undeclared (first use this function)
:18: error: (Each undeclared identifier is reported only once for each function it appears in.)
:18: error: `endl' undeclared (first use this function)
:24: error: `cin' undeclared (first use this function)
:24: error: `choice' undeclared (first use this function)
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
usingnamespace std;
int a = 0;
int b = 0;
int c = 0;
double answer1 = -b + sqrt((b * b)-(4 * a * c));
double answer2 = -b - sqrt((b * b)-(4 * a * c));
bool choice= "Nothing.";
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 << "ax^2 + bx + c = 0" << endl;
cin >> choice;
if(choice="ax^2 + bx + c = 0"){
cout << "Input your a:" << endl;
cin >> a;
cout << "Input your b:" << endl;
cin >> b;
cout << "input your c:" << endl;
cin >> c;
cout << "Your first answer is " << answer1 << "." << endl;
cout << "Your second answer is " << answer2 << "." << endl;
}
system("pause");
}
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
usingnamespace std;
int a = 0;
int b = 0;
int c = 0;
double answer1 = -b + sqrt((b * b)-(4 * a * c));
double answer2 = -b - sqrt((b * b)-(4 * a * c));
bool choice= "Nothing.";
int main(){
std::cout << "Welcome to the Maths Problem solver, this program has been designed to work out answers for you." << endl;
std::cout << "Pick which kind of formulae do you want to use:" << endl;
std::cout << "ax^2 + bx + c = 0" << endl;
std::cin >> choice;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if(choice="ax^2 + bx + c = 0"){
std::cout << "Input your a:" << endl;
std::cin >> a;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Input your b:" << endl;
std::cin >> b;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "input your c:" << endl;
std::cin >> c;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Your first answer is " << answer1 << "." << endl;
std::cout << "Your second answer is " << answer2 << "." << endl;
}
system("pause");
}
Welcome...
Pick which...
ax^2 + bx + c = 0
//I input ax^2 + bx + c = 0 here:
ax^2 + bx + c = 0
//Immediately outputs these lines:
Input your a:
Input your b:
Input your c:
Your first answer is 0.
Your second answer is 0.
Press any key to continue...
Replace line 23 with a call to the above function I wrote. (:
Edit:
I assume you don't know much about functions?
If you place a function( with code in the braces {} ) after int main(), you will need to 'prototype' the function before main(), so that it knows what it is.
Otherwise, you can just place the function above main, without a prototype.
Also, place usingnamespace std; before the function, or you will need to use std::, as the function will not know to use usingnamespace std;, as it would be declared after the function.
#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 = 0;
int b = 0;
int c = 0;
double answer1 = -b + sqrt((b * b)-(4 * a * c));
double answer2 = -b - sqrt((b * b)-(4 * a * c));
bool choice= "Nothing.";
int main(){
std::cout << "Welcome to the Maths Problem solver, this program has been designed to work out answers for you." << endl;
std::cout << "Pick which kind of formulae do you want to use:" << endl;
std::cout << "ax^2 + bx + c = 0" << endl;
std::cin >> choice;
clearBuffer();
if(choice="ax^2 + bx + c = 0"){
std::cout << "Input your a:" << endl;
std::cin >> a;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Input your b:" << endl;
std::cin >> b;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "input your c:" << endl;
std::cin >> c;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Your first answer is " << answer1 << "." << endl;
std::cout << "Your second answer is " << answer2 << "." << endl;
}
system("pause");
}
This time, I can input the a,b and c, but the answer is always said to be 0. For example, if I input the a,b and c to be 1,2 and 3, the two solutions to x should be -1 and -2 but instead the program outputs 0 and 0. What is the problem?
When you declare double answer1... a, b and c are 0!
So the whole equation is compiled with 0's.
Create a function, then pass it a, b and c. Call this function after the user has entered the values. Then the function will run the code: double answer1 = -b + sqrt((b * b)-(4 * a * c));
With the users values and not 0's.
Edit: ( again! lol )
You don't need to pass variables when you'll be using global variables.
( Variables declared outside a function. )
I really appreciate your help and I really do admire your will to help me :D
I am a bit confused about some of the terms (I am a mega newbie) but this is what the code looks like right now:
#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 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 << "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');
cout << "Your first answer is " << (-b) + sqrt((b * b)-(4 * a * c)) << "." << endl;
cout << "Your second answer is " << (-b) - sqrt((b * b)-(4 * a * c)) << "." << endl;
}
system("pause");
}
It compiles, runs, lets me input a,b and c but it now outputs incorrect answers. For example, setting a,b and c as 1,3 and 2 should output the two answers to be -1 and -2 but instead it outputs -2 and -4. I noticed that it keeps outputting double the correct answers ( ie: -2/2=-1 = correct and -4/2/-2 = correct) so I guess I will just halve the outputted answers!
Thank you so SO much!