| | |
Function not calling a variable
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
When I compile the program it returns "error C2065: 'answer' : undeclared identifier". I thought answer was declared from the function and would call answer. Any tips where I'm going wrong on this. The program is made to out put Radius, cosx(x), and a user defined cosx in 3 rows.
C++ Syntax (Toggle Plain Text)
double cosx (int x, int answer); int main () { for (double x = 0.0; x <= 3.1; x = x + .1) { cout << "Radians" << setw(6) << "Cousines by cos(x)" << setw(15) << "Cousines by user defined" << endl; cout << setprecision(2) << fixed << x << setw(6) << cos(x) << setw(15) << cosx (x, answer) << endl; } } double cosx (int x, int answer) { int n, num, denom, ans, term, count; n = 2; while (ans >= .000001) { for (int i = 0; i <= n; n++) { num = num*n; } for (int k = num; k >= 1; k--) { denom = denom*k; } term = num/denom; if ((term%11)== 0) ans = ans + term; else ans = ans - term; count++; n+= 2; } return ans; }
A function can't "call a variable". It may be called (from another functions) and may call another functions.
The parameter name "answer" in cosx header does not bear a relation to a word "answer" in the main function body. You must declare int variable (called answer or what else) in main and assign a value to this variable before using it in cosx call.
No need in the 2nd parameter of cosx at all. Look at cosx body: you don't use the 2nd parameter.
There are lots of another defects in your code. You declare the 1st parameters of cosx as int but obviously treat it as double. You compare ans (why int again? ) unitialized variable with 0.00001 double value in while loop conditional expression. You don't initialize num and denom variables too.
Correct the code and try again...
The parameter name "answer" in cosx header does not bear a relation to a word "answer" in the main function body. You must declare int variable (called answer or what else) in main and assign a value to this variable before using it in cosx call.
No need in the 2nd parameter of cosx at all. Look at cosx body: you don't use the 2nd parameter.
There are lots of another defects in your code. You declare the 1st parameters of cosx as int but obviously treat it as double. You compare ans (why int again? ) unitialized variable with 0.00001 double value in while loop conditional expression. You don't initialize num and denom variables too.
Correct the code and try again...
I am on my second attempt on this program but it doesn't output right.
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cin; using std::cout; using std::endl; using std::fixed; #include <iomanip> using std::setw; using std::setprecision; #include <cmath> double cosx (double x); int main () { double x = 0.0, result1, result2; cout << " Radians" << "Cousines by cos(x)" << setw(10) << "Cousines by user defined" << endl; while(x < 3.2) { result1 = cos(x); result2 = cosx(x); cout << setw(10) << fixed << setprecision(2) << x << setw(10) << setw(10) << result1 << setw(10) << result2 << endl; x = x + .1; } return 0; } double cosx (double x) { double num, denom, ans, term; int n, count; n = 2; ans = 1; term = 1; count = 1; while (fabs(term) >= .000001) { num =1; for (int i = 1; i <= n; n++) { num = num*n; } denom = 1; for (int k = n; k >= 1; k--) { denom = denom*k; } term = num/denom; if ((count%2) == 0) { ans = ans + term; } else { ans = ans - term; } count++; n+= 2; } return ans; }
![]() |
Similar Threads
- Function calling (C)
- Calling three functions (C++)
- Change function in bottom (JavaScript / DHTML / AJAX)
- Tic-Tac-Toe (C++)
- Object-Oriented Programming (C++)
- Can Any One Help With This??? (C++)
- Something People Should Look At BEFORE Posting... (Geeks' Lounge)
- How do I create a program using an Array ? (C++)
- Data Abstraction (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: Yet Another Vtable Problem
- Next Thread: C++ program help
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






