This question already has answers here:
Chaining multiple greater than/less than operators
(6 answers)
Closed last year.
How can I break the do while loop here in my code? The program should loop until the user inputs a valid year from the category.
When I enter the valid input, the printf still shows up even if it is meant to stop.
Here is the code below:
#include <stdio.h>
int main()
{
float year;
do {
printf("Please input your year of birth :");
scanf("%f", &year);
if ((year >= 1946) && (year <= 1964))
printf("You belong to the baby boomer generation \n");
else if ((year >= 1965) && (year <= 1980))
printf("You belong to the Generation X \n");
else if ((year >= 1981) && (year <= 1996))
printf("You belong to the Millenials/Generation Y \n");
else if ((year >= 1997) && (year <= 2012))
printf("You belong to the Zoomers/Generation Z \n");
} while(1946 > year < 2012);
}
(1946 > year < 2012)
should be
(year > 1946 && year < 2012)
and break; should be used if you want to break it somewhere like
if(year == 2000)
break;
Your while condition does not work as you expect. For the computer, 1946 > year < 2012 is (1946 > year) < 2021; 1946 > year will be either 0 or 1, so in the end you have 0 < 2021 (or 1 < 2021), which is always true, and the loop won't stop.
Try to change it to e.g. 1946 > year && year < 2012.
See https://stackoverflow.com/a/6961728/5471218 for details.
Add an else condition to break the loop.
if ((year >= 1946) && (year <= 1964))
printf("You belong to the baby boomer generation \n");
else if ((year >= 1965) && (year <= 1980))
printf("You belong to the Generation X \n");
else if ((year >= 1981) && (year <= 1996))
printf("You belong to the Millenials/Generation Y \n");
else if ((year >= 1997) && (year <= 2012))
printf("You belong to the Zoomers/Generation Z \n");
else
break;
Related
I just started learning C and I'm trying to create a simple "Guess the Number" game.
Player 1 will enter a number that is between 1 and 1000.
Player 2 will be given 10 chances to guess the number entered by Player 1.
If Player 2's guess is beyond the range (1 to 1000), the system should display "Invalid. Out of range." instead of "Too high" or "Too low".
Currently, my program does not validate whether Player 2's guess is within the range (1 to 1000). Instead, it will just display "Too high" even if Player 2's guess is 2000 which is beyond the range (1 to 1000)
Here is my code:
#include <stdio.h>
int main()
{
int number, guess, count = 10;
printf("Enter a number between 1 and 1000:\n");
scanf("%d",&number);
while(number < 1 || number > 1000)
{
printf("Number is out of range.\n");
printf("Enter a number between 1 and 1000:\n");
scanf("%d",&number);
}
while(count >= 1 && count <= 10)
{
printf("Player 2, you have %d guesses remaining.\n", count);
printf("Enter your guess:\n");
scanf("%d", &guess);
count = count - 1;
if (guess >= 1 || guess <= 1000)
{
if (guess > number)
{
printf("Too high.\n");
}
else if (guess < number)
{
printf("Too low.\n");
}
else if (guess == number)
{
printf("Player 2 wins.\n");
}
}
else
{
printf("Invalid. Out of range.");
}
}
if (count == 0)
{
printf("Player 1 wins.");
}
return 0;
}
You need to change
if (guess >= 1 || guess <= 1000)
to
if (guess >= 1 && guess <= 1000)
to avoid values outside 1 and 1000. Otherwise, an input of 2000, will match the condition guess >= 1 and as per the short circuit property, it'll evaluate the if condition as truthy and control will never go to else block.
guess >= 1 || guess <= 1000 should be guess >= 1 && guess <= 1000
I am composing a code that gives the user the total of their package after weight and mileage is calculated. It was working before but after I made minor changes anything that weighs over 2lbs is not printing out anything. Why is this?
#include <stdio.h>
#include <stdlib.h>
int main(){
int weight;
int miles;
double mileCost;
int segment;
int remainder;
printf("Charge by weight:(We don't tale packages over 10lbs\n");
printf("\n 1-2 lbs: $1.50\n 3-6 lbs: $3.70\n 7-10 lbs: $5.25\n ");
printf("Enter your package's weight:\n");
scanf("%d", &weight);
printf("Charge by mile: \n");
printf("$1.50 for every 500 miles\n");
printf("Enter the total miles for your package:\n");
scanf("%d", &miles);
if(miles == 0 && weight == 0 && weight < 10){
printf("Invalid entry! Try Again.");
}
segment= miles / 500;
remainder = miles % 500;
if(remainder > 0)
remainder = 1;
if(weight <= 2){
mileCost = 1.50 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 2 && weight >= 6){
mileCost = 3.70 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 6 && weight >= 10){
mileCost = 5.25 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
return 0;
}
You have your upper bounds written with greater than or equal to >= instead of less than or equal to <=:
if(weight > 2 && weight >= 6){
if(weight > 6 && weight >= 10){
This should have been
if(weight > 2 && weight <= 6){
if(weight > 6 && weight <= 10){
regarding:
if(weight > 2 && weight >= 6){
1) this should be preceeded by else so it is not even looked at if weight is <= 2
2) the statement should be:
else if(weight > 2 && weight <= 6){
notice the <= 6
3) similar considerations exist for:
if(weight > 6 && weight >= 10){
I really don't know much about c program so really need your help..
This is the program to find the day of input value(ex. 1583.3.31 -> Thursday) and print "Wrong year/month/day" if it is unvalid date.
I wrote return 0; in the middle part because I have to get out from this when the date is unvalid. I checked it works. But eventhough when the date is valid (ex. 1583.3.31) it doesn't go to the next part(should print day if number is valid) It just ends the program. I wonder why:(
#include <stdio.h>
int main(void) {
int year, month, day;
printf("Enter Gregorian year (year >= 1583): ");
scanf("%d", &year);
printf("Enter Gregorian month (month: 1..12): ");
scanf("%d", &month);
printf("Enter Gregorian day (1..28|29|30|31): ");
scanf("%d", &day);
{if (month < 1 || month > 12)
printf("Wrong month! Try again!");
if (year < 1583)
printf("Enter year >= 1583! Try again!");
if (day < 1 || day > 31)
printf("Wrong day! Try again!");
if (month == 4 && (day < 1 || day > 30))
printf("Wrong day! Try again!");
if (month == 6 && (day < 1 || day > 30))
printf("Wrong day! Try again!");
if (month == 9 && (day < 1 || day > 30))
printf("Wrong day! Try again!");
if (month == 11 && (day < 1 || day > 30))
printf("Wrong day! Try again!");
if (month == 2){
if (year % 4 == 0 && (day < 1 || day > 29))
printf("Wrong day! Try again!");
if (year % 100 == 0 && (day < 1 || day > 28))
printf("Wrong day! Try again!");
if (year % 400 == 0 && (day < 1 || day > 29))
printf("Wrong day! Try again!");}
else if (month == 2 && (day < 1 || day > 28))
printf("Wrong day! Try again!");
return 0;}
year += 8000;
if (month < 3) { year--; month += 12; }
long julian = (year*365) + (year/4) - (year/100) + (year/400) - 1200820 + (month*153+3)/5 - 92 + (day-1);
switch(julian % 7){
case 0:
printf("%d-%d-%d is Monday", year -= 8000, month, day);
break;
case 1:
printf("%d-%d-%d is Tuesday", year -= 8000, month, day);
break;
case 2:
printf("%d-%d-%d is Wednesday", year -= 8000, month, day);
break;
case 3:
printf("%d-%d-%d is Thursday", year -= 8000, month, day);
break;
case 4:
printf("%d-%d-%d is Friday", year -= 8000, month, day);
break;
case 5:
printf("%d-%d-%d is Saturday", year -= 8000, month, day);
break;
case 6:
printf("%d-%d-%d is Sunday", year -= 8000, month, day);
break;}
return 0;
}
You have an unconditional return 0; after the if statements. All but the first if should be an else if and the code for the correct date should be in an else block. Alternatively, return at the end of every error case, proceeding only if each check passes—although this will lead to a lot of duplication.
Enclosing all those statements in a single set of braces merely creates a lexical scope for variable declarations, probably not what you meant.
The code to check validity could instead be a do ... while loop that retries while you enter invalid dates, and you should calculate the day of the week after you fall through that loop, which will happen when the date is valid.
Hi im trying to understand why if 9 or above is entered for judge it passes but it shouldnt cause the if says >= 4 and <= 8
Thanks
while(!(judge >= 4) && (judge <= 8))
{
printf("How many judges are there ? Enter a number between 4 - 8 \n");
scanf("%d", &judge);
while(!(judge >= 4) && (judge <= 8))
{
printf("You entered %d Enter a number between 4 - 8 \n", judge);
scanf("%d", &judge);
if((judge >= 4) && (judge <= 8))
{
break;
}
}
}
It looks like you are missing a pair of parentheses in
while(!((judge >= 4) && (judge <= 8)))
^ ^
(This mistake appears in two places.)
By the way, you can avoid a lot of the repetition by restructuring your code like so:
printf("How many judges are there ? Enter a number between 4 - 8 \n");
for (;;) {
scanf("%d", &judge);
if (judge >= 4 && judge <= 8) {
break;
}
printf("You entered %d Enter a number between 4 - 8 \n", judge);
}
I have a friend who was working on a c example from a book and he did a code like
#include<stdio.h>
#include<math.h>
#pragma warning(disable:4996)
int main()
{
float numGrade;
printf("\n\nPlease enter your numerical grade: ");
scanf("%f", &numGrade);
if (numGrade >= 90)
printf("\nYou got an A.\n\n");
else if (90 > numGrade >= 80)
printf("\nYou got a B.\n\n");
else if (80 > numGrade >= 70)
printf("\nYou got a C.\n\n");
else if (70 > numGrade >= 60)
printf("\nYou got a D.\n\n");
else if (60 > numGrade)
printf("\nYou got an F.\n\n");
else
printf("\nThis is an invalid grade!\n");
}
Is there any problem with doing it like that or should he do it like :
int main()
{
float numGrade;
printf("\n\nPlease enter your numerical grade: ");
scanf("%f", &numGrade);
if (numGrade >= 90)
printf("\nYou got an A.\n\n");
else if (90 > numGrade && numGrade >= 80)
printf("\nYou got a B.\n\n");
else if (80 > numGrade && numGrade >= 70)
printf("\nYou got a C.\n\n");
else if (70 > numGrade && numGrade >= 60)
printf("\nYou got a D.\n\n");
else if (60 > numGrade)
printf("\nYou got an F.\n\n");
else
printf("\nThis is an invalid grade!\n");
}
That first example won't work at all.
The first comparison in each test will return either 0 or 1. So it will always fail the second.
EDIT:
However, the program will probably still "work" the way it is desired, simply because the second comparison in each test is not needed.