NO

Author Topic: my quit() function won't work  (Read 4725 times)

fyzafuaad

  • Guest
my quit() function won't work
« on: June 02, 2016, 07:18:50 am »
Can anybody know why does why does my void quit() function won't work?
if Y, it supposed to shut down the console, if N, it should go back to int main(void)

Code: [Select]
void quit()
{
char stop;

printf("\nWill you end this banking system? Enter Y/N\n");
scanf("%c", &stop);

if (stop == 'Y')
{
exit(1);
}
else
{
return;
}
}

but it won't even let the users input neither Y nor N and just went to main again

Offline PaoloC13

  • Member
  • *
  • Posts: 36
Re: my quit() function won't work
« Reply #1 on: June 02, 2016, 07:15:06 pm »
Try this:

Code: [Select]
void quit(void)
{
char stop = 0;

for (;;)
{
printf("\nWill you end this banking system? Enter Y/N\n");
scanf("%c", &stop);

if (stop == 'y' || stop == 'Y')
{
exit(1);
}
else if (stop == 'n' || stop == 'N')
{
return;
}
}
}

inside main() call it:

Code: [Select]
quit();
printf("\nPerform your task here...\n");

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 761
Re: my quit() function won't work
« Reply #2 on: June 02, 2016, 07:27:55 pm »
Can anybody know why does why does my void quit() function won't work?
if Y, it supposed to shut down the console, if N, it should go back to int main(void)

Code: [Select]
void quit()
{
char stop;

printf("\nWill you end this banking system? Enter Y/N\n");
scanf("%c", &stop);

if (stop == 'Y')
{
exit(1);
}
else
{
return;
}
}

but it won't even let the users input neither Y nor N and just went to main again
Because you are comparing with an uppercase "Y" while most likely entering a lowercase "y". That are two different characters ("Y" is ASCII 89, while "y" is ASCII 121), hence your comparison (stop == "Y") will simply not be true unless you actually enter an uppercase "Y".

Include ctype.h at the top of your program and use if (toupper (stop) == "Y") and it will magically start to work...  ;)

Ralf

fyzafuaad

  • Guest
Re: my quit() function won't work
« Reply #3 on: June 03, 2016, 07:04:54 am »
Try this:

Code: [Select]
void quit(void)
{
char stop = 0;

for (;;)
{
printf("\nWill you end this banking system? Enter Y/N\n");
scanf("%c", &stop);

if (stop == 'y' || stop == 'Y')
{
exit(1);
}
else if (stop == 'n' || stop == 'N')
{
return;
}
}
}

inside main() call it:

Code: [Select]
quit();
printf("\nPerform your task here...\n");

yeayyy thanks a lot ! but I was wondering why did the "Will you end this banking system? Enter Y/N" line come out 2 times?

fyzafuaad

  • Guest
Re: my quit() function won't work
« Reply #4 on: June 03, 2016, 07:09:52 am »
Quote
Because you are comparing with an uppercase "Y" while most likely entering a lowercase "y". That are two different characters ("Y" is ASCII 89, while "y" is ASCII 121), hence your comparison (stop == "Y") will simply not be true unless you actually enter an uppercase "Y".

Include ctype.h at the top of your program and use if (toupper (stop) == "Y") and it will magically start to work...  ;)

yeahh I tried it, but error occurs T.T . So I just kinda use the code PaoloC13 gave up there

Scripter

  • Guest
Re: my quit() function won't work
« Reply #5 on: June 03, 2016, 10:02:58 pm »
Can anybody know why does why does my void quit() function won't work?
if Y, it supposed to shut down the console, if N, it should go back to int main(void)

Code: [Select]
void quit()
{
char stop;

printf("\nWill you end this banking system? Enter Y/N\n");
scanf("%c", &stop);


but it won't even let the users input neither Y nor N and just went to main again

You probably have the carriage return from your last keyboard action still in the buffer. Put 1 space between the (" and % in your scanf.... scanf(" %c", &stop);
That will clear the buffer before looking for new characters.
« Last Edit: June 04, 2016, 02:55:23 pm by Scripter »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 761
Re: my quit() function won't work
« Reply #6 on: June 04, 2016, 09:25:01 am »
Quote
Because you are comparing with an uppercase "Y" while most likely entering a lowercase "y". That are two different characters ("Y" is ASCII 89, while "y" is ASCII 121), hence your comparison (stop == "Y") will simply not be true unless you actually enter an uppercase "Y".

Include ctype.h at the top of your program and use if (toupper (stop) == "Y") and it will magically start to work...  ;)

yeahh I tried it, but error occurs T.T . So I just kinda use the code PaoloC13 gave up there
What error?

Ralf

fyzafuaad

  • Guest
Re: my quit() function won't work
« Reply #7 on: June 05, 2016, 09:39:33 am »
Quote
Because you are comparing with an uppercase "Y" while most likely entering a lowercase "y". That are two different characters ("Y" is ASCII 89, while "y" is ASCII 121), hence your comparison (stop == "Y") will simply not be true unless you actually enter an uppercase "Y".

Include ctype.h at the top of your program and use if (toupper (stop) == "Y") and it will magically start to work...  ;)

yeahh I tried it, but error occurs T.T . So I just kinda use the code PaoloC13 gave up there
What error?

Ralf

It is still the same . it doesn't let me input neither y or n

Scripter

  • Guest
Re: my quit() function won't work
« Reply #8 on: June 05, 2016, 11:01:06 am »
Quote
It is still the same . it doesn't let me input neither y or n

Edit your lines with scanf() like this....  scanf(" %c", &key) ... the space before the % sign tells scanf() to clear previous clutter out of the input buffers.

Also when doing Y/N type questions... a Y or a y is yes, everything else is a no.
Code: [Select]
void AskExit(void)
  {
     int key = 0;

     printf("Want to quit? ");
     scanf(" %c", &key);

     if(key == 'Y' || key == 'y') exit(0);

    return;
  }
« Last Edit: June 07, 2016, 02:00:22 pm by Scripter »

Offline PaoloC13

  • Member
  • *
  • Posts: 36
Re: my quit() function won't work
« Reply #9 on: June 06, 2016, 01:26:20 pm »
yeayyy thanks a lot ! but I was wondering why did the "Will you end this banking system? Enter Y/N" line come out 2 times?

The answer comes from Scripter: putting a space before the character '%'. So:

Code: [Select]
void quit(void)
{
char stop = 0;

for (;;)
{
printf("\nWill you end this banking system? Enter Y/N\n");
scanf(" %c", &stop);

if (stop == 'y' || stop == 'Y')
{
exit(1);
}
else if (stop == 'n' || stop == 'N')
{
return;
}
else
printf("\nIncorrect imput; retry!\n");
}
}

Scripter

  • Guest
Re: my quit() function won't work
« Reply #10 on: June 07, 2016, 01:12:52 am »
The answer comes from Scripter: putting a space before the character '%'. So:

Code: [Select]
void quit(void)
{
char stop = 0;

for (;;)
{
printf("\nWill you end this banking system? Enter Y/N\n");
scanf(" %c", &stop);

if (stop == 'y' || stop == 'Y')
{
exit(1);
}
else if (stop == 'n' || stop == 'N')
{
return;
}
else
printf("\nIncorrect imput; retry!\n");
}
}

Yes, your way will work.... It may be my "personal" style, I don't know, but I really hate situations where I have to deliver error messages over trivial things, so I tend to do it the way I showed in my last message.  Like I said... Y or y is "yes", everything else is "no". Either way... no worries. 

It's the " %c" thing that makes the real difference... you should always use that space trick with %c and %s since it is likely the trailing line end from the last input operation is still in the keyboard's buffer. When you launch scanf() without the space, it sees the stray line end, decides you just hit Enter, and returns.

Another way is to #include conio.h and use the fflush(stdin); call to dump the keyboard buffer before each input function.

One last note... your programs should always return 0 or exit(0) to indicate "no error". This value is communicated to scripts and batch files.  Many scripting tools will signal an error on non-zero values. If you are signalling an error, use one of the Windows Error Codes as appropriate to the failure...

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx





« Last Edit: June 07, 2016, 02:47:25 pm by Scripter »

Offline PaoloC13

  • Member
  • *
  • Posts: 36
Re: my quit() function won't work
« Reply #11 on: June 08, 2016, 02:28:21 am »
It may be my "personal" style, I don't know, but I really hate situations where I have to deliver error messages over trivial things, so I tend to do it the way I showed in my last message.  Like I said... Y or y is "yes", everything else is "no". Either way... no worries.

I agree, ...since we have not:

Yes -> Quit
No  -> Nuclear_War_Start

After all, my post was worthless, your example is the best way.

Scripter

  • Guest
Re: my quit() function won't work
« Reply #12 on: June 08, 2016, 03:17:33 pm »
It may be my "personal" style, I don't know, but I really hate situations where I have to deliver error messages over trivial things, so I tend to do it the way I showed in my last message.  Like I said... Y or y is "yes", everything else is "no". Either way... no worries.

I agree, ...since we have not:

Yes -> Quit
No  -> Nuclear_War_Start

After all, my post was worthless, your example is the best way.

Your post is not worthless. This is the cool part about programming, exploring and trying different ways of doing things is how we learn and grow with the hobby/profession. There are situations where I might use your approach, especially if there is data loss or potential for system crashes from incorrect responses. There may be situations where you find my method better...  It's all good.