sin program using while (extreme newby)

Hi all. first I have looked and searched and found similar but not quite what i was looking for. I am new to c+ literally 3weeks in and I have a problem. I wrote this program and it compiled but the math is wrong and I cant quite get it to do what i what it to do. her is the program:

int k;
main (void)

double param, result, radians, sinx;
char ans= 'y', unit, sum;
param= k;

while (ans == || ans == 'Y')
{
prinf("\n\n What number do you want to use?\n\n")
scanf("%lf",&k);
radians= k*(PI/180);
result= sin(k*PI/180);
printf ("result is: %lf",sin(k*PI/180);
printf ("the sin of %lf is %lf\n,k,result);

}

printf ("\n\n Would you like to enter another number? (y for yes)\n\n");

return 0;
}

I know its probably something simple and I apologize if it is, I just really need help on this one. Also if there is a way to put tolerances in here some where(lol) any tips would be very helpful and appreciated.
I wonder which compiler you did use to compile this program. There are a lot of syntax and semantic errors i guess you didn't just copy and paste it?

1. After main(void) you should open a paranthesis {.
2. What do you need that variable int k; for?
3. while (ans == || ans == 'Y') should be while (ans == 'y' || ans == 'Y') i guess.
4. the number you get from the user with scanf("%lf",&k); is a double floating point value that is stored in an integer variable k. This makes no sense, i guess what you want is something like scanf("%lf",&param);
5.You calculate the radians with radians= k*(PI/180);, but you do not make use of that calculation in the next line, where you could simply write result = sin(radians)
6.The two printf statements afterwards do pretty much the same, the first prints the result and the second one prints the result too. I'd say keep the second and remove the first.
7. The question if the user wants to enter another number has to be in the while loop, followed by a scanf to get a potentially new value for the answer ans.
8. I suggest to remove all occurences of the variable k and replace it with param.

Also did you include <cmath> and did you define PI ?
thanks for the quick reply.
1. I just forgot to type it in. its there.
2. I thought the k would represent the variable/number imputed.
3. and once again i didn't type it in but its there.
4. thanks I was wondering about that.

for the rest of the questions I made the appropriated changes and a proper cut and paste lol thanks for the help so far:

int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, sinx;
char ans= 'y', unit, sum;



/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
while (ans == 'y'|| ans == 'Y')
{

printf ("\n\n What number do you want to use?\n\n");
scanf ("%lf", &param);
result= sin(radians);
printf ("The sin of %lf is %lf\n", param, result);
printf("\n\n Would you like to enter another number? (Y for yes)\n\n");
scanf("%lf", &ans);

}



return 0;
}

/***************************************************************************/


This is the program I have so far. with the changes suggested. The only problem now is the loop runs continually . The loop doesn't stop. also it doesn't give the answer to the question. if you type in 50 it answers with 0.

Thank you so much for the help.
I got the program to answer the question by changing the result=sin(radians) to result=sin(param*PI/180). and wanted to add an if file to it. so if they answered no, it would respond with a thank you for using me to figure sine. else run the same loop. now it runs in a continuous loop, doesn't stop. after you answer yes or no.

Here is the FILE:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159265

/************************** MESSAGE FUNCTION *****************************/

void
msg1(void)
{

printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
printf("\n EE 150 \n" );
printf("\n Here you should put a message that the user");
printf("\n can read to know what the program is about" );
printf("\n and what to do to use it. \n\n\n\n\n\n\n\n" );

}

/* SOMETIMES YOU WILL PUT OTHER FUNCTIONS HERE */

/*************************** MAIN FUNCTION *******************************/


int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, sinx;
char ans= 'y', unit, sum;



/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
while (ans == 'y'|| ans == 'Y')
{

printf ("\n\n What number do you want to use?\n\n");
scanf ("%lf", &param);
result= sin(param*PI/180);
printf ("The sin of %lf is %lf\n", param, result);
printf("\n\n Would you like to enter another number? (Y for yes)\n\n");
scanf("%lf", &ans);

}

if ('n')
{
printf ("\n\n Thank you for using me to figure sine \n\n");
scanf ("%lf", &ans);
}

else
{
printf ("\n\n What degree would you like to do next? \n\n");
scanf ("%lf", &param);
result= sin(param*PI/180);
printf ("\n\n The sin of %lf is %lf\n", param, result);
}
return 0;
}

/***************************************************************************/


You can still write result = sin(radians);, but you need to set radians appropriately, i.e. radians = param * PI / 180.;. (That's what you did at the beginning)

The reason why it's an infinite loop now is because of the line where you read the answer to the question whether the user wants to continue or not. %lf in scanf means that it's reading a double value, but what you want is to get a character. To achieve that you simply need to change it to scanf("%c",&ans);, same with
1
2
3
4
5
if ('n')
{
printf ("\n\n Thank you for using me to figure sine \n\n");
scanf ("%lf", &ans);
}

this part of your code, change the %lf to %c.

Then, however, you'll face another problem. Because scanf waits for the user to press enter, you will have to read that enter charater from input stream too, and that can be achieved using a dummy char variable:

1
2
char dummy;
scanf("%lf%c",&param, &dummy);


This will discard the following enter character by storing it in dummy, and so your program is ready for another scanf.
Last edited on
these are the changes I have made according to the you suggestion.:


int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, sinx;
char ans= 'y', unit, sum, dummy;added dummy to the char



/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
while (ans == 'y'|| ans == 'Y')
{

printf ("\n\n What number do you want to use?\n\n");
scanf ("%lf", &param);
result= sin(param*PI/180);
printf ("The sin of %lf is %lf\n", param, result);
printf("\n\n Would you like to enter another number? (Y for yes)\n\n");
scanf("%lf", &ans);

}

if (ans == 'n'|| ans == 'N')
{
printf ("\n\n Thank you for using me to figure sine \n\n");
scanf ("%c", &param, &dummy); switched from previous version
}

else
{
printf ("\n\n What degree would you like to do next? \n\n");
scanf ("%lf", &param);
result= sin(param*PI/180);
printf ("\n\n The sin of %lf is %lf\n", param, result);
scanf ("%lf%", &ans);
}
return 0;
}

/***************************************************************************/

Actually thinking about do i really need the else file in this situation? since the while is running if the answer is yes. can I just discard the else file all together?
You're right, you can just discard the else-case, it makes no much sense to put it there.

I marked other things in the code you should change:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, sinx;
char ans= 'y', unit, sum, dummy;



/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
while (ans == 'y'|| ans == 'Y')
{

printf ("\n\n What number do you want to use?\n\n");
scanf ("%lf%c", &param, &dummy);  //<---add dummy here
result= sin(param*PI/180);
printf ("The sin of %lf is %lf\n", param, result);
printf("\n\n Would you like to enter another number? (Y for yes)\n\n");
scanf("%c", &ans);  //<--- use %c here instead of %lf because you want to read a character, no double

}

if (ans == 'n'|| ans == 'N')
{
printf ("\n\n Thank you for using me to figure sine \n\n");
scanf ("%c", &param, &dummy); //<-- Why do you need scanf here?
}

else
{
printf ("\n\n What degree would you like to do next? \n\n");
scanf ("%lf", &param);
result= sin(param*PI/180);
printf ("\n\n The sin of %lf is %lf\n", param, result);
scanf ("%lf%", &ans);
}
return 0;
}

/***************************************************************************/
this is what i got:


int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, sinx;
char ans= 'y', unit, sum, dummy;



/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
while (ans == 'y'|| ans == 'Y')
{

printf ("\n\n What number do you want to use?\n\n");
scanf ("%lf", &param, &dummy);
result= sin(param*PI/180);
printf ("The sin of %lf is %lf\n", &param, &result);
printf("\n\n Would you like to enter another number? (Y for yes)\n\n");
scanf("%c", &ans);

}

if (ans == 'n'|| ans == 'N')
{
printf ("\n\n Thank you for using me to figure sine \n\n");
scanf ("%c", &param, &dummy);
}


return 0;
}

/***************************************************************************/
when I made these changes. Know the math does't work. and doesn't do the if file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

int
main(void)
{

/********************* DECLARATION OF VARIABLES **************************/

double param, result, radians, sinx;
char ans= 'y', unit, sum, dummy;



/*******************THE EXECUTABLE PART OF THE PROGRAM ********************/

/******* Print the Opening Message *******/

msg1();

/******* Computations ******/
while (ans == 'y'|| ans == 'Y')
{

printf ("\n\n What number do you want to use?\n\n");
scanf ("%lf%c", &param, &dummy);      //<--- you forgot the %c to read the enter character
result= sin(param*PI/180);
printf ("The sin of %lf is %lf\n", param, result);  //<--- Do not give addresses of param and result to printf (with & operator)
printf("\n\n Would you like to enter another number? (Y for yes)\n\n");
scanf("%c", &ans);

}

//Removed if statement
printf ("\n\n Thank you for using me to figure sine \n\n");
scanf("%c", &ans); //<--- see explanation below (*)


return 0;
}

/***************************************************************************/


(*) i have to repeat myself, if you write %c in the scanf string, it means you want to read a char (character). If you write %lf, it means you want to read a double value from input. So if you write
scanf("%c", &param, &dummy);
it means that you read a character (%c) and store it in the first parameter provided to the function after the string literal "%c", which is param. But param is a variable of type double. Assigning a char to it makes no sense. Furthermore there is another parameter, namely &dummy. You did not specify any more than "%c" to read from standart input, so this parameter is redundant. Nothing is read from input and copied to the dummy variable.
To summarize this, the first parameter is &param, which is the address of a double variable and the second parameter is &dummy, which is the address of a char variable, so the correct string would be "%lf%c", or the whole line scanf("%lf%c",&param,&dummy);.

But at this place in your sourcecode this logically makes no sense, because all you want is the user to press enter to exit the program, so it would be most useful to write something like scanf("%c", &dummy) or scanf("%c", &ans). (Doesn't matter if you use dummy or ans since the program is about to exit anyway and you're not going to use the value anymore)
I got it load and clear. anks for the info. I really appreciate it. I think I learned more in these question and answer sessions than in class this whole time. lol And it works perfectly.
Last edited on
Topic archived. No new replies allowed.