Thread: how to run my code iteratively until the user presses y

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    1

    how to run my code iteratively until the user presses y

    Hi.

    Its my first post in this forum, along with this, I am also brand new to C language.

    I studied little bit if condition and now i reached to loops. So I started loops first.

    I have tried a code here:
    Code:
    #include "stdafx.h"
    #include <conio.h>
    
    int main()
    {
    
    char a = 'y', store;
    
    printf("Do you have friends, press y for yes and n for no = \n");
    scanf("%c", &store);
    
    
        while (a == store)
        {
            
            printf(" Yes I do have friends \n");
            break;
        }
    
        printf("Loop ended");
    
        getch();
        
    }
    What i expect from my code is that as long as the user is pressing 'y' the code will be executing over and over again. As the user presses the 'n' the program will stop executing.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well your code needs to also read input inside the loop.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    22
    Quote Originally Posted by adsuit View Post
    Hi.

    Its my first post in this forum, along with this, I am also brand new to C language.

    I studied little bit if condition and now i reached to loops. So I started loops first.

    I have tried a code here:
    Code:
    #include "stdafx.h"
    #include <conio.h>
    
    int main()
    {
    
    char a = 'y', store;
    
    printf("Do you have friends, press y for yes and n for no = \n");
    scanf("%c", &store);
    
    
        while (a == store)
        {
            
            printf(" Yes I do have friends \n");
            break;
        }
    
        printf("Loop ended");
    
        getch();
        
    }
    What i expect from my code is that as long as the user is pressing 'y' the code will be executing over and over again. As the user presses the 'n' the program will stop executing.
    you can also do this
    Code:
        while (store!='n') {
            scanf("%c", &store);       
        }

  4. #4
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    I usually do something like this:

    Code:
    int main(){
        char choice;
        while(1){
            fputs("#Please select the exercise (1-5, 0 to exit): ",stdout);
            choice = getchar();
    
            switch (choice) {
                case '0':
                    puts("Good bye");
                    return 0;
    
                case '1':
                    Ex1_main();
                    break;
    
                case '2':
                    Ex2_main();
                    break;
            }
        }
    
        return 0;
    I'm not sure that this is the best option but it works for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to detect if user presses enter?
    By failCprogrammer in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2011, 08:39 PM
  2. Check if user presses the Enter key
    By 337higgin in forum C Programming
    Replies: 8
    Last Post: 09-19-2011, 08:53 PM
  3. Getting user key presses
    By Mavix in forum C# Programming
    Replies: 4
    Last Post: 08-09-2007, 01:49 PM
  4. mkae it so when the user presses...
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 09-27-2004, 10:30 PM
  5. execute until user presses a key?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 11:31 PM

Tags for this Thread