C programming.. basic - program runs through without input

I know mostly C++. I haven't had any experience using C programming. If anyone knows a bit can they please help me? It's pretty basic what I'm attemping. Is there a function like cin.ignore() that I can use here? The program runs all the way through without asking for my input and it's not finished so ignore the function and stuff..


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
//Write a function that accepts two arguments, integer n and real number x, 
//computes the following series, and returns the computed result:

// 1 - 2(x^2)/1! + 3(x^4)/2! - 4(x^6)/3! + ...+ [(-1)^n](n+1)(x^2n)/n!
#include <stdio.h>

float formula( int n, float x );

int main()
{
	int counter;		// Declare counter variable
	int n;			// Declare integer n
	float x;			// Declare real number x
	char choice = 'y';	// Declare choice variable

	while( choice= 'y'|| 'Y' )
	{

		printf( " Please enter an integer:  \n " );			//Ask for integer
		scanf( "%d", &n );
		printf(" Now, please enter a real number: \n" );
//Ask for real number
		scanf( "&f", &x );

		printf( " Do you want to run this program again?\n"
			"Please enter 'y' for yes or 'n' for no." );
//Ask to run again
		scanf( "&c", &choice );
	}

	return 0;
}
hi,

first of all you need to change the while-loop-condition to this:

while ((choice == 'y') || (choice == 'Y'))
because you don't want to assign the value 'y' to choice but check if it already has the value 'y'.

In addition
(choice == 'y' || 'Y')
has the same meaning as
( (choice == 'y') || 'Y')
but since 'Y' is not a boolean, this is not allowed or will at least give an result you don't want, so you have to write
((choice == 'y') || (choice == 'Y'))

Another thing i've noticed is that you did not use scanf the right way. The first time you call it is correct, but when you prompt for a floating point number and a character, you've used the '&'-operator instead of the '%', which you should use between the quotation marks.
you should google "c programming tutorial"
Last edited on
Also your last two scanf should be
scanf( "%f", &x );
instead of
scanf( "&f", &x );
and
scanf( "%c", &choice );
instead of
scanf( "&c", &choice );
Topic archived. No new replies allowed.