Thursday, 15 March 2012

c - Why does my program loop without receiving any new input? -



c - Why does my program loop without receiving any new input? -

i trying create programme start on 1 time reply given. won't function 1 time again 1 time run once. want create functional user doesn't have start programme again. thanks!

#include <stdio.h> #include <math.h> int main() { float firstnum, secondnum, answer; char function; printf("\nhello , welcome calculator!\n"); //intro start: //area loop when programme completes printf("\nplease input function use. these include +, -, *, /.\n"); //asking function input scanf("%c", &function); //receiving function input printf("\nnow please input 2 variables.\n"); //asking variables scanf("%f", &firstnum); scanf("%f", &secondnum); //receiving input variables if (function == '+') //doing calculation { reply = firstnum+secondnum; } else if (function == '-') { reply = firstnum-secondnum; } else if (function == '*') { reply = firstnum*secondnum; } else if (function == '/') { reply = firstnum/secondnum; } else { printf("sorry wrong function. right inputs +, -, *, /."); //if don't follow directions } printf("your reply %f \n", answer); //answer goto start; //loop homecoming 0;

}

this why utilize loops. (and seek not utilize goto this).

#include <stdio.h> #include <math.h> int main() { float firstnum, secondnum, answer; char function, buffer[2]; while(1) { printf("\nhello , welcome calculator!\n"); printf("\nplease input function use. these include +, -, *, /.\n"); scanf("%s", &buffer); function = buffer[0]; printf("\nnow please input 2 variables.\n"); scanf("%f", &firstnum); scanf("%f", &secondnum); if (function == '+') reply = firstnum+secondnum; else if (function == '-') reply = firstnum-secondnum; else if (function == '*') reply = firstnum*secondnum; else if (function == '/') reply = firstnum/secondnum; else printf("sorry wrong function. right inputs +, -, *, /."); printf("your reply %f \n", answer); } homecoming 0; }

this should go in infinite loop, utilize input user break; loop exit program

note : have replaced scanf %c %s indicating input of string & used buffer.

scanf("%s",&buffer); function = buffer[0];

(updated per give-and-take in comments)

c scanf

No comments:

Post a Comment