How to implement a flag control in my loop?

This is a discussion on How to implement a flag control in my loop? within the C Programming forums, part of the General Programming Boards category; Code: int main() { int total_value=0; char mychar; int mynum; do { printf("Line: "); scanf("%c",&mychar); mynum=mychar; total_value=total_value+mynum; } while(mychar!='.'); printf("Checksum ...

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

    How to implement a flag control in my loop?

    Code:
     int main()
       {
       int total_value=0;
       char mychar;
       int mynum;
      
       do
      {
      printf("Line: ");
      scanf("%c",&mychar);
      mynum=mychar;
      total_value=total_value+mynum;
      } while(mychar!='.');
     
      printf("Checksum %c",total_value);
     
     
      return 0;
    My code works fine. It does the calculations and prints out the results. However with a bit of extras.

    So basically how do i implement a flag-control in this loop. Such that the program does calculations until it reachs a '.' (period)
    Code:
    while(flag=1)
    {
    
    }
    while(mynum!='.')
    {
    }

  2. #2
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by tmac619619 View Post
    My code works fine. It does the calculations and prints out the results.
    Are you sure? This
    Code:
    printf("Checksum %c",total_value);
    prints "total_value" as a character. I think you want to use %d to print the sum.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement a loop into my code?
    By CtrlAltElite in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2011, 09:10 AM
  2. Control loop with scanf
    By sandeep156 in forum C Programming
    Replies: 5
    Last Post: 05-02-2009, 02:06 PM
  3. flag-controlled while loop
    By azsquall in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2008, 02:48 PM
  4. Replies: 9
    Last Post: 11-12-2007, 02:29 PM
  5. Loop control
    By astro_not in forum C Programming
    Replies: 5
    Last Post: 02-28-2003, 07:43 AM

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