Reputation:
So I am writing a basic program that asks the user to input a number and the loop will continue until they enter a certain number. (25). Afterward the program will add up all of the numbers they inputted. The issue is when I type the exit number, the loop doesn't exit, and I'm not sure why.
double userNum = 0;
double sum = 0;
do {
printf("Please enter a number you would like to add [Enter 25 to exit at any time]:\n");
scanf("%f", &userNum);
sum = sum + userNum;
} while (userNum != 25);
printf("The sum of all the numbers you entered:%f\n", sum);
Also Im not sure the sum will calculate properly as Ive never been able to exit the loop.
Upvotes: 0
Views: 71
Reputation: 1
You are wanting to use a sentinel controlled loop (25 being your sentinel). Here is what I would write:
#include <stdio.h>
int main()
{
double userNum = 0;
double sum = 0;
while ( userNum != 25 ) { //sentinel controlled loop
puts("Please enter a number you would like to add [Enter 25 to exit at any time]:"); // puts automatically inputs a newline character
scanf("%lf", &userNum); // read user input as a double and assign it to userNum
sum = sum + userNum; // keep running tally of the sum of the numbers
if ( userNum == 25 ) { // Subtract the "25" that the user entered to exit the program
sum = sum - 25;
} // exit the if
} // exit while loop
printf("The sum of all the numbers you entered:%lf\n", sum);
} // exit main
OR, you can stick with do...while:
// Using the do...while repetition statement
#include <stdio.h>
// function main begins program execution
int main( void )
{
double userNum = 0; // initialize user input
double sum = 0; //initialize sum as a DOUBLE
do {
puts("Please enter a number you would like to add [Enter 25 to exit at any time]:"); // puts automatically inputs a newline character
scanf("%lf", &userNum); // read user input as a double and assign it to userNum
sum = sum + userNum; // keep running tally of the sum of the numbers
if ( userNum == 25 ) { // Subtract the "25" that the user entered to exit the program
sum = sum - 25;
} // end if statement
} while ( userNum != 25 ); // end do...while
printf("The sum of all the numbers you entered:%lf\n", sum);
} // end function main
Upvotes: 0
Reputation: 8286
Consider using fgets for input and parse the value with sscanf. With this you could enter done or exit to terminate the loop instead of 25. The format for scanning a double is %lf
.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( void)
{
char input[99] = "";
double userNum = 0;
double sum = 0;
while ( 1) {
printf("Please enter a number you would like to add [Enter 25 to exit at any time]:\n");
if ( ( fgets ( input, sizeof ( input) , stdin))) {
if ( strcmp ( input, "25\n") == 0) {//could use exit, done ... instead of 25
break;
}
if ( ( sscanf(input, "%lf", &userNum)) == 1) {//sscanf successful
sum = sum + userNum;
}
}
else {
break;//fgets failed
}
}
printf("The sum of all the numbers you entered:%f\n", sum);
return 0;
}
Upvotes: 1
Reputation: 3962
You're using the wrong datatype, go with integers instead:
int userNum = 0;
int sum = 0;
do {
printf("Please enter a number you would like to add [Enter 25 to exit at any time]:\n");
scanf("%d", &userNum);
sum = sum + userNum;
} while (userNum != 25);
printf("The sum of all the numbers you entered:%d\n", sum);
Upvotes: 0