Thread: Help with simple function

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Help with simple function

    Hi guys, first post so take it easy. I just started programming yesterday in C. And decided to try and write a simple programme to calculate the cos of a user input value. This is my attempt so far but its not working
    Code:
    #include <stdio.h>
    #include <math.h>
    main()
    {
    int d;
    float x;
    	printf("welcome to the cos calculator\n");
    	printf("what angle do you want the cos of:\n");
    	scanf("%d" &d);
    	x=cos(d);
    	printf("x=%0.2f\n",x);
    }
    I know this is probably completely wrong so any help would be much appreciated
    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	scanf("%d", &d);
    Btw, Read the documentation of all functions you used.google is your friend.

    The cos() function returns the cosine of x, where x is given in radians.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    The main function needs a type specifier, namely int:

    Code:
    int main()
    {
        ...
        return 0;
    }
    Need a comma in that scanf statement:

    Code:
    scanf("%d", &d);
    Pretty sure the cos function expects radians, so you'll need to convert from degrees to radians.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    There is some syntax error in the code and logic is quite right. Few things which picked

    Code:
    main() should be int main()
    Main should always return int. Perhaps with the C99 if you ignore it is interpreted as int.

    Code:
    scanf("%d", &d);
    Missed the comma between the format specifier and the argument list. And also note you might have to clear the input buffer, to avoid any buffer issues when reading value furthre on.

    Code:
    return 0;
    Missed the return value. Main must return.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    cos() expects a double. Conversion is implied but your compiler should have given you a warning when you passed it an int. It returns a double, so when you assign to a float there's another conversion implied. In the end it's probably easiest to only work with doubles.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Thanks guys, I got it to work with the following code, I then added a bit more on to see how much i could do. But i have 2 questions. Firstly why do you have to put "int" in front of "main ()" as we did not have to that for other stuff we learned in class.
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
    int r;
    double x;
    double d;
    	printf("welcome to the cos calculator\n");
    	printf("what angle do you want the cos of):\n");
    	scanf("%d", &r);
    	d=r*0.01745;
    	x=cos(d);
    	printf("x=%0.5f\n",x);
    int y;
    printf("are you happy with the answer?(yes=1/no=0)\n");
    scanf("%d",&y);
    	if(y==1) {
    	printf("huzzah!!\n");
    	}
    	else if(y==0){
    	printf("Too bad, it's right!\n");
    	}
    }
    and second of all, I had to use integers for the yes/no part at the end, because I couldn't figure out how to just use the input "y" or "n". How could I've done this instead?
    Many thanks, alex

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    int main() vs. void main()

    To read a single char, you can use getchar()

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Thanks guys, I got it to work with the following code, I then added a bit more on to see how much i could do. But i have 2 questions. Firstly why do you have to put "int" in front of "main ()" as we did not have to that for other stuff we learned in class.
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
    int r;
    double x;
    double d;
    	printf("welcome to the cos calculator\n");
    	printf("what angle do you want the cos of(in radians please):\n");
    	scanf("%d", &r);
    	d=r*0.01745;
    	x=cos(d);
    	printf("x=%0.5f\n",x);
    int y;
    printf("are you happy with the answer?(yes=1/no=0)\n");
    scanf("%d",&y);
    	if(y==1) {
    	printf("huzzah!!\n");
    	}
    	else if(y==0){
    	printf("Too bad, it's right!\n");
    	}
    }
    and second of all, I had to use integers for the yes/no part at the end, because I couldn't figure out how to just use the input "y" or "n". How could I've done this instead?
    Many thanks, alex

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by alexl View Post
    Firstly why do you have to put "int" in front of "main ()" as we did not have to that for other stuff we learned in class.
    Fire your teacher! Then, check out the link Bayint Naung provided, or check here. It's always best to be explicit and follow the standard, since you never know what compiler somebody else may be using.
    and second of all, I had to use integers for the yes/no part at the end, because I couldn't figure out how to just use the input "y" or "n". How could I've done this instead?
    Many thanks, alex
    Code:
    printf("are you happy with the answer?(yes=y/no=n)\n");
    scanf("%c", &c);
    if (c == 'y') {
        printf("huzzah!!\n");
    }
    else if(y == 'n') {
        printf("Too bad, it's right!\n");
    }
    else {
        printf("Can't you follow directions?  I said press 'y' or 'n'!\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM