Problem with program

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include iostream
#include stdio.h
#include math.h
#include string

using namespace 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)


Any help would be appreciated. Thanks
Last edited on
When you include header files use <>

i.e.
#include <iostream>

Also, line 23 has no purpose other than outputting an newline;
You could just add this to line 22:
cout << "ax^2 + bx + c = 0" << endl << endl;
Last edited on
Thanks Lynx!

new code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace 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");
}


but now, it doesn't let me input a,b or c :S
I did and I saw the std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
line, I decided I should add that in. Still no luck :S
Add it after each cin >> int. It will remove the '\n'( newline ) that's remaining in the buffer.

Edit:
Sorry, I thought 'choice' was an int.

But either way, this should run through the if-statement. This is because you have used the assignment operator, instead of comparison. Line 25.

=/==
Last edited on
Thank you! I will try that in one second.
I rewrote the code to make it look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace 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");
}


Still no luck :(
Am I doing something wrong?
You can enter the values for a, b and c now though?

Edit:
Sorry! Also, clear() the cin too!.

It may be worth creating a function and adding the lines in there. Then just call it when needed:
1
2
3
4
5
void clearBuffer()
{
    cin.clear();
    cin.ignore( numeric_limits< streamsize >::max(), '\n' );
}


Then just call this function to clear the input buffer.

You don't actually need to add std::, as you have using namespace std;
Last edited on
No :S It just skips all the way to the end as so:

1
2
3
4
5
6
7
8
9
10
11
12
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...


Without letting me input a,b or c :(
...call this function to clear the input buffer.

So do I write it after using namespace std; but before int main(){?
And if so, when exactly do I call it?
Thanks
Did you see my edit?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//header includes

//function prototypes
void clearBuffer();

int main()
{
    clearBuffer();

    return 0;
}

void clearBuffer()
{
    cin.clear();
    cin.ignore( numeric_limits< streamsize >::max(), '\n' );
}


Otherwise, you can just place the function above main, without a prototype.

Also, place using namespace std; before the function, or you will need to use std::, as the function will not know to use using namespace std;, as it would be declared after the function.

Some info on functions:
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Hm, thanks for the info. I changed the code to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace 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?
Scrap my last comment, I just read your last edit
This is where you'll need another function.

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. )
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>

using namespace 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!
IT WORKS! Thanks Lynx! :D
You're welcome! Happy coding! (:
you have used the assignment operator, instead of comparison. Line 25.
http://www.hqew.net
Topic archived. No new replies allowed.