Thread: How do you end a program if conditions don't match?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    How do you end a program if conditions don't match?

    [code]
    Code:
     while(bread_type!='w && bread_type!='W')
     {
        printf("Please enter a valid character");
     
     
      }




    Basically what happens next is my program "crashes" and prints "Please enter a valid char" infinitely.
    Last edited by tmac619619; 10-22-2012 at 11:48 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're telling them to enter a valid character and then you're closing the program anyway?

    You could use exit(1), beneath the printf() statement, but it sounds wrong to end a program right after requesting a char.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Oh right. So i should use a loop to keep prompting the user to enter the correctchar right?


    Code:
     while(bread_type!='w && bread_type!='W')
     {
        printf("Please enter a valid character");
     
     
      }
    Uhmm basically i want to keep prompting the user till they enter the correct character.
    but my program crashes.. in an endless "Please enter a valid character" loop
    Last edited by tmac619619; 10-22-2012 at 11:43 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Take a long hard look at your if statement. It can never be false since everything is either not 'w' or not 'W'.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tmac619619 View Post
    Oh right. So i should use a loop to keep prompting the user to enter the correctchar right?


    Code:
     while(bread_type!='w && bread_type!='W')
     {
        printf("Please enter a valid character");
     
     
      }
    Uhmm basically i want to keep prompting the user till they enter the correct character.
    but my program crashes.. in an endless "Please enter a valid character" loop
    A while() loop is a bit awkward for this. Consider a do while loop:
    Code:
    do {
       printf("Please enter a valid character [w,d,r]: ");
       scanf(" %c",&mychar);
       mychar=tolower(mychar);
    }while(mychar != 'w' && mychar != 'd' && mychar != 'r');
    The difference is the naturalness of a test at the end of the loop, instead of at the beginning. This is when the loop will always be entered, at least once. A while loop may or may not be entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ program that will search word for match
    By mcoder in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2012, 12:54 PM
  2. How to simplify these conditions ? (engine model c program)
    By Jason Singh in forum C Programming
    Replies: 15
    Last Post: 10-24-2011, 11:41 PM
  3. Program to match filenames and print stat
    By chrisfrmatl in forum C Programming
    Replies: 6
    Last Post: 05-10-2010, 10:06 PM
  4. Problem with Program to Check if Separators Match
    By vileoxidation in forum C++ Programming
    Replies: 30
    Last Post: 10-16-2009, 08:57 PM
  5. Have You Got A program To Match Question.
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 06-01-2002, 03:50 PM