C++ code not compiling correctly

  • C/C++
  • Thread starter Benzoate
  • Start date
  • Tags
    C++ Code
In summary, Dev-C++ found a problem with the code when it tried to evaluate the root1 and root2 values.
  • #1
Benzoate
422
0
I'm pretty sure I wrote the correct code for the quadratic formula and yet Dev-C++ continues to find a problem with my code. I will post the code I've written:



#include<iostream.hpp>
#include<math.h>

int main()
{
double root1,root2,a,b,c,root;

cout << "Enter the coefficients a,b,c: "; ! This is the line where Dev-C++ finds an error
cin >> a >> b >> c;
root=sqrt(b*b-4.0*a*c);
root1=.5*(root-b)/a;
root2=-.5*(root-b)/a;
cout << "The solutions are " <<root1 << " and " <<root2 << "\n";

return(0);

}
 
Technology news on Phys.org
  • #2
You need either std::cout or put a "using namespace std" declaration at the top.
 
  • #3
You might want to try the C++ math library instead:

#include <iostream>
#include <cmath>
using namespace std;

I found this here. You might want to glance over it.
http://forums.devshed.com/c-programming-42/using-std-namespace-what-does-it-mean-45679.html
 
  • #4
As others have pointed out, there are a couple of mistakes. First, functions such as cout, cin, and so on belong to the standard namespace in C++. So if you want to use them in your code you need to do one of two things. The first option is to precede every call to these functions with a declaration that they're from the standard namespace, i.e., instead of

Code:
cout << "This is some text..." << endl;

you should have

Code:
std::cout << "This is some text..." << std::endl;

However, typing std:: each time you call a function from the C++ standard library is cumbersome. Therefore, you can save time by using the following as a preprocessor directive in your code:

Code:
using namespace std;

Typically, you should declare all of the parts of the standard library that your code needs and then include "using namespace std;" on the next line.

The other difficulty is that you should use the cmath library instead of the math library. Both are essentially identical apart from the fact that cmath allows its members to be placed in the standard namespace. So your code should really look like this:

Code:
#include <iostream>
#include <cmath>

int main()
{
  double root1,	root2, a, b, c,	root;

  cout << "Enter the coefficients a, b, c: " <<	endl;
  cin  >> a >> b >> c;

  root = sqrt (b*b - 4.0*a*c);
  root1 =	.5*(root - b) / a;
  root2 =	-.5*(root - b) / a;
  cout << "The solutions are:" << endl
       << "root1 = " <<	root1  << endl
       << "root2 = " <<	root2
       << endl;

  return 0;
}
 

FAQ: C++ code not compiling correctly

Why am I getting "syntax error" messages when I try to compile my C++ code?

Syntax errors occur when the compiler cannot understand the structure or language of your code. This could be due to a missing semicolon, incorrect use of brackets, or misspelled keywords. Check your code for these common mistakes and make sure it follows proper C++ syntax.

What does "undefined reference" mean when I try to compile my C++ code?

This error means that the compiler cannot find the definition of a function or variable that you are referencing in your code. Make sure the function or variable is declared and defined correctly before using it.

How do I fix "no matching function for call" error in my C++ code?

This error occurs when the arguments you are passing to a function do not match the parameters of that function. Check the data types and number of arguments being passed and make sure they match the function's declaration.

Why am I getting "out of scope" errors when I try to compile my C++ code?

This error means that the compiler cannot find a specific variable or object because it is not within the current scope or context. Make sure the variable or object is declared and defined within the correct scope for it to be accessible.

How do I resolve "template instantiation depth exceeds maximum" error in my C++ code?

This error occurs when you are using templates in your code and the compiler is unable to instantiate them due to a large number of recursive template calls. To fix this, try reducing the complexity of your templates or increasing the maximum template instantiation depth limit in your compiler settings.

Similar threads

Replies
22
Views
3K
Replies
5
Views
2K
Replies
6
Views
10K
Replies
4
Views
2K
Replies
23
Views
2K
Replies
39
Views
4K
Replies
13
Views
1K
Replies
7
Views
1K
Back
Top