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

This is a discussion on How to exit out of a while loop? (short code) within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int total_value=0; printf("Line: "); // user enters a word/sentence char mynum; scanf("%c",&mynum); while(mynum!='.') // ...

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

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

    Code:
     #include <stdio.h>
       int main()
       {
      
       int total_value=0;
      
       printf("Line: "); // user enters a word/sentence
       char mynum;
       scanf("%c",&mynum);
     
      while(mynum!='.') // while the entry isn't '(period)'
      {
      mynum+=total_value;
      }
      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
    Location
    Singapore
    Posts
    21,781
    You need to call scanf in the loop too. Oh, and please indent your code properly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Location
    New Zealand
    Posts
    6,305
    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, 03:59 PM
  5. Can't exit this loop
    By Goalie35 in forum C Programming
    Replies: 4
    Last Post: 10-11-2001, 01:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21