While loops are killing my program

Hi I'm doing some simple programs for class. In one of them I installed some error prevention using while loops basically just to keep someone from entering a negative number. Anyway the compiler works, compiles the program without any error messages. When I enter a negative number my program prompts me to enter a new value but when I do a windows error pops up and says my program has stopped responding then shuts the console down, anyone have any ideas?
Post your code.
No doubt an infinite loop. You should check your while conditions.
I used comments to negate the while loops so I could debug the rest of the program which all seems to work

and if it were an infinite loop it would continue to print the statement over and over which it did not




//Program name: Gas Cost
//Author: Robert Wadsworth II
//Course: COP3223 section 3
//Date: 1/21/09
//Purpose: To allow the user to input specific data about there vehicle and then having the program calculate how much their gas
// will cost them that month.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float MPG, GPT, CPG, MPM, COST_OF_GAS;
printf("Enter the miles per gallon your car achieves:");
scanf("%f", &MPG); // stores an integer that is able to float and is stored as the variable MPG or miles per gallon
printf("Enter the amount of fuel your car can contain in gallons:");
scanf("%f", &GPT); /* stores an integer as GPT or gallons per tank*/
printf("Enter the price of gas per gallon:");
scanf("%f", &CPG); // stores an integer as CPG or cost per gallon
printf("Enter the number of miles you drive in a month:");
scanf("%f", &MPM); // stores and integer as MPM or miles per month
/* while (MPG<0)
{
printf("The value you have entered for the miles per gallon your car achieves,\n is less than zero please enter another value:");
scanf("%f", MPG);
}
while (GPT<0)
{
printf("The value you have entered as the amount of fuel your car can contain is less than zero please enter another value:");
scanf("%f", GPT);
}
while (CPG<0)
{
printf("The value you have entered as the price of gas is less than zero please enter a new value:");
scanf("%f", CPG);
}
while (MPM<0)
{
printf("The value you have entered as the number of miles you drive in the month is less than zero please enter a new value:");
scanf("%f", MPM);
}*/
COST_OF_GAS =(GPT-(MPM/MPG))*CPG;
printf("The cost of gas for this month is %.2f\n", COST_OF_GAS);
system("PAUSE");
return 0;
}
Last edited on
You have left & off of your variables in your scanf()'s within your while loops.

~psault
Last edited on
nice catch thanks
Topic archived. No new replies allowed.