Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2006

Tip: Looking for answers? Try searching our database.

Combining Functions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nutkin - 13 Nov 2006 03:15 GMT
Hi i have to program a code to perform 1 of 5 functions at the users
request, I have got to the part where i have to program the equations
and i cant seem to get them to link together. Basicly when the used
enters his radians i was to link to a sin x function to calculate the
sin of the number also there will be a factorial function and a cos
function. but none of them are linking.

#include <iostream>

using namespace std;

int factorial()
{
    int i=0;
    for (i=0; i>0; i--)
       

    return 0;
}

double sin()
{
    double sin=0;
    double x=0;
   
    sin=x-(x*x*x/(3*2*1))+(x*x*x*x*x/(5*4*3*2*1));

return sin;
}

int main()
{
    int item=0;
    double x=0;
    double pi=3.14159265;
   
    cout <<"Please enter your x value in radians : ";
    cin >> x;

    cout <<"Select the Equation to Perform:";
    cout <<"\n";
    cout <<"1 = sin(x)\n";
    cout <<"2 = cos(x)\n";
    cout <<"3 = tan(x)\n";
    cout <<"4 = sin^2(x) + cos^2(x)\n";
    cout <<"5 = 2sin(x)cos(x)\n";
    cout <<"Enter the Number: ";
    cin >> item;

    switch (item){
        case 1:
            cout <<sin();
            cout <<"\n\n\n";
            break;
        case 2:
            cout << "Case 2\n\n";
            break;
           
        case 3:
            cout << "case 3\n\n";
            break;
        case 4:
            cout << "case 4\n\n";
            break;
        case 5:
            cout << "case 5\n\n";
            break;
        default:
            cout <<"No Number Entered";
            break;
    }

    return 0;
}
Bruno van Dooren [MVP VC++] - 13 Nov 2006 07:51 GMT
> Hi i have to program a code to perform 1 of 5 functions at the users
> request, I have got to the part where i have to program the equations
> and i cant seem to get them to link together. Basicly when the used
> enters his radians i was to link to a sin x function to calculate the
> sin of the number also there will be a factorial function and a cos
> function. but none of them are linking.

Hi,
It would be helpful if you include the actual error messages.
We are not yet clairvoyant.

If you do math you should also include math.h.
Look in MSDN to see which functions are contained in which library.
There should be no need to program sin and cos functions yourself.

Signature

Kind regards,
   Bruno van Dooren
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Ben Voigt - 13 Nov 2006 22:18 GMT
> Hi i have to program a code to perform 1 of 5 functions at the users
> request, I have got to the part where i have to program the equations
> and i cant seem to get them to link together. Basicly when the used
> enters his radians i was to link to a sin x function to calculate the
> sin of the number also there will be a factorial function and a cos
> function. but none of them are linking.

You should have asked your professor first, he/she is paid to help you.

> #include <iostream>
>
[quoted text clipped - 12 lines]
> double sin=0;
> double x=0;

Local variable x, always zero

> sin=x-(x*x*x/(3*2*1))+(x*x*x*x*x/(5*4*3*2*1));

Reusing the name "sin", bad idea.

x = 0, hence sin = 0

> return sin;

always 0

> }
>
> int main()
> {
> int item=0;
> double x=0;

Another local variable x, distinct from the one in sin().

> double pi=3.14159265;
>
> cout <<"Please enter your x value in radians : ";
> cin >> x;

This x changes.

> cout <<"Select the Equation to Perform:";
> cout <<"\n";
[quoted text clipped - 9 lines]
> case 1:
> cout <<sin();

This always prints 0.

> cout <<"\n\n\n";
> break;
[quoted text clipped - 24 lines]
> Posted via http://www.codecomments.com
> ------------------------------------------------------------------------
David Wilkinson - 13 Nov 2006 23:07 GMT
> Hi i have to program a code to perform 1 of 5 functions at the users
> request, I have got to the part where i have to program the equations
> and i cant seem to get them to link together. Basicly when the used
> enters his radians i was to link to a sin x function to calculate the
> sin of the number also there will be a factorial function and a cos
> function. but none of them are linking.

[snip]

Well, for starters you should be passing the value of x to the functions
as an an argument. The x in your main() is a local variable, so there is
no way the functions can see it (and global variables in C++ are bad
anyway).

What do you mean by "not linking" ?

David Wilkinson

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.