Okay so the prompt was:
"Write a simple program (to demonstrate use of an array) that asks a user to type in numbers and keep typing in numbers until the user enters -1 or the total number of numbers entered reaches 20. Your program will stop asking for new input based on either condition above. Once one of the above conditions are met, the program will output all numbers entered as follows: 1. 888 2. 999 3. 4 …..and so on. Lastly, the program needs to display the sum of all values entered, excluding the -1 used to terminate the user input loop. The array should be used to store the user's inputs, and display them back before the program terminates."
Now, I have most of the prompt completed. It stops at 20 entered variables and displays them and the sum correctly. However, no matter how hard I try, I can't get it to register -1 as an exit. Nor can I get the bulleted list to start from 1. and end at 20. Everything else works just fine.
#include <stdio.h>
#define NUMBER_INPUTS 20
int main(void){
double userInput[NUMBER_INPUTS] = { 0 };
int i = 0;
while (i < NUMBER_INPUTS){
printf("Enter number: \n", i);
scanf("%lf", &userInput[i]);
i++;
}
for(i=0; i<NUMBER_INPUTS; i++)
printf("%i. %.1lf \n", i, userInput[i]);
double total=0;
for(i=0; i<NUMBER_INPUTS; i++)
total+=userInput[i];
printf("Total: %.1lf \n", total);
return 0;
}
This is the code that works just fine. I've tried different do-whiles in different places to register the -1 but it messes up the output list AND doesn't stop the run.
#include <stdio.h>
#define NUMBER_INPUTS 20
int main(void){
double userInput[NUMBER_INPUTS] = { 0 };
int i = 0;
do{
while (i < NUMBER_INPUTS){
printf("Enter number: \n", i);
scanf("%lf", &userInput[i]);
i++;
}
}while (userInput[i] != -1);
for(i=0; i<NUMBER_INPUTS; i++)
printf("%i. %.1lf \n", i, userInput[i]);
double total=0;
for(i=0; i<NUMBER_INPUTS; i++)
total+=userInput[i];
printf("Total: %.1lf \n", total);
return 0;
}
Like this? Just stops at the 20th variable entered. Doesn't matter if I've entered a -1 it just keeps going.
And for the list I attempted to use
i=i+1
to get the list from 1. to 20. to try and bump the i variable up one but for some reason that just shorted the output list to only show 20. and the total? I don't know what I'm doing wrong and would appreciate some input. Thank you.