Thread: How to exit out of a while loop? (short code)

  1. #1
    Registered User
    Join Date
    Oct 2012

    How to exit out of a while loop? (short code)

     #include <stdio.h>
       int main()
       int total_value=0;
       printf("Line: "); // user enters a word/sentence
       char mynum;
      while(mynum!='.') // while the entry isn't '(period)'
      printf("Total values are %d",mynum);
      return 0;
    How would i break out of this while loop?
    Begins if i enter something other than (period), it keeps re-prompting me over and over again.
    Last edited by tmac619619; 11-02-2012 at 12:26 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You need to call scanf in the loop too. Oh, and please indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    total_value is zero, so line 13 currently has no effect. Since nothing changes the while loop condition, you then have an infinite loop.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my do-while loop won't exit
    By Darkroman in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2011, 08:42 AM
  2. why it can not exit the while loop
    By letmoon in forum C Programming
    Replies: 3
    Last Post: 10-30-2008, 09:59 AM
  3. Can't exit a do/while loop - please help
    By Vireyda in forum C++ Programming
    Replies: 11
    Last Post: 08-08-2005, 06:46 AM
  4. Cannot exit 'while' loop
    By s_ny33 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2005, 04:59 PM
  5. Can't exit this loop
    By Goalie35 in forum C Programming
    Replies: 4
    Last Post: 10-11-2001, 01:39 PM