You are on page 1of 3
oat2023, 12.27 In Programming, How do | use a loop to ask user if they want to continue? - Stack Overflow In C Programming, How do | use a loop to ask user if they want to continue? Asked 7 years, 8 months ago Modified 7 years, 7 months ago Viewed 6k times | am a ‘st year Computer Science student, currently taking intro to Programming. We are learning the C Programming language and I'm having trouble figuring out how to properly input a loop at the end of my code to ask the user if they want to continue or not. This simple assignment asks to develop a program that will determine an employee's gross pay (including an “if* statement to determine overtime). As you'll see in my code, | think I've done so correctly. The assignment goes on to state that at the end of my program | should use a loop to ask the user if they want to continue. In class, we went over For loops, and While loops, but lam a this feature correctly. le Lost on how to implement | initially tried doing something like. printfC*Would you Like to continue? (1 = Yes, 2 = No) \n" scané(" dj White © yell nye + But not sure what to declare for the input (scanf) or what to putin the while loop. Please help. Its my spring break and no campus tutoring is around. Thanks! Here's my code: Hinclude include int main Q { double totalHours, rate, grossPay, overTime, overTimepay, otHours, grossPaywith0T; //1. 1 began by asking user for total hours & getting input print¢("Enter your total hours worked : \n"); scané("%L¢", StotalHours) ; //Now I'm using a selection statenent to determine pay for overtine hours if (totalHours > 48) { //a. Inforn user they have overtine hours printfC"You worked over Uo hours this period. \n"); //b. Ask how many hours over 48 they worked printé("How many hours over 48 did you work? : \n"); scanf(*%L", SotHours) ; //c. Bsk the user for hourly rate printéCwhat is your hourly rate? : \n"), scanf(*™lf", Grate); //4, Overtime Rate Calculation & Gross Pay Calculation hips:ilstackoverlow.comiquestions/25976786fin-c-pragramming-how-do-use-2-loop-to-ask-user-ishey-want‘o-continue 1 oat2023, 12.27 In Programming, How do | use a loop to ask user if they want to continue? - Stack Overflow grossPay = totalHours * rate; overTime = 1.5 * rate; overTimepay = otHours * overTime; grossPaywithOT = overTimepay + grossPay; J/e. Display overtime pay and Gross Pay printf(*Your overtime pay is %.02Lf \n", overTimepay); printf("Your total Gross Pay including overtime is %.62U¢ \n", grossPaywith0T) ; F else { 7/2, Rsk the user for hourly rate printéC"what is your hourly rate? : \n™ //3. User input for hourly rate scanf("%l¢", Sate); 1/4. Gross Pay Calculation grossPay = totalHours * rate; 1/5. Display grossPay printf(*Your Gross Pay is %.621f \n", grossPay); < while-loop. Share Improve this question edited Mar 28, 2016 at 8:33 asked Mar 13, 2016 at 22:39 Follow ketan Brandon Hilton 19.2k ©42 ©60 ©99 1e103 printf("Would you like to continue? (1 = Yes, 2 = No) \n"); scanf("%i", __); While ( 1_ == 'Y)(// logic printfWould you like to continue? (1 = Yes, 2 = No) \n"); scanf(‘%i", __);}. You need to ask if the user wants to continue again whilst in the loop or you will be stuck in a infinite loop, Hope that helps - level zebra 3 Answers Sorted by:| Highest score (default) whileCLoop ==""y') { //00 your stuff here print#("do you want to Loop? (y/n) scan" %c", Sloop); i#Cloop I= 'y!) Loop: } JA Share Improve this answer Follow answered Mar 13, 2016 at 22:54 hips:ilstackoverlow.comiquestions/25976786fin-c-pragramming-how-do-use-a-loop-to-ask-user-ishey-wantto-continue 20 oat2023, 12.27 In Programming, How do | use a loop to ask user if they want to continue? - Stack Overflow Anders Cedronius 2,046 © 1 © 23 029 ‘Thanks! This helped a bunch. ~ Brandon Hilton Mar 13, 2016 at 23:34 J/ Loop until they decide to stoj 0 P y P // put the code to do your normal stuff here int i= do { // Loop until they input a1 or 2 print#("\nWlould you Like to continue? (1 = Yes, 2 = No) \n" ); scanf("ai", 84); Fwhile (Ci != 1) && G t= 292; } while Gi Dj Share Improve this answer Follow answered Mar 13, 2016 at 22:47 David Schwartz 180k © 17 © 216 ©281 scanf( doesn't write into variables, but into memory addresses. So, in your _ should write a pointer to a memory address. A couple of examples: _ code you int * intPointer; scanf("%i", intPointer); Or: int integer; scanf("*i", &integer); Note that you typed "*i" in the formatting string. i tells seanf() that the string typed by the user should be parsed as an integer. You want to read it as a character. C formatting functions (that's what the final f in scan#() means use %c for characters. Share Improve this answer Follow answered Mar 13, 2016 at 22:52 JAL 10.7k ©4 ©37 ©50 hips:ilstackoverlow.comiquestions/25976786fin-c-pragramming-how-do-use-a-loop-to-ask-user-ishey-wantto-continue 30

You might also like