I'm new here and this is my only coding class since I'm an engineering major. I've been developing a scientific calculator in C. I've been using functions for each operation and I'm using a while loop in order to either loop around and perform more calculations, or break the loop and end the code when the user inputs Q. The problem is, my code is looping regardless and is not giving me the option to input my characters. I have included all the necessary libraries so I won't be posting them in this code excerpt.
void remove_crlf(char *s){ char *t = s + strlen(s); // t begins at the null sentinel at the end of s.
t--;
/* t is now at the last character of s - unless s didn't contain any characters,in
which case, t is now *BEFORE* s. We have to keep checking for that. */
/* We repeat until EITHER t slides to the left of s, OR we find a character that is
not a line feed (\n) or a carriage return (\r). */
while ((t >= s) && (*t == '\n' || *t == '\r'))
{
*t = '\0'; // Clobber the character t is pointing at.
t--; // Decrement t.
}
}
double addition(double x, double y)
{
return (x + y);
}
int main(void)
{
int n;
double x, y;
double answer;
char s[64];
FILE *ofp;
ofp = fopen("project.txt", "a");
while (1)
{
printf("Enter the correct number in order to pursue your desired operation.\n");
printf(" 1.Addition\n");
scanf("%d", &n);
if (n == 1)
{
printf("Enter your first value: ");
scanf("%lf", &x);
fprintf(ofp, " %lf", x);
fprintf(ofp, " +");
printf("Enter your second value: ");
scanf("%lf", &y);
fprintf(ofp, " %lf", y);
answer = addition(x, y);
printf("%lf\n", answer);
fprintf(ofp, " = %lf\n", answer);
}
printf("Would you like to perform more calculations?\n Press Q to quit.\n Press any other character to perform more calculations.\n");
fgets(s, 63, stdin);
remove_crlf(s);
if (strcasecmp(s, "Q") == 0)
{
break;
}
else{
}
So this is just an example of how my code is formatted. I have many functions but all work the same way that the addition function works and I have about 16 different operations. Remove_crlf is a function I used in a previous code, and was provided by my professor although I'm not really sure what it does. I am also printing to a txt file but that shouldn't matter much. Any feedback is helpful and sorry if I did not ask my question correctly. Thank you!