Thread: do/while loop should end after entering the value 'N' but it does not?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    4

    do/while loop should end after entering the value 'N' but it does not?

    Code:
    #include <stdio.h>
    main()
    {
     float num1, num2, result;
     char choice;
     do {
    printf("Enter your first number to multiply:");
    scanf_s(" %f", &num1);
    printf("Enter your second number to multiply:");
    scanf_s(" %f", &num2);
    result = num1 * num2;
    printf("%.2f times %.2f equals %.2f\n\n", num1, num2, result);
    printf("Do you want to enter another pair of numbers");
    printf("to multiply(Y/N): "); scanf_s(" %c", &choice); } while (choice != 'N' && choice != 'n');
    return 0; }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Once again, I can't replicate the problem. Of course, if scanf_s is broken somehow, or has different behavior than you expect, well, I can't help you. As I said in your other thread, it's non-standard.

    main returns an int. That means you should type
    Code:
    int main()
    Notice the int at the beginning.

    Also, as I said before, you need to provide us with the input you are giving, that is causing the problem. Copy-pasting a run from your console might be good.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    4
    Sorry new to the site not sure how everything works also new to programming!

    the reason for the scanf_s is due to the fact I use Visual Studio and it doesn't seem to like the command and seems to fail until I change "scanf()" to "scanf_s()"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're going to use scanf_s, you'll have to read the docs:
    Quote Originally Posted by MSDN
    Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Arichardson View Post
    Sorry new to the site not sure how everything works also new to programming!

    the reason for the scanf_s is due to the fact I use Visual Studio and it doesn't seem to like the command and seems to fail until I change "scanf()" to "scanf_s()"
    This is one reason I'm not a big fan of newbies learning on VS. Microsoft has made it very clear they have no intention of following the C standard. There are ways to disable the warnings/errors you get from using scanf, but you would have to check the compiler documentation. Honestly, though, I would recommend Code::Blocks, with the default MinGW compiler, or even Pelles C (both free), as they better support the C standard.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Or set the VS projects up to use _CRT_SECURE_NO_DEPRECATE
    c++ - Disabling Warnings generated via _CRT_SECURE_NO_DEPRECATE - Stack Overflow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    From scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l

    The correct way to input a char is

    Code:
    scanf_s("%c", &c, 1);
    You really need to turn on Compiler Warnings or get a Compiler that gives warnings. Because if MSVC does NOT give warnings for this it means its a PoC.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Actually, the _s family of functions introduced my Microsoft have been added as optional to the C11 standard, but with different prototypes. I think that only CLang (among the popular compilers) supports them correctly according to the latest standard. As far as I know, both gcc and pelles c do not support them at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-08-2012, 07:18 AM
  2. entering a string using for loop, "¶"
    By jackson6612 in forum C++ Programming
    Replies: 9
    Last Post: 06-21-2011, 04:28 PM
  3. Endless loop when entering ch
    By sid13 in forum C++ Programming
    Replies: 12
    Last Post: 02-21-2011, 02:56 PM
  4. Segmentation fault when entering a for loop
    By rehpot in forum C Programming
    Replies: 6
    Last Post: 02-17-2011, 09:59 PM
  5. Infinite Loop when entering invalid input
    By acwheat in forum C Programming
    Replies: 5
    Last Post: 04-18-2006, 04:17 PM