Project help

Hey everyone! I'm very new to programming (just now taking some beginner courses at school) and I have to write this code ... I'm at a stuck point getting these errors when trying to compile

Error E2451 ProgrammingProject1.cpp 12: Undefined symbol 'input' in function main()

Warning W8019 ProgrammingProject1.cpp 12: Code has no effect in function main()

Error E2292 ProgrammingProject1.cpp 13: Function should return a value in function main()

Warning W8066 ProgrammingProject1.cpp 14: Unreachable code in function main()

Error E2451 ProgrammingProject1.cpp 20: Undefined symbol 'a'

Error E2121 ProgrammingProject1.cpp 20: Function call missing )

The project is supposed to "repeatedly" prompt the user for 3 coefficients a,b,c and then evalute the expression below and display two values for x

x = 3(a+2b)+-sqrt(4-a^2)
c-a

If the user enters -999 for a then the program should terminate without asking for b or c

if -999 is not enter then it will prompt the user on separate lines for b and c then print the two values for x

it should also clear the screen at the beginning of each run

Like I said I'm very new and this project kinda got thrown at us from our professor and I definitely should've started on it earlier lol.

Here's what I got so far

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
#include <iostream.h>											
#include <math.h>

int main()
{
//Variable Declarations
double a, b, c;

//Variable Inputs
cout << "Enter the value of a: ";
cin >> a;
if(a = '-999');
       return;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
}
//Computations
double expression = 3(a+2b)/(c-a)+-sqrt(4-pow(a2))

//Output
if (expression == 0)
   {
   cout << "The answer is ";
   }
if (expression = 0)
   {
   cout << "The answer is 0";
   }
return 0;
There's too much for one post at the moment (others may well chime in, I'm sure)...

if(a = '-999');

This attempts an assignment (the single = sign).

To test if something is equal to a value, use two ....

if ( a == .....

However, "a" is declared as a double. Doubles are notorious for inexact values. Testing a double for 999 might work, it might be off a little. Study the floating point representation for more information (IEE 754).

Numeric literal values are not in single quites. They're naked.

This

3(a+2b)/(c-a)+-sqrt(4-pow(a2))

Looks like algebra, and is incomprehensible to the compiler. Each junction requires an operator, there are no implied multiplications in C/C++ like there is in algebra.

Try something like

3 * ( a + 2 * b )/(c-a) + (-sqrt(4-pow( a * 2 )))

but "pow" does not take 1 parameter, so that's not entirely correct. I don't know if you mean "a" squared or "a times 2", but "a" squared is written "a*a", or maybe pow( a, 2 )

Last edited on
I appreciate it, I am trying to right a^2 , wasn't thinking about just using a * a lol.

Im getting issues when compiling with it recognizing my variables a,b,c

Its saying

Undefined Symbol 'a' 'b' 'c' on line 24, but I don't understand why they're undefined ?
Shouldn't the headers be without the ".h"?
'a', 'b', and 'c' are undefined in your code because the variables are only defined in the function main but you are trying to access those variables outside of that function. Whitepsace helps for readability so looking at it then it's easier to see the scope of variables and the flow of the program
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
#include <iostream>											
#include <cmath>

int main()
{
     //Variable Declarations
     double a, b, c;

     //Variable Inputs
     cout << "Enter the value of a: ";
     cin >> a;
     if(a = '-999');
          return;
     cout << "Enter the value of b: ";
     cin >> b;
     cout << "Enter the value of c: ";
     cin >> c;
}

//Computations
double expression = 3(a+2b)/(c-a)+-sqrt(4-pow(a2))

//Output
if (expression == 0)
        cout << "The answer is ";
if (expression = 0)
   cout << "The answer is 0";
return 0;


Using this, it makes it a lot easier to see what the scope of your variables is. Make sure to have your code in main.

You should also use <iostream> and <cmath> instead of <iostream.h> and <math.h>
Last edited on
Okay, after browsing for a while and realizing I was going about t his WAY wrong, here's where I'm at. It will compile and ask for the value of a,b,c and then it just closes without sending the answer to the equation.

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
#include <iostream.h>											//Hunter Crescenzi
#include <math.h>

int main() {

		float a, b, c, x1, x2, discriminant;



		cout << "Enter the value of a: "<<endl;
		cin >> a;
		cout << "Enter the value of b: "<<endl;
		cin >> b;
		cout << "Enter the value of c: "<<endl;
		cin >> c;
		discriminant = 4 - a * a;

		if (discriminant > 0) {
			x1 = (3 * (a + 2 * b) + sqrt(discriminant)) / (c-a);
			x2 = (3 * (a + 2 * b) - sqrt(discriminant)) / (c-a);
			cout << "x1 = " << x1 << endl;
			cout << "x2 = " << x2 << endl;
}

		else if (discriminant == 0) {
			x1 = (3 * (a + 2 * b) + sqrt(discriminant)) / (c-a);
			cout << "x1 = x2 = " << x1 << endl;
}




return 0;
}
ShivalryIsDead wrote:
It will compile
No it won't.

Replace your 2 lines at the start with
1
2
3
#include <iostream>											
#include <cmath>
using namespace std;

Then it will compile.


No idea what you are trying to solve, but it then gives, e.g.
1
2
3
4
5
6
7
8
Enter the value of a: 
1
Enter the value of b: 
2
Enter the value of c: 
3
x1 = 8.36602
x2 = 6.63397

It will do nothing if your discriminant happens to be < 0 because you have no else block for that possibility and you probably need to warn the user of that.

Please indent correctly.
Last edited on
On line 16: if a > 2 then discriminant will be negative. There is no if case for negative discriminant.
Topic archived. No new replies allowed.