Solving Unknown Error in C++ Code

  • Context: Comp Sci 
  • Thread starter Thread starter SOHAWONG
  • Start date Start date
  • Tags Tags
    C++ Error
Click For Summary

Discussion Overview

The discussion revolves around identifying and resolving errors in a C++ code snippet related to polynomial input. The focus is on debugging and correcting syntax and logical errors in the code.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes a missing closing brace for the main() function, suggesting this is a likely source of the error.
  • Another participant points out the absence of an else clause at the end of the if-else statements, indicating a potential logical flaw.
  • It is mentioned that the code lacks input statements for the coefficients of the polynomial and that variables for these coefficients have not been declared.
  • A participant provides a corrected version of the code, addressing the identified issues.

Areas of Agreement / Disagreement

Participants generally agree on the errors identified in the original code, with multiple suggestions for corrections being offered. However, there is no explicit consensus on the completeness of the solution or further implications of the errors.

Contextual Notes

Limitations include the potential for additional errors not identified in the discussion, as well as assumptions about the user's intent regarding input handling and variable declarations.

Who May Find This Useful

Individuals interested in debugging C++ code, particularly those working with polynomial functions and user input handling.

SOHAWONG
Messages
16
Reaction score
0
after typing these following code
#include <iostream>
using namespace std;
int main()
{int deg;

count << "Enter the degree of the polynomial:" << endl;
cin >> deg ;
count << "For the polynomial f(x)=";
if (deg ==3)
{
count<<"For the polynomial f(x)=C1*x^3+C2*x^2+C3*x^1+C4, enter C1, C2, C3, C4 in order:";
}
else if(deg==2)
{
count<<"For the polynomial f(x)=C1*x^2+C2*x^1+C3, enter C1, C2, C3 in order:";
}
else if (deg==1)
{
count<<"For the polynomial f(x)=C1*x+C2, enter C1, C2 in order:";
}

return 0;

when running this code
error occurs,but the editor doesn't indicate me where's the problem
 
Physics news on Phys.org
One obvious error is that there is no } to finish the main() subroutine. There may be others...
 
Use [ code] and [ /code] (without the leading spaces) for your code.

Code:
#include <iostream>
using namespace std;
int main()
{
   int deg;
   cout << "Enter the degree of the polynomial:" << endl;
   cin >> deg ;
   cout << "For the polynomial f(x)=";
   if (deg ==3)
   {
      cout<<"For the polynomial f(x)=C1*x^3+C2*x^2+C3*x^1+C4, enter C1, C2, C3, C4 in order:";
   }
   else if(deg==2)
   {
      cout<<"For the polynomial f(x)=C1*x^2+C2*x^1+C3, enter C1, C2, C3 in order:";
   }
   else if (deg==1)
   {
      cout<<"For the polynomial f(x)=C1*x+C2, enter C1, C2 in order:";
   }

   return 0;

As phyzguy points out, you're missing the final brace at the end of main.

There is another error as well - you are missing an else clause at the end of your if ... else if clauses.

Also, you apparently want the program user to enter the coefficients of the various powers of x, but you don't have input statements to do this, and you don't have variables declared to hold c1, c2, etc.
 
thx a lot,problem fixed finally
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 25 ·
Replies
25
Views
15K
  • · Replies 1 ·
Replies
1
Views
2K