Reputation: 7
#include <stdio.h>
#define MINIMUM 1
#define MAXIMUM 20
int main ()
{
//Declaring variables.
char name[50];
int number, even_sum, odd_sum, even_count, odd_count;
//Sets all variables to zero.
even_sum=0;
odd_sum=0;
even_count=0;
odd_count=0;
//Asks the user to enter their name and their choice of numbers.
printf("Please enter your first name.\n");
scanf("%s", name);
printf("Please enter integer numbers ranging from 1 to 20; when you're finished entering numbers, enter 0.\n");
scanf("%d", &number);
//Begins loop to decide if the numbers entered are even or odd and adding them to respective totals.
while (number>=MINIMUM && number<=MAXIMUM)
{
printf("Please enter integer numbers ranging from 1 to 20; when you're finished entering numbers, enter 0.\n");
scanf("%d", &number);
if (number%2==0)
{
even_sum += number;
even_count++;
}
else
{
odd_sum+=number;
odd_count++;
}
}
//Separating each line makes it easier to read. The following lines print out the final count and totals of all numbers entered.
printf("\n%s, the numbers you entered are broken down as follows:\n\n", name);
printf("You entered %d even numbers with a total value of %d.\n", even_count, even_sum);
printf("You also entered %d odd numbers with a total value of %d.\n", odd_count, odd_sum);
return 0;
}
The last time I ran it I used integers 2, 4, 3, and exited with 0. The output said I had 2 even entries with a total value of 4. Also that I had 1 odd entry with a total of 3.
The odd output is correct, but the even total is wrong and I can't figure out what I'm doing wrong.
Upvotes: 0
Views: 128
Reputation: 12698
The problem is that you are discarding the first data after you enter the loop, because you are reading it again (so discarding it)
You'd better to move the printf(); scanf(); sequence in the loop to the end of it, so you actually use the data input when it is ok.
There are two problems in your code: You correctly check the input data to be between the two limits to enter processing, but once you get into the loop, you discard those data for a new one. This makes the data not to be checked again (so it can be the token data you use to exit the loop, that makes it to be processed as data, which it must not). And you get 0
processed as data, and the first data completely ignored.
To correct your problem, the solution can be to move the printf(); scanf();
sequence inside the loop to the final part of it, in that case you'll get data processed in the first data case, and not enter the loop again when you finally enter 0
.
Upvotes: 0
Reputation: 9473
so decided to remodel logic loop with an extra variable
#include <stdio.h>
#define MINIMUM 1
#define MAXIMUM 20
int main ()
{
//Declaring variables.
char name[50];
int number, even_sum, odd_sum, even_count, odd_count;
bool canWeGo;
//Sets all variables to zero.
even_sum=0;
odd_sum=0;
even_count=0;
odd_count=0;
canWeGo=true;
while (canWeGo)
{
printf("Please enter integer numbers ranging from 1 to 20; when you're finished entering numbers, enter 0.\n");
scanf("%d", &number);
canWeGo = number>=INIMUM && number<=MAXIMUM;
if (canWeGo)
{
if (number%2==0)
{
even_sum += number;
even_count++;
}
else
{
odd_sum+=number;
odd_count++;
}
}
}
/Separating each line makes it easier to read. The following lines print out the final count and totals of all numbers entered.
printf("\n%s, the numbers you entered are broken down as follows:\n\n", name);
printf("You entered %d even numbers with a total value of %d.\n", even_count, even_sum);
printf("You also entered %d odd numbers with a total value of %d.\n", odd_count, odd_sum);
return 0;
}
Upvotes: 0
Reputation: 169
You get 3 in the even numbers because your input wasn't 2 4 3, it was 2 4 3 0.
When you enterd 0 the program was waiting for input on the scanf and then continued to check the if's with input 0.
Because 0%2==0 you added 1 to the even counter, and only then you checked the while condition and found out the input was 0 and stoped the while loop.
After new code: The first number you enterd outside the while loop is lost when your program is asking you for the next number, you never added it to any of the counters, so naturally your program isn't considering it.
You enterd the number, and immediately afterwards you are asking for a new number. A professor of mine once told me - If a computer as made an calculation, but hadn't store it - Has it made any calculation?
One of the ways to solve it would be to move the scanf in the while loop to the end of the loop.
This way the first number would get calculated to the result, and the input that resulted in an exit won't.
printf("Please enter integer numbers ranging from 1 to 20; when you're finished entering numbers, enter 0.\n");
scanf("%d", &number);
//Begins loop to decide if the numbers entered are even or odd and adding them to respective totals.
while (number>=MINIMUM && number<=MAXIMUM)
{
if (number%2==0)
{
printf("even\n");
even_sum += number;
even_count++;
}
else
{
printf("odd\n");
odd_sum+=number;
odd_count++;
}
printf("Please enter integer numbers ranging from 1 to 20; when you're finished entering numbers, enter 0.\n");
scanf("%d", &number);
}
Upvotes: 0
Reputation: 9473
it hard to say what's wrong as snippet provided is not giving a whole picture. are you decreasing
number
variable?
Please make some tests and provide result let's say for 1,3,5,7,11,13.
--
as @Deddy spoted, you need to add a exit statement from loop, so let's when user provide 0 then skip it
if (number>0){
if (number%2==0)
{
....
}
don't forget to close bracket :-)
Upvotes: 1