Thread: A few program questions regarding while loops

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    1

    A few program questions regarding while loops

    I'm currently working on a couple of programs. The first deals with binary numbers. I'm supposed to take the number that is inputed and then pick off each digit, which I know how to do. Then, I am supposed to take each digit (and only if it is a 1 or a 0) and convert the whole thing to binary. Which, again, I know how to do. My only issue is with figuring out how to use the while and/or if statement to only do this if the numbers are all 1s or 0s. Here's my code so far:

    Code:
    #include <iostream>
    using namespace std;
    int main() {
        int digit, eval, num1, num2, num3, num4, num5, digit5, digit4, digit3, digit2, digit1, sum;
        cout << "Enter a Binary Number: ";
        cin >> digit;
        cout << " " << endl;
        num1 = (digit / 10000) % 10;
        num2 = (digit / 1000) % 10;
    
        num3 = (digit / 100) % 10;
        num4 = (digit / 10) % 10;
        num5 = (digit / 1) % 10;
              
        digit5 = num5 * 1;
        digit4 = num4 * (2 * 1);
        digit3 = num3 * (2 * 2);
        digit2 = num2 * (2 * 2 * 2);
        digit1 = num1 * (2 * 2 * 2 * 2);
        sum = digit5 + digit4 + digit3 + digit2 + digit1;
        cout << "\n";
        cout << "The decimal equivalent of " << digit << " is " << sum;
        cout << "\n";
        
        system("pause");
       
       return 0;
    }
    It's messy, but outside of the fact it works no matter what numbers are input, it works.

    OK, so that's that. Now I'm working on another program, where I accept input from the user in regards to mileage and gallons. I'm supposed to prompt the user to input their mileage and gallons for a trip, and then, based on that, display the MPG. Also, I am supposed to put up a total that displays beneath the MPG that is basically each MPG reported divided by the number of MPG input. Finally, I am supposed to make the program so that it quits if the user enters -1. Here's my code for that so far:

    Code:
    #include <iostream>
    using namespace std;
    int main() {
        int i, mileage, gallons, mpg, total;
        
        i = 1;
        total = 0;
        while (i <= 1) {
        while (mileage != -1) {
                  cout << "Enter Mileage for Trip #" << i << " : ";
                   cin >> mileage;
                   cout << "Enter Gallons for Trip #" << i << " : ";
                    cin >> gallons;
                    cout << "\n";
                    mpg = mileage / gallons;
                    cout << "MPG for Trip #" << i << " : " << mpg;
                    cout << "\n"; 
       total = (total + mpg) / i;   
       cout << "\n";     
       cout << "Total so far: " << total;
       cout << "\n";
       i = i + 1 ;
    }
    }
    cout << "\n";
    cout << "Thanks for using our program!";
    
    system("pause");
    return 0;
    }
    How can I get the while loop to work in the way I need it to work? I appreciate any suggestions.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    First, you need to initialize mileage prior to the first WHILE test.

    Second, if the user does enter -1 for mileage, then your program is going to ignore that fact and prompt the user for gallons anyway. Do you want that to happen?

    And third, I have to mention the obligatory "fix your indentation".

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Oh, and you don't need two WHILE loops - one will suffice.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sf.net/User:Elysia/Indentation
    The sign of inconsistent indentation levels and bad bracket placement is a sign you need some tutorials in indentation. Consider clicking the link.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. My first program (and a few questions)
    By elfjuice in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2002, 04:30 PM
  5. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 01:48 PM