Originally Posted by
whiteflags
This is still no different than writing
Code:
char what = 'y';
int whatnow = 0;
while (tolower(what) != 'n') {
whatnow++;
if (whatnow == 10) {
printf("I chased my tail %d times!\n"
"Do you still want to see me chase my tail y/n? ", whatnow);
scanf(" %c", &what);
whatnow = 0;
}
}
The while loop breaks when the person using the program types the right thing, nothing else matters, not even the switch.
I wouldn't be so critical, but I think if you want to force someone to notice a feature is missing, you need to make such a thing actually necessary.
what a loop or switch?
With that code you wrote I was trying to do something else. that is why it was getting screwed up. I was thinking one thing and my brain was typing something else, due to motor memory whatever they call it, when you get stuck on something (besides stupid) and it needs to be a different response.Kind of like muscle memory reflects. well whatever I was trying to do it was not working, time was an issue.
the switch was to change the it to the right conditions. why not utilize it if you're going to be using it anyways inside of a loop? that was the only thing here on the switch and loop criteria. Because they have already finished theirs. I'll post mine to show what I am talking about.
because I already know about what you're saying, I was just pressed for time last night. so I got crap results. Okay
Code:
#include <stdio.h>
double celsius_to_fahrenheit(double input)
{ return input * 1.8 + 32; }
double fahrenheit_to_celsius(double input)
{ return (input - 32) * .5556; }
//K = C + 273.15
double fahrenheit_to_kelvin (double input)
{ return (fahrenheit_to_celsius(input) + 273.15); }
double celsius_to_kelvin(double input)
{ return input + 273.15; }
int main (void)
{
char choice;
double temp;
while ( choice != 'q' )
{
printf("Enter a selection to convert temperatures.\n"
"1. Celsius to Fahrenheit\n"
"2. Fahrenheit to Celsius.\n"
"3. Celsius to Kelvin\n"
"4. Fahrenheit to Kelvin\n"
"5. Quit\n") ;
scanf(" %c", &choice);
switch (choice)
{
case '1' :
printf("enter tempiture in Celsius\n");
scanf(" %lf", &temp);
printf("\nCelsius %.2lf\nFahrenheit %.2lf\n",temp, celsius_to_fahrenheit(temp));
break;
case '2':
printf("enter tempiture in Fahrenheit\n");
scanf(" %lf", &temp);
printf("\nFahrenheit %.2lf\nCelsius %.2lf\n",temp, fahrenheit_to_celsius(temp));
break;
case '3':
printf("enter tempiture in Celsius\n");
scanf("%lf", &temp);
printf("\nCelsius %.2lf\nKelvin %.2lf\n",temp, celsius_to_kelvin(temp));
break;
case '4':
printf("enter tempiture in Fahrenheit\n");
scanf(" %lf", &temp);
printf("\nFahrenheit %.2lf\nKelvin %.2lf\n",temp, fahrenheit_to_kelvin(temp));
break;
case '5':
printf("\naddio\n");
choice = 'q';
break;
default:
break;
}
printf("\n"); // formating
}
return 0;
}
because choice is already not equal to 'q' it just drops straight into the loop. Now everything can be contained within it, instead of the print message then go into loop then repeat the same message again and again until loop ends scenario.
selection 5 then assigns choice to 'q' therefore meeting the requirements to stop the loop.