Question about C syntax - c

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.

Related

Unable to validate input in C programming

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

Can someone tell me why the math is not printing?

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){

How to make my output show only whats needed with my cheque generator program for c

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.

Why does my program give me the excess output? C program

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.

Can't seem to pass information between function and main

I can't get my code to run properly. Here it is:
#include <stdio.h>
void intro_msg( );
float compass_value( );
float direction( );
int main ( void )
{
float compass;
intro_msg( ) ;
compass_value(compass );
direction(compass );
return ( 0 ) ;
}
void intro_msg(void)
{
printf("Welcome to the Compass Program \n \n");
}
float compass_value(compass )
{
printf("Please enter a value for the compass direction. (0 - 360 degress ) : ");
scanf("%f", &compass);
printf("You entered %f degrees \n" , compass);
return(compass);
}
float direction(compass)
{
if (compass >= 354.38 && compass <= 360.00){
printf("You are heading North \n");
}
else if (compass >= 0.0 && compass <= 39.37){
printf("You are heading North \n");
}
else if (compass >= 39.38 && compass <= 84.37){
printf("You are heading Northeast \n");
}
else if (compass >= 84.38 && compass <= 129.37){
printf("You are heading East \n");
}
else if (compass >= 129.38 && compass <= 174.37){
printf("You are heading Southeast \n");
}
else if (compass >= 174.38 && compass <= 219.37){
printf("You are heading South \n");
}
else if (compass >= 219.38 && compass <= 264.37){
printf("You are heading Southwest \n");
}
else if (compass >= 264.38 && compass <= 309.37){
printf("You are heading West \n");
}
else if (compass >= 309.38 && compass <= 354.37){
printf("You are heading Northwest \n");
}
else
{
printf("You did not enter a value between 0 - 360 degrees");
}
}
I'm trying to obtain a degree between 0 - 360 from the user, record it and then determine the direction they are facing (North, South, Northeast, etc.) but my value for compass is never being recording.
Immediately after asking the user for the value, I go to print the value and I get 0.0, not what the user entered. What am I doing wrong here?
I think you know that you are actually passing compass variable into compass_value() by value. Means that the change wont be reflected into the compass variable of the main function.
I see that you are returning the read value, but you dont recieve it into anything at the call to compass_value() in your main function. Try recieving it :
compass = compass_value(compass);
This should fix your problems
#include <stdio.h>
void intro_msg( );
float compass_value(float); // You had not specified Argument type
float direction(float); // You had not specified Argument type
int main ( void )
{
float compass;
intro_msg( ) ;
compass_value(compass);
direction(compass);
return ( 0 ) ;
}
void intro_msg(void)
{
printf("Welcome to the Compass Program \n \n");
}
float compass_value(float compass) // You did not specify type
{
printf("Please enter a value for the compass direction. (0 - 360 degress ) : ");
scanf("%f", &compass);
printf("You entered %f degrees \n" , compass);
return(compass);
}
float direction(float &compass) // You did not specify type, also made it a reference
{
if (compass >= 354.38 && compass <= 360.00){
printf("You are heading North \n");
}
else if (compass >= 0.0 && compass <= 39.37){
printf("You are heading North \n");
}
else if (compass >= 39.38 && compass <= 84.37){
printf("You are heading Northeast \n");
}
else if (compass >= 84.38 && compass <= 129.37){
printf("You are heading East \n");
}
else if (compass >= 129.38 && compass <= 174.37){
printf("You are heading Southeast \n");
}
else if (compass >= 174.38 && compass <= 219.37){
printf("You are heading South \n");
}
else if (compass >= 219.38 && compass <= 264.37){
printf("You are heading Southwest \n");
}
else if (compass >= 264.38 && compass <= 309.37){
printf("You are heading West \n");
}
else if (compass >= 309.38 && compass <= 354.37){
printf("You are heading Northwest \n");
}
else
{
printf("You did not enter a value between 0 - 360 degrees");
}
}
Now, the problems with your code were that you did not specify the argument type in the function declaration and definitions. You were also passing compass by value, due to which the value you were accepting from the user in compass_value was not being put into your compass in the main. So, It always showed north. To fix this, pass compass by reference as indicated.
If you don't want to pass by reference, this code will help you
#include <stdio.h>
void intro_msg( );
float compass_value(float ); // You had not specified Argument type
float direction(float ); // You had not specified Argument type
int main ( void )
{
float compass;
intro_msg( ) ;
compass=compass_value(compass ); // Since the function was returning a compass, assign that value to the compass in main()
direction(compass );
return ( 0 ) ;
}
void intro_msg(void)
{
printf("Welcome to the Compass Program \n \n");
}
float compass_value(float compass ) // You did not specify type
{
printf("Please enter a value for the compass direction. (0 - 360 degress ) : ");
scanf("%f", &compass);
printf("You entered %f degrees \n" , compass);
return(compass);
}
float direction(float compass) // You did not specify type
{
if (compass >= 354.38 && compass <= 360.00){
printf("You are heading North \n");
}
else if (compass >= 0.0 && compass <= 39.37){
printf("You are heading North \n");
}
else if (compass >= 39.38 && compass <= 84.37){
printf("You are heading Northeast \n");
}
else if (compass >= 84.38 && compass <= 129.37){
printf("You are heading East \n");
}
else if (compass >= 129.38 && compass <= 174.37){
printf("You are heading Southeast \n");
}
else if (compass >= 174.38 && compass <= 219.37){
printf("You are heading South \n");
}
else if (compass >= 219.38 && compass <= 264.37){
printf("You are heading Southwest \n");
}
else if (compass >= 264.38 && compass <= 309.37){
printf("You are heading West \n");
}
else if (compass >= 309.38 && compass <= 354.37){
printf("You are heading Northwest \n");
}
else
{
printf("You did not enter a value between 0 - 360 degrees");
}
}
In the declaration
float direction(compass)
what type do you think compass is? (Hint: it is not a float or double.)
Here is how I would write this program (tested):
#include <stdio.h>
#define ARRAY_SIZE(A) (sizeof A / sizeof A[0])
void intro_msg (void)
{
printf ("Welcome to the Compass Program\n\n");
}
float compass_value (void)
{
double direction;
int bad = 1;
do {
printf ("Please enter a value for the compass direction (0 to 360 degrees): ");
fflush (stdout);
if (1 == scanf ("%lf", &direction))
bad = 0;
} while (bad);
printf ("You entered %f degrees\n", direction);
return direction;
}
static const struct {
float lo, hi;
const char *name;
} dirs [] = {
{354.38, 360.0, "North"},
{ 0.0, 39.37, "North"},
{ 39.37, 84.37, "Northeast"},
{ 84.37, 129.37, "East"},
{129.37, 174.37, "Southeast"},
{174.37, 219.37, "South"},
{219.37, 264.37, "Southwest"},
{264.37, 309.37, "West"},
{309.37, 354.37, "Northwest"},
};
void direction (double compass)
{
int j;
for (j = 0; j < ARRAY_SIZE (dirs); ++j)
if (compass >= dirs [j] .lo && compass <= dirs [j] .hi)
{
printf ("Direction is %s\n", dirs [j] .name);
return;
}
printf ("Direction is not between 0 and 360 degrees.\n");
}
int main (void)
{
double compass;
intro_msg ();
compass = compass_value ();
direction (compass);
return 0;
}

Resources