1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ while loops

Discussion in 'C++' started by becki, Sep 12, 2006.

  1. becki

    becki New Member

    Joined:
    Sep 12, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi every one,
    im working on a program to convert the binary number system to the decimal one. I have the code yet im wondering if it a while loop can be used instead of a for loop. i have major problems trying to work out for loops, stupid me!

    Code:
    #include <iostream>
    #include <cmath>
    usingnamespace std;
    void bintodec(int binarynumber, int& decimal, int& weight);
    int main ()
    {
    	int decimalnum;
    	int binarynum;
    	int bitweight;
    	int digit;
    	int weight;
    	decimalnum = 0;
    	bitweight = 0;
    	cout<<"Please enter the number of digits you will be using: ";
    	cin>>digit;
    	cout<<"Enter number in binary: ";
    	cin>>binarynum;
    	
    	bintodec(binarynum, decimalnum, bitweight);
    	cout<<"Binary "<<binarynum<<" = " << decimalnum << " decimal"<<endl;
    	return 0;
    }
    void bintodec(int binarynumber, int& decimal, int& weight)
    {
    	int bit;
    	int digit;
    	for(int weight; weight <= digit; weight++)
    	{
    		bit = binarynumber % 10;
    		decimal = decimal + bit * static_cast<int>(pow(2, weight));
    		binarynumber = binarynumber /10;
    		
    		bintodec (binarynumber, decimal, weight);
    	}
    }
    
    Here is the code, thanks so much for any1 who helps me.

    Cheers
    Becki
     
  2. Essial

    Essial New Member

    Joined:
    Sep 12, 2006
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Without reading your posted code, he's the most basic difference between FOR and WHILE loops:

    FOR loops have control over a variable. You use this if your have an incremental loop. That is, if you need to loop through a SET (of objects, indexes in an array, etc).

    WHILE loops have no control over a variable. They instead simply continue to loop until the condition specified is no longer met.

    Here is an example of a FOR and WHILE loop doing the same thing
    Code:
    for ( int i = 0; i < 10; i ++ )
    {
      // Do whatever
    }
    
    // or...
    
    int i = 0;
    while ( i < 10 )
    {
      // Do whatver
      i++;
    };
    
    As far as converting binary to decimal.. Binary numbers CANNOT be decimal.I don't quite understand what you are trying to achieve here in your code. I can understand a binary text string like "1101" being converted to an integer value of 13, but your code doesn't seem to be doing that.
     
  3. becki

    becki New Member

    Joined:
    Sep 12, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thanks and I figured out the difference of the loops. I am practising them now. Thanks for the help Essial.
     
  4. jsh

    jsh New Member

    Joined:
    Mar 18, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    for ( int i = 0; i < 10; i ++ )
    {
      // Do whatever
    }
    
    // or...
    
    int i = 0;
    while ( i < 10 )
    {
      // Do whatver
      i++;
    };
    
    // or...
    
    int i=0;
    
    start:
    
    // Do whatver
    i++;
    
    if ( i < 10 )
      goto start; 
    
    // or...
    
    run( 10 );
    ...
    void run (int a)
    {
      if ( a > 0)
        {
           // Do whatver
           run(a-1);
        }
     
    }
    
    // or...
    
    int i=0;
    
    do{
    
    // Do whatver
    i++;
    
    }while(i<=10);
     
  5. najmatsuhail

    najmatsuhail New Member

    Joined:
    Mar 30, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    how to creat c++ programme that i can write the numbers from 1 to 10, and write even on the top of even numbers or odd on the top of odd numbers ,, with useing these orders \t, \n, if or while.

    the answer it should be lie this
    odd even
    1 2
    3 4
    5 6
    7 8
    9 10

    pls, pls, pls help fast
     
  6. jsh

    jsh New Member

    Joined:
    Mar 18, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    //.............
    #include<iostream.h>
    
    void main()
    {
      int a;
      int b;
      cout<<" A < B \n  A :";
      cin>>a;
      cout<<" B :";
      cin>>b;
      if(a % 2 ==0)
       cout<<' '<<'\t';
      while( b >= a )
       {
         cout<<a;
          if(a % 2 ==0)  { cout<<'\n'; }
            else  {  cout<<'\t';  }
            a++;
       } 
    }
    
    
    //..............
    
    for EX:
    Code:
    A : 5
    B : 10
    
    5----6
    7----8
    9----10
     
    
    ----------
    
    A = 4
    B = 11
    
    -----4
    5----6
    7----8
    9----10
    11----
    
     

Share This Page