I have a menu for a project that takes a string input and does whatever option you specify. It works perfectly EXCEPT when it goes through the while-loop (while you didn't say quit the menu reloads). It will spew out all the text in the menu like a bulimic teenager. Then it reloads properly and asks you for an input like nothing happened. While this is not a fatal error (code still functions), it is irksome!
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
double aa=0, ab=0, ac=0, sa=0, sb=0, sc=0;
char a1[13], o1[]="AAS\n", o2[]="ASA\n", o3[]="SSA\n", o4[]="SAS\n", o5[]="SSS\n", o6[]= "quit\n";
void AAS()
{
puts("Please enter Angle C");
scanf("%lf", &ac);
puts("Please enter Angle A");
scanf("%lf", &aa);
puts("Please enter Side C. Length is a maximum of 200");
scanf("%lf", &sc);
if (ac >= 180 || aa >= 180 || aa+ac >= 180 || aa <= 0 || ac <= 0 || sc > 200 || sc <= 0)
{
puts("Error, you have entered an invalid triangle");
AAS(); /*Loops you back to the start of Angle-Angle-Side to enter a valid triangle*/
}
else
{
ab= 180-ac-aa;
sb= sin(ab)*sc/sin(ac);
sa= sin(aa)*sc/sin(ac);
printf ("Angle A is %6.3lf Angle B is %6.3lf Angle C is %6.3lf\n Side A is %7.3lf Side B is %7.3lf Side C is %7.3lf \n \n", aa, ab, ac, sa, sb, sc);
}
return;}
void ASA()
{
puts("Please enter");
return;}
void SSA()
{
puts("Please enter");
return;}
void SAS()
{
puts("Please enter");
return;}
void SSS()
{
puts("Please enter");
return;}
void menu()
{
puts(" C\n / \\ \n / \\ \n b / \\ a \n / \\ \n / \\ \n A-----------B \n c");
puts("Please type the abbreviation of the method you would like to use (i.e. SSA, ASA, AAS)");
fgets(a1, 13,stdin);
if(strcmp(a1, o1)==0) /*I am using the strcmp function to see what the user inputs.*/
{
AAS();
return;
}
else if(strcmp(a1, o2)==0)
{
ASA();
return;
}
else if(strcmp(a1, o3)==0)
{
SSA();
return;
}
else if(strcmp(a1, o4)==0)
{
SAS();
return;
}
else if(strcmp(a1, o5)==0)
{
SSS();
return;
}
else if(strcmp(a1, o6)==0) /*This is the "quit" option*/
{
return;
}
else if(strcmp(a1, o1)!=0 && strcmp(a1, o2)!=0 && strcmp(a1, o3)!=0 && strcmp(a1, o4)!=0 && strcmp(a1, o5)!=0 && strcmp(a1, o6)!=0)
{
puts("Sorry, I didn't understand you. Please use all caps for a method or type 'quit' to exit.\n");
return;}
}
int main (void)
{
while(strcmp (a1,o6) != 0)
{
menu();
}
return 0;
}