0

I am writing a program that starts printing at 0.
It prints up to 15 and asks the user a y/n question.

  • if y that program prints next 15.
  • if n program stops.

The program I wrote does not work.

Help solving this.

int main()
{
int i=0,k=1;
char ans;

while(k=1)
{
    i++;
    printf("\n%d",i);

    if(i%15==0)
    {
        printf("\nDo you want to continue?(y/n): ");
        scanf("%c",ans);
        ans = toupper(ans);
        if(ans=='Y') {
            continue;
        }
        else if(ans=='N') {
            k=0;
        }
    }
}
}

----------------------------------EDIT------------------------------------- changed the code as @Programmer400. Also 15-->3. Now my computer prints

1
2
3
Do you want to continue?(y/n): y

4
5
6
Do you want to continue?(y/n): 
7
8
9
Do you want to continue?(y/n): y

First it prints till 3 and asks. After Y, it prints till 6 and asks and then without any input prints till 9 and asks. Note the missing y in the 2nd question.

Uttaran
  • 25
  • 6

2 Answers2

1

I tried changing your code so that it doesn't generate warnings, now it seems to "work" (but maybe it can be even more correct).

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>

int main()
{
    int i=0,k=1;;
    char c;
    char ans[] = "";
    while(k==1)
    {
        i++;
        printf("\n%d",i);

        if(i%15==0)
        {
            printf("\nDo you want to continue?(y/n): ");
            scanf(" %c",ans);
            ans[0] = (char) toupper(ans[0]);
            if(ans[0]=='Y') {
                continue;
            }
            else if(ans[0]=='N') {
                k=0;
            }
        }
    }
}
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
1

I have provided a working C program below that performs the tasks you specified in your question.

I have taken an effort to stay true to the functions that you used in your original code sample and I have also taken care to only make additions (not remove code).

In the comments, I have explained lines of code that I have added that were not in your original code sample.

#include <stdio.h>

int main(void)
{ 
        int i = 0, k = 1;
        char user_input;
        char ans;

        while(k == 1)
        {
                i++;
                printf("%d\n", i);

                if (i % 15 == 0)
                {
                        printf("Do you want to continue? (y/n/Y/N): ");
                        scanf(" %c",&user_input); // Keep the whitespace in front of the %c format specifier -- it's important!
                        getchar(); // Consume the newline character left in the buffer by scanf()

                        // Check if user input is already capitalized
                        if (user_input >= 65 && user_input <= 90)
                                // If it is, keep it capitalized
                                ans = user_input;
                        else
                                // If it isn't, capitalize it
                                ans = toupper(user_input);

                        if (ans=='Y')
                        {
                                // Allow the loop to continue
                                continue;
                        }
                        else if (ans == 'N')
                        {
                                // Inform the user that execution is ending
                                printf("Exiting loop... ending program.\n");
                                // Consider removing 'k' entirely, just use a 'break' statement
                                k = 0;
                        }
                        else
                        {
                            // Inform the user that the input was not recognized (if not y/n/Y/N...)    
                            printf("User input not recognized... please provide input again.\n");
                                // Decrement 'i' so that the user is forced to provide input again...
                                i--;
                                // Allow the loop to continue
                                continue;
                        }
                }
        }
}

Helpful notes:

  1. scanf leaves a newline character in the buffer when you are reading user input with character formatters. Namely...

    %c, %n, and %[] are the 3 specified expectations that do not consume leading whitespace -- From a comment on this StackOverflow answer.

  2. Keep in mind that if you would like to exit your while loop, you could simply insert a break statement. This way, you don't have to change the value of k (which is rather ambiguous) to end the loop and the code is more readable because an explicit break statement is harder to misinterpret. In this simple case, the use of k is easily understood (so don't worry about it too much, for now).

  3. If you ever intend to read string input from a user (i.e., an array of characters), then I would recommend that you use fgets() instead of scanf(). A discussion of the merits of fgets() in comparison to scanf() is provided in this StackOverflow answer. Further, it is important to recognize that even though you can use gets() to perform a similar operation it is highly dangerous and never advised. Check out the explanations provided by the top two answers to this StackOverflow question.

Community
  • 1
  • 1
Vladislav Martin
  • 1,504
  • 2
  • 15
  • 34