Comp Sci Solving Unknown Error in C++ Code

  • Thread starter Thread starter SOHAWONG
  • Start date Start date
  • Tags Tags
    C++ Error
AI Thread Summary
The discussion addresses an unknown error in a C++ code snippet related to polynomial input. Key issues identified include a missing closing brace for the main function and the absence of an else clause for handling invalid input. Additionally, the code lacks input statements for the coefficients and variables to store them. After addressing these errors, the problem was resolved. The importance of proper syntax and variable declaration in C++ programming is emphasized.
SOHAWONG
Messages
16
Reaction score
0
after typing these following 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;

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
3
Views
1K
Replies
2
Views
3K
Replies
8
Views
1K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
15
Views
2K
Back
Top