So c++ is my first programming class and we haven't covered much in class when it comes to statements and conditions. We haven't covered switch commands but after reading through the chapters, the switch works best for me. Even after reading, I still don't understand the while loop :( And that's where I'm having problems. I need to make my code so that when you input 'y' for "more" it circles back to choices 1 or 2. I've searched almost everywhere and the only I find is really basic options for numerical operations. If I could get some help looping that would be awesome! I'm using Code::Blocks for Mac if that helps.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float c, f, cel, fah;
char more = 'y';
int choice;
printf ("\n\tRelationship between Fahrenheit and Celsius\n\t\tPlease Select a Number");
printf ("\n\t\t\t1) F -> C \n\t\t\t2) C -> F \n\t\t");
scanf ("%d", &choice);
switch (choice)
{
case 1: //choice 1 f-c
printf ("\n\t\tInput F: ");
scanf ("%f, \n", &f);
cel = 5.0/9.0 * (f - 32.0);
printf ("\n\t\t%7.2fF = %7.2fC\n", f, cel);
printf ("\n\t\tDo more (Y/N) ? ");
scanf ("%s%c", &more);
break; //end case 1
case 2: //choice 2 c-f
printf ("\n\tInput C: ");
scanf ("%f, \n", &c);
fah = 9.0/5.0 * (c + 32.0);
printf ("\n\t%7.2fC = %7.2fF\n", c, fah);
printf ("\n\tDo more (Y/N) ? ");
scanf ("%s%c", &more); //end case 2
break;
default:
printf ("\n\t\tCan't you follow directions??\n\t\tYou broke me. kthxbye");
break;
} //end switch
return 0;
}
(I goofed around the default because I was frustrated with the loop :))
Aucun commentaire:
Enregistrer un commentaire