C++ code not compiling correctly

  • Context: C/C++ 
  • Thread starter Thread starter Benzoate
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary

Discussion Overview

The discussion revolves around a C++ code snippet intended to implement the quadratic formula. Participants are addressing compilation errors encountered in the code, focusing on issues related to namespace usage and library inclusion.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant identifies a specific line in the code where the compiler finds an error, suggesting that the issue may relate to the use of "count" instead of "cout".
  • Another participant suggests using "std::cout" or a "using namespace std" declaration to resolve the issue with standard output functions.
  • A different participant recommends using the C++ math library ("") instead of the C library ("") for better compatibility with the standard namespace.
  • Further clarification is provided on the necessity of either prefixing standard library functions with "std::" or using the "using namespace std;" directive to avoid repetitive typing.
  • One participant provides a corrected version of the code, incorporating the suggested changes, including proper namespace usage and library inclusion.

Areas of Agreement / Disagreement

Participants generally agree on the need to address namespace issues and the preference for using "" over "". However, there is no consensus on the best approach to resolve the compilation errors, as multiple solutions are proposed.

Contextual Notes

Some limitations include potential misunderstandings about the use of namespaces in C++, as well as the implications of using different math libraries. The discussion does not resolve whether one approach is superior to another.

Benzoate
Messages
420
Reaction score
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;

count << "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;
count << "The solutions are " <<root1 << " and " <<root2 << "\n";

return(0);

}
 
Technology news on Phys.org
You need either std::count or put a "using namespace std" declaration at the top.
 
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
 
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;
}
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 39 ·
2
Replies
39
Views
5K
Replies
10
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K