0

I've been fiddling with this for a couple hours and I can't figure out why this keeps prompting twice, i've searched this website and followed up on a few tips like clearing the buffer etc the loop is still prompting twice when it comes back from the function

Please enter the type of person exiting the garage

S or s for Staff. P or p for Patient. G or g for a Guest

Please enter the type of person exiting the garage

S or s for Staff. P or p for Patient. G or g for a Guest

Any advice would he helpful. Here is my code.

#include <stdio.h>
#define SENTINEL -99
double clientC;
double revenue;
char work;
double hours;

int main(void)
{

double hours;
printf("Type E or e when you, the parking operator are ready to clock out of         
the day.\n");
int loop = 0;

while (loop != SENTINEL)
{


    printf("Please enter the type of person exiting the garage\nS or s for 
  Staff.\nP or p for Patient.\nG or g for a Guest:");
    scanf_s("%c", &work);



    if (work == 'e' || work == 'E')
    {
        printf("The total revenue earned for the day:%.2lf\n", revenue);
        loop == SENTINEL;
    }
    if (work == 's' || work == 'S')
        staff();

}


}


int staff(void)
{
    double clientC = 2.0;
    printf("Please enter the number of hours they were in the parking     
    garage:\n");
    scanf_s("%lf", &hours);
    printf("The charge for staff members is $2.00 a day.\n Your charge is 
    $%.2lf\n", clientC);
    revenue +=clientC;
    return(0);
}
  • 2
    `loop == SENTINEL;` Oh look, a reason to pay attention to compiler warnings ;) "I can't figure out why this keeps prompting twice" What is it that's prompting twice? – George Nov 12 '17 at 01:00
  • yeah but if you type E it exits, otherwise its suppose to tally revenue... Im not actually getting any compiler warnings for once either :( – James Rozell Nov 12 '17 at 01:05
  • I'd double check they're on, `loop == SENTINEL;` will 100% produce a warning. You've also still not said what's prompting twice, might be easier for us to help you if we had a [mcve], – George Nov 12 '17 at 01:10
  • I did tell you whats prompting twice, its prompting for input twice... ive tried flushing... here ill edit and show you my whole code. – James Rozell Nov 12 '17 at 01:12
  • Its prompting the "parking garage attendant" twice for the type of person exiting the garage after it leaves staff.. I haven't finished implementing the patient/guest functions yet because the sentinel loop will loop twice instead of waiting for the prompt, i tried putting a space and a few other things to prevent 2 characters from being stored but to no avail, any advice? and no im still not getting any warnings... – James Rozell Nov 12 '17 at 01:19
  • One: you don't check the return value from `scanf_s()`. Two: you have `scanf_s("%c", &work);` which reads the first character you type on the first lap, then gets a newline `'\n` on the second lap, and then reads a new character on the third lap, etc. Use `if (scanf_s(" %c", &work) != 1) { …bail out… }` instead. – Jonathan Leffler Nov 12 '17 at 01:36
  • Ah right ok, well... `scanf_s("%c", &work);` -> `scanf_s(" %c", &work);`. Also, i've just compiled your code and g++ is having a complete fit, make sure you've got your warning flags on `-Wall`, `-pedantic` or whatever your compiler specific flags are :-). And I know this code is not finished, but you should always be vigilant of name hiding as with `hours` and `clientC`, and be mindful that automatic variables are not auto initialised, I can't see any just now but ub bugs can be horrible to track down. – George Nov 12 '17 at 01:36
  • 1
    @George: Remember that the `*_s()` functions are essentially specific to Microsoft's C runtime. You can find some more information about them at [Do you use the TR 27431 'safe' functions?](https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions) I'm not sure whether your G++ compiler would be accepting C code (GCC — `gcc` — would be a better choice). And the `*_s()` functions could be adding to the mess. – Jonathan Leffler Nov 12 '17 at 01:40
  • Also note that your use of `scanf_s(" %c", &work)` (with or without the blank) is erroneous; you have to supply a length argument too — you'd need `if (scanf_s(" %c", &work, 1) != 1) { …deal with error… }`. – Jonathan Leffler Nov 12 '17 at 01:43
  • @JonathanLeffler Yep, I agree, just laziness on my part using g++ but i'm sure any warnings would be similar if not the same to gcc. Thanks for the pointer by the way, I didn't really know anything about the TR functions, and your answer is very informative! :). – George Nov 12 '17 at 01:53
  • Thanks guys, very insightful comments I appreciate your help. – James Rozell Nov 12 '17 at 01:57

0 Answers0