Like Tree2Likes
  • 2 Post By laserlight

new way of doing it

This is a discussion on new way of doing it within the C++ Programming forums, part of the General Programming Boards category; Code: deduction = 0; if(deductiontype.compare("Itemized") == 0) { while(1) { input>> deditem; if(deditem==0) break; deduction =deduction+ deditem; } } im ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Question new way of doing it

    Code:
     deduction = 0;
          if(deductiontype.compare("Itemized") == 0)
           {
             while(1)
               {
              input>> deditem;
              if(deditem==0)
    
             break;
               deduction =deduction+ deditem;
                }
          }
    im trying to figure out if there a different way to put while(1)
    which equalls true but is there a different way without putting 1 in there

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Code:
    do{ 
    	cin >> ded;
    	deduction+=ded;
    }while ( ded != 0);
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    For starters, format your code properly, e.g.,
    Code:
    deduction = 0;
    if (deductiontype.compare("Itemized") == 0)
    {
        while (1)
        {
            input >> deditem;
            if (deditem == 0)
                break;
            deduction = deduction + deditem;
        }
    }
    Now, it is easy to see that you can simplify to:
    Code:
    deduction = 0;
    if (deductiontype.compare("Itemized") == 0)
    {
        while ((input >> deditem) && deditem != 0)
        {
            deduction += deditem;
        }
    }
    Incidentally, if you were just looking to replace the 1, then replace it with true, or you can use for (;;) instead.
    Salem and iMalc like this.
    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

Popular pages Recent additions subscribe to a feed

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