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);
}
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 need to find a way to shorten my if-else statements with switch. The if-else statements are really long and they look really unprofessional and I hope there is a way to shorten them into a couple of lines instead of the mess of multiple lines as I have it now.
I tried to implement a switch block but it didn't go correctly and the way I wanted it to go.
int numbers(int tal[]) {
int choice,a;
printf("\nWrite a specific number: ");
scanf("%d", &choice);
int b = 0;
for(a = 0 ;a < MAX ;a++){
if(tal[a]== choice){
b = 1;
printf("\nExists in the sequence on this location: ");
if(a <= 9)
printf(" Row 1 och column %d\n",a +1);
else if (a > 9 &&a <= 19)
printf(" Row 2 och column %d\n", (a +1) - 10);
else if (a > 19 &&a <= 29)
printf(" Row 3 och column %d\n", (a +1) - 20);
else if (a > 29 &&a <= 39)
printf(" Row 4 och column %d\n", (a +1) - 30);
else if (a > 39 &&a <= 49)
printf(" Row 5 och column %d\n", (a +1) - 40);
else if (a > 49 &&a <= 59)
printf(" Row 6 och column %d\n", (a +1) - 50);
else if (a > 59 &&a <= 69)
printf(" Row 7 och column %d\n", (a +1) - 60);
else if (a > 69 &&a <= 79)
printf(" Row 8 och column %d\n", (a +1) - 70);
else if (a > 79 &&a <= 89)
printf(" Row 9 och column %d\n", (a +1) - 80);
else if (a > 89 &&a <= 99)
printf(" Row 10 och column %d\n", (a +1) - 90);
break;
}
}
if (b == 0)
printf("\n%d It does not exists in the sequence", choice);
}
I got it working, and I changed all the if-else statements to this one;
edit: nvm the column answer I get is incorrect.
int choice,a,row,col;
printf("\nWrite a specific number: ");
scanf("%d", &choice);
int b = 0;
for(a = 0 ;a < MAX ;a++){
if(tal[a]== choice){
b = 1;
printf("\nExists in the sequence on this location: ");
if(a <= 9)
col = a % 10 + 1;
row = a / 10 + 1;
printf("Row %d och column %d\n", row, col);
break;
}
}
if (b == 0)
printf("\n%d It does not exists in the sequence", choice);
enter image description here
You can make it look a bit better by:
Properly indenting it
Omitting redundant checks with >
like that:
if(a <= 9)
printf(" Row 1 och column %d\n",a +1);
else if (a <= 19)
printf(" Row 2 och column %d\n", (a +1) - 10);
else if (a <= 29)
printf(" Row 3 och column %d\n", (a +1) - 20);
...
But in this case you can completely avoid the if-block by calculating the values, for example like that:
col = a % 10 + 1;
row = a / 10 + 1;
print("Row %d och column %d\n", row, col);
I have made a program that can output whatever numeral is inputted in to it but it will convert it to words instead of just numerals for example if you input "1234.56" it will convert it to "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents". The cents should always be in numerals. So far everything works great, however if I put in an amount that is less that one thousand I will get the excess words such as "Thousand" or "Hundred" in there. For example if I input "15.77" my output will be "Thousand Hundred Fifteen Dollars and ... 77 Cents".
I don't want the Thousand or Hundred to be there, without those it would be perfect!
The code is as follows:
#include <stdio.h>
void printNum(int);
void printNum2(int);
void printNum3(int);
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int num = 0;
int printcents; //To convert the float "cents" to an integer.
float inclusive;
float cents;
printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.01 || inclusive >= 10000.00) {
printf("Sorry, cannot create cheque for that amount, try again next time!\n");
}
else
{
a = inclusive / 1000; //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
inclusive = inclusive - (a*1000);
b = inclusive / 100;
inclusive = inclusive - (b*100);
if ( inclusive > 19 )
{
c = inclusive / 10;
inclusive = inclusive - (c*10);
}
else
{
c = inclusive;
d = 0;
}
d = inclusive;
num = inclusive;
cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
printcents = cents;
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printf("%d", printcents);
printf(" Cents\n");
}
}
void printNum(int x) //Created functions to easily output various if statements.
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
void printNum2(int x)
{
if ( x == 10)
printf("Ten ");
else if ( x == 11)
printf("Eleven ");
else if ( x == 12)
printf("Twelve ");
else if ( x == 13)
printf("Thirteen ");
else if (x == 14)
printf("Fourteen ");
else if (x == 15)
printf("Fifteen ");
else if (x == 16)
printf("Sixteen ");
else if (x == 17)
printf("Seventeen ");
else if (x == 18)
printf("Eighteen ");
else if (x == 19)
printf("Ninteen ");
else if (x == 2)
printf("Twenty ");
else if (x == 3)
printf("Thirty ");
else if (x == 4)
printf("Forty ");
else if (x == 5)
printf("Fifty ");
else if (x == 6)
printf("Sixty ");
else if (x == 7)
printf("Seventy ");
else if (x == 8)
printf("Eighty ");
else if (x == 9)
printf("Ninety ");
}
void printNum3(int x)
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
I have been coding for exactly one month now so if it seems like I made simple mistakes that's why.
You need to add conditionals around your printfs:
if (a > 0)
{
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
}
The problem is that you are unconditionally printing the "Thousand", "Hundred", etc...
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printf("%d", printcents);
printf(" Cents\n");
if the number you send to printNum is zero, you don't want to print out your text string, you'd have to check for that condition:
/* call to printNum x */
if ( /* check if the parameter to printNum matches any case, seems to be if not zero){
printf(/* whatever string is appropriate */);
}
as an example, say the input was 512.26
512.26/1000 <<< that should be 512.26/ 1000.f so both operands of the divide are floats
will result in a float value of : 0.51226f NOT 0.0f
using the floor(0.51226f) Will result in 0.0f
Suggest always applying the floor() function to yield a 0.0f
Even with a float, a comparison such as if( value != 0.0f ) then print it
would work after applying the floor() function.
I've made a program that is a cheque generator. Whatever amount you input in the scanf will output in words. For example if I were to to input "1234.56" My output will be "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents", or if I wanted to input the amount "0.01" the output will be "Zero Dollars and ... 1 Cents". The program works perfectly, however there is a minor issue, if I wanted to input the amount "9909" it will output "Nine Thousand Nine Hundred Ninety Nine Dollars and ... 0 Cents". The output should be "Nine Thousand Nine Hundred Nine Dollars and ... 0 Cents". Please help!
The code is as follows:
#include <stdio.h>
void printNum(int);
void printNum2(int);
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int num = 0;
int printcents; //To convert the float "cents" to an integer.
float inclusive;
float cents;
printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.00 || inclusive >= 10000.00) {
printf("Sorry, cannot create cheque for that amount, try again next time!\n");
}
else
{
a = inclusive / 1000; //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
inclusive = inclusive - (a*1000);
b = inclusive / 100;
inclusive = inclusive - (b*100);
if ( inclusive > 19 ){
c = inclusive / 10;
inclusive = inclusive - (c*10);
}
else
{
c = inclusive;
d = 0;
}
d = inclusive;
num = inclusive;
cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
printcents = cents;
/*Printing if the variables are in the thousands, hundreds, tens or ones categories.*/
if (c == 0 && b == 0 && a == 0){
printf("Zero Dollars and ... %d Cents\n", printcents);
}
else{
if (a > 0){
printNum(a);
printf("Thousand ");
}
if (b > 0){
printNum(b);
printf("Hundred ");
}
printNum2(c);
if (d >= 0){
printNum(d);
printf("Dollars and ... ");
}
printf("%d", printcents);
printf(" Cents\n");
}
}
}
void printNum(int x) //Created functions to easily output various if statements.
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
void printNum2(int x)
{
if ( x == 10)
printf("Ten ");
else if ( x == 11)
printf("Eleven ");
else if ( x == 12)
printf("Twelve ");
else if ( x == 13)
printf("Thirteen ");
else if (x == 14)
printf("Fourteen ");
else if (x == 15)
printf("Fifteen ");
else if (x == 16)
printf("Sixteen ");
else if (x == 17)
printf("Seventeen ");
else if (x == 18)
printf("Eighteen ");
else if (x == 19)
printf("Nineteen ");
else if (x == 2)
printf("Twenty ");
else if (x == 3)
printf("Thirty ");
else if (x == 4)
printf("Forty ");
else if (x == 5)
printf("Fifty ");
else if (x == 6)
printf("Sixty ");
else if (x == 7)
printf("Seventy ");
else if (x == 8)
printf("Eighty ");
else if (x == 9)
printf("Ninety ");
}
I have been coding for a month now so I apologize for the simple errors.
After you have calculated b and adjusted inclusive so it is now 9, you get to your else case, which sets c to 9 (hence the Ninety output) and d to 0, but immediately afterwards sets d to 9 (hence the Nine in the output). I think you mixed up c and d in the else case.