Originally Posted by
samii1017
Code:
float enterYourNumber(){
double number = 0;
printf("Enter the number. \n");
scanf("%lf", &number);
}//End enterYourNumber
That function does not work. It does prompt the user for a number, and it tries to parse a double from the standard input. You do not check whether the scanf() succeeds or fails (it returns the number of successful conversions it did, or EOF if there is no more input or input does not match fixed characters in the pattern). And, you don't do anything with the parsed number, either.
Assume you rewrite your function to a different logic: Read numbers from input, storing them into an array, repeating until there is no more room in the array, no more input, or you encounter a non-numeric input. Your function could be something like
Code:
int read_numbers(double *const number, const int numbers, FILE *const in)
{
int n = 0;
/* Read numbers from in, until no more room in array. */
while (n < numbers) {
/* Try reading the next number. */
if (fscanf(in, " %lf", &number[n]) == 1) {
/* A number was read successfully. */
n++;
} else {
/* Not a number, or no more input. Break out of while loop. */
break;
}
}
/* Return the number of numbers read. */
return n;
}
In your main(), you'd need something along the lines of
Code:
#define NUMBERS_MAX 500
int main(void)
{
double input[NUMBERS_MAX];
int inputs;
/* some kind of printf() to ask the user for numbers, and to
* end the list with anything non-numeric, like end or = or * or !
* or just Ctrl+Z (in Windows) or Ctrl+D (in Linux/BSD/Mac OS X).
*/
inputs = read_numbers(input, NUMBERS_MAX, stdin);
/* you now have 'inputs' numbers in 'input',
* from input[0] to input[inputs-1].
* Use some kind of a loop -- you'll need a new int
* variable in the loop -- to print them out.
*/
return 0;
}