473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

loop

hi im new to c what i want is that when the user enters an invalid charartar it tells it runs again and tells to try again my code is
Expand|Select|Wrap|Line Numbers
  1.     a.x = "who won the FIFA world cup 1986";
  2.  
  3.     Expand|Select|Wrap|Line Numbers
  •  
  •     
  •  
  •     printf("%s choose between\nA:Brazil\nB:Italy\nC:Germany\nD:Argentina\n", a.x);
  •     alt = getchar();
  •  
  •  
  •     while (alt = getchar()){
  •  
  •         switch (alt){
  •  
  •         case 'A':
  •             printf("right\n");
  •             break;
  •  
  •         case 'B':
  •             printf("tyvaar\n");
  •             break;
  •         case 'C':
  •             printf("tyvaar\n");
  •             break;
  •         case 'D':
  •             printf("tyvaar\n");
  •             break;
  •         default:
  •  
  •             printf("tyvaar!!! choose right din turk!\n");
  •             break;
  •  
  •         }
  •  
  •         fflush(stdin);
  •  
  •     }
  •     printf("tyvaar!!! choose right din turk!\n");
  •     system("pause");
  • }
  •  
  •     
  •  
  •  
  • Nov 18 '15 #1
    1 1181
    weaknessforcats
    9,208 Expert Mod 8TB
    Try this:

    Expand|Select|Wrap|Line Numbers
    1. int main()
    2. {
    3.     while (1)
    4.     {
    5.  
    6.     }  //end of while loop
    7. } //end of main()
    8.  
    This loop will run forever. Now put your loop inside this one:

    Expand|Select|Wrap|Line Numbers
    1. int main()
    2. {
    3.     while (1)
    4.     {
    5.         while (alt = getchar()){
    6.  
    7.             switch (alt){
    8.  
    9.             case 'A':
    10.                 printf("right\n");
    11.                 break;
    12.  
    13.             case 'B':
    14.                 printf("tyvaar\n");
    15.                 break;
    16.             case 'C':
    17.                 printf("tyvaar\n");
    18.                 break;
    19.             case 'D':
    20.                 printf("tyvaar\n");
    21.                 break;
    22.             default:
    23.  
    24.                 printf("tyvaar!!! choose right din turk!\n");
    25.                 break;
    26.  
    27.             }
    28.  
    29.             fflush(stdin);
    30.  
    31.         }
    32.         printf("tyvaar!!! choose right din turk!\n");
    33.         system("pause");
    34.  
    35.  
    36.     }  //end of while (1) loop
    37. } //end of main()
    38.  
    Of course the while (1) loop never stops so you will need to add a case to stop the program. A call to exit(1) will do that.

    You may also need to re-display your menu, etc inside the
    while(1) loop before the inner loop starts.

    Let me know what you think.
    Nov 18 '15 #2

    Sign in to post your reply or Sign up for a free account.

    Similar topics

    0
    by: Charles Alexander | last post by:
    Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
    5
    by: Martin Schou | last post by:
    Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
    32
    by: Toby Newman | last post by:
    At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
    2
    by: Alex | last post by:
    Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
    3
    by: Ben R. | last post by:
    In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
    32
    by: cj | last post by:
    When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
    16
    by: Claudio Grondi | last post by:
    Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
    29
    by: garyusenet | last post by:
    I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
    2
    ADezii
    by: ADezii | last post by:
    If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
    1
    by: ed5195 | last post by:
    All programming languages have a loop capability. Looping is the program logic’s ability to repeat one or more lines of code either: 1. A set number of times, a “for loop” (i.e. 10, 100, 500, 763 or...
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
    0
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    0
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    0
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
    0
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
    0
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
    0
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.