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;
}
Related
Trying to make a GPA calculator.
Here is my code:
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
float fgpa, grade, n;
char reply;
n = 1;
grade = 1;
printf("--- GPA Calculator ---");
while(fgpa < 1 || fgpa > 4)
{
printf("\n\nEnter your current GPA: ");
scanf("%f", &fgpa);
if(fgpa < 1 || fgpa > 4)
printf("Invalid Input! Please re-enter value.");
}
printf("\nUsing the following table to convert grade to points\n\nA+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\nC = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
while(grade > 0, n++)
{
printf("\nEnter points for module %.0f, enter \"0\" if there are no more additional modules: ", n);
scanf("%f", &grade);
printf("%f", grade);
fgpa = (fgpa + grade) / n;
}
fgpa = fgpa* n / (n-1);
n--;
printf("\n\nNumber of modules taken: %.0f\nFinal GPA: %.1f", n, fgpa);
return 0;
}
I've tried using if(grade = 0) break; but its still not breaking the loop even when the grade is correctly read 0.
picture of 0 being read correctly but loop still continuing
There are multiple problems in the code:
fgpa is uninitialized so the first test in the loop has undefined behavior.
you should also test the return value of scanf() to detect invalid or missing input.
while (grade > 0, n++) is incorrect too: you should instead always read the next grade and test its value and break from the loop before incrementing n.
Your averaging method seems incorrect too: you do not give the same weight to every module.
It seems more appropriate for your purpose to use for ever loops (for (;;)), unconditionally read input, check for scanf() success and test the input values explicitly before proceeding with the computations.
Here is a modified version:
#include <stdio.h>
// flush the rest of the input line, return EOF at end of file
int flush(void) {
int c;
while ((c = getchar()) != EOF && c != \n')
continue;
return c;
}
int main() {
float fgpa;
float grade;
int n = 1;
char reply;
printf("--- GPA Calculator ---");
for (;;) {
printf("\n\nEnter your current GPA: ");
if (scanf("%f", &fgpa) == 1) {
if (fgpa >= 1 && fgpa <= 4)
break;
}
} else {
if (flush() == EOF) {
fprintf(stderr, "unexpected end of file\n");
return 1;
}
}
printf("Invalid Input! Please re-enter value.\n");
}
printf("\nUsing the following table to convert grade to points\n\n"
"A+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\n"
"C = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
for (;;) {
printf("\nEnter points for module %d, enter \"0\" if there are no more additional modules: ", n);
if (scanf("%f", &grade) == 1) {
if (grade <= 0)
break;
printf("%f\n", grade);
fgpa = fgpa + grade;
n = n + 1;
} else {
if (flush() == EOF)
break;
printf("Invalid Input! Please re-enter value.\n");
}
}
fgpa = fgpa / n;
printf("\n\nNumber of modules taken: %d\nFinal GPA: %.3f\n", n, fgpa);
return 0;
}
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){
This is an assignment that my teacher gave me, been trying to get it work for a few days now and can't find what causing the problem.
The code that I type works on a 2012 version of visual studio and when using a 2017 version, the code closes on itself when I enter -1 into the console and I can't get it to print the grades that I want.
#include <stdio.h>
int main()
{
int grades = 0;
int counter = 0;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int E = 0;
do
{
counter++;
printf("Please enter grade between 0 to 100 or -1 to quit:");
scanf_s("%d", &grades);
if (grades != -1 ){
if (grades >= 90 && grades <= 100)
A++;
else if (grades >= 80 && grades <= 89)
B++;
else if (grades >= 70 && grades <= 79)
C++;
else if (grades >= 60 && grades <= 69)
D++;
else if (grades >= 0 && grades <= 59)
E++;
else counter--;
}
else
counter--;
}
while (grades != -1);
printf("Total Number of Grades = %d\n\n", counter);
printf("Number of grade A: %d\n", A);
printf("Number of grade B: %d\n", B);
printf("Number of grade C: %d\n", C);
printf("Number of grade D: %d\n", D);
printf("Number of grade E: %d\n", E);
return 0;
}
To my understanding, the program does what you expect (printing the grades at the end), but you are unable to see the results beacause the program terminates and the windows closes. You can wait for a keypress at the end using the getchar function, which is explained here.
Too keep the console window open in Visual Studio start the project with Ctrl+F5 instead of just F5.
Good morning, guys! I'm currently a newly started programming learner. Down here is my code for a mini app store. However, there is a problem going on, yet I couldnt locate the problem.
The problem happen when I tried buying an app for $89.99 and I chose to redeem $10 9 times so I would have enough money to purchase the app (I didnt choose the $100 option because it would work just fine). However, the remained amount became $-79.99 instead of $0.01. Like I said, if I chose to deposit $100, the remained balance would be $10.01, which is normal. I don't get where I did wrong. Here is my code!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int Compare(double deposit, double choiceCost);
void DisplayApps(char *selectionPtr);
void SetCost(char selection, double *costPtr);
void PaymentOptions(double *depositPtr,double cost);
void DoItAgain(char *quitPtr);
//void Pay(double *depositPtr, double choiceCost);
void GetChange(double *depositPtr, double choiceCost);
void DoItAgain(char *quitPtr);
int main()
{
char selectionPtr;
char selection;
char quitPtr;
double costPtr;
double deposit = 0.0;
double choiceCost;
double depositPtr = 0.0;
double cost = 0.0;
printf("Welcome to the App Store\n");
printf("***************************\n\n");
printf("Your deposit is: %.2f\n", deposit);
printf("\n");
while (1)
{
do {
DisplayApps(&selectionPtr);
selection = selectionPtr;
SetCost(selection, &costPtr);
choiceCost = costPtr;
Compare(deposit, choiceCost);
while (Compare(deposit, choiceCost) == 0)
{
printf("Your balance isn't enough.\n");
printf("In order to purchase this item, you have to redeem more money.\n");
PaymentOptions(&depositPtr, cost);
deposit += depositPtr;
printf("You have redeemed $%.2f\n", depositPtr);
printf("Your balance now is: $%.2f\n", deposit);
printf("\n");
}
deposit = depositPtr;
GetChange(&depositPtr, choiceCost);
DoItAgain(&quitPtr);
} while (quitPtr == 'Y' || quitPtr == 'y');
return 1;
}
return 0;
}
void DisplayApps(char *selectionPtr)
{
printf("-------------------------\n");
printf("HERE ARE THE SLECTIONS\n");
printf("C -- Clown Punching $299.99\n");
printf("V -- Virtual Snow Globe $349.99\n");
printf("R -- Remote PC $999.99\n");
printf("G -- Grocery List Helper $2.99\n");
printf("M -- Mobile Cam Viewer $89.99\n");
printf("\n");
printf("Please enter a selection: ");
scanf(" %c", &*selectionPtr);
printf("\n");
}
void SetCost(char selection, double *costPtr)
{
if (selection == 'C' || selection == 'c')
{
*costPtr = 299.99;
}
else if (selection == 'V' || selection == 'v')
{
*costPtr = 349.99;
}
else if (selection == 'R' || selection == 'r')
{
*costPtr = 999.99;
}
else if (selection == 'G' || selection == 'g')
{
*costPtr = 2.99;
}
else if (selection == 'M' || selection == 'm')
{
*costPtr = 89.99;
}
}
int Compare(double deposit, double choiceCost)
{
if (deposit < choiceCost)
{
return 0;
}
else
{
return 1;
}
}
void PaymentOptions(double *depositPtr, double cost)
{
printf("You have (4) options to choose:\n");
printf("1 - $1000.00\n");
printf("2 - $500.00\n");
printf("3 - $100.00\n");
printf("4 - $10.00\n");
printf("How much do you want to redeem?");
printf(">>>>> ");
scanf("%lf", &cost);
printf("\n");
printf("-------------------------------------\n");
if (cost == 1)
{
*depositPtr = 1000.00;
}
else if (cost == 2)
{
*depositPtr = 500.00;
}
else if (cost == 3)
{
*depositPtr = 100.00;
}
else if (cost == 4)
{
*depositPtr = 10.00;
}
}
void GetChange(double *depositPtr, double choiceCost)
{
*depositPtr = *depositPtr - choiceCost;
printf("You have purchased this item successfully.\n");
printf("You still have $%.2f remained in you balance.\n", *depositPtr);
}
void DoItAgain(char *quitPtr)
{
printf("Do you want to continue purchase another item? [Y/N]\n");
scanf(" %c", &*quitPtr);
}
In this code : GetChange(&depositPtr, choiceCost);
You shold pass deposit (total deposit) and not &depositPtr (last deposit, only 10)
#include <stdio.h>
#include <time.h>
int main (void)
{
int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;
float frequency = 0;
srand (time(NULL));
while (1)
{
printf ("Pick one of the three doors infront of you, which do you want?\n");
scanf ("%d", &pickedDoor);
if (pickedDoor > 3 || pickedDoor <= 0)
{
break;
}
winningDoor = rand() % 3 + 1;
do
{
hostDoor = rand() % 3 + 1;
} while (hostDoor == pickedDoor || hostDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == pickedDoor || remainingDoor == hostDoor);
printf ("The door the host picked is %d\n", hostDoor);
do
{
printf("Do you want to switch doors? Please enter in the door you want:\n", hostdoor);
scanf("%d", &option);
if (option > 3 || option <= 0)
{return 0;}
}while (option == hostDoor);
if (option == winningDoor)
{
printf("You Won!\n");
wins++;
}
else
{
printf("YOU LOSE!\n");
}
games++;
}
frequency = ((float) wins / games) *100;
printf ("The number of games that you won is %d\n", wins);
printf ("The frequency of winning is %.0f%%\n", frequency);
return 0;
}
Hi, this is my version of the monty hall game show, im getting unexpected results though.
sometimes when I enter in a door for my option it just brings me back to the "pick one of the three doors infront of you" statement, when it should tell me if i have won or lost.
I think this is because the "option" door is equal to the "hostDoor.
I thought having "option != hostDoor" would fix it but it does not.
If i am correct in that assumption how can I fix it? If not why is it not working and how can I fix it?
To simulate correctly, OP needs to show the host door.
do {
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
if (option > 3 || option <= 0 ) {
return 0;
}
} while (option == hostDoor);
// instead of
#if 0
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
if (option > 3 || option <= 0 ) { return 0; }
#endif
To deal with OP " it should tell me if i have won or lost." problem, change
else if (option == remainingDoor)
to
else
Your scanf("%d", &option) is OK. I prefer the fgets()/sscanf() combo and its alway useful to check the result of scanf() family, but that is not your issue here.
Its because of these:
scanf ("%d", &pickedDoor);// reads \n after the last input
scanf("%d", &option); // reads \n after the last input
**option != hostDoor; // completely useless .. get rid of it**
I would suggest putting a getchar() after each scanf to get rid of the \n character
so something like this:
#include <stdio.h>
#include <time.h>
int main (void)
{
int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;
char collect; //variable to collect any extra input like \n
float frequency = 0;
srand (time(NULL));
while (1)
{
printf ("Pick one of the three doors infront of you, which do you want?\n");
scanf ("%d", &pickedDoor);
collect = getchar(); // get rid of the \n from the input stream
printf("collect = %c\n",collect);
if(collect!='\n'){ // is it actually a \n or did you take in something else
putchar(collect); // if it isn't \n put it back
}
if (pickedDoor > 3 || pickedDoor <= 0)
{
break;
}
winningDoor = rand() % 3 + 1;
do
{
hostDoor = rand() % 3 + 1;
} while (hostDoor == pickedDoor || hostDoor == winningDoor);
do
{
remainingDoor = rand() % 3+1;
} while (remainingDoor == pickedDoor || remainingDoor == hostDoor);
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
collect = getchar(); // get rid of the \n from the input stream
printf("collect = %c\n",collect);
if(collect!='\n'){ // is it actually a \n or did you take in something else
putchar(collect); // if it isn't \n put it back
}
if (option > 3 || option <= 0 )
{
return 0;
}
if (option == winningDoor)
{
printf("You Won!\n");
wins++;
}
else if (option == remainingDoor)
{
printf("YOU LOSE!\n");
}
games++;
}
frequency = ((float) wins / games) *100;
printf ("The number of games that you won is %d\n", wins);
printf ("The frequency of winning is %.0f%%\n", frequency);
return 0;
}
Another more efficient way would be to use fgets or to have error checks on scanf() itself
srand and rand belongs to stdlib header file
#include<stdlib.h> // include this header file
option != hostDoor; // this statement does not do anything
and your else if (option == remainingDoor) should be else { printf("you lose");}