Thread: While Loops

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Angry While Loops

    I'm trying to write a program with a loop that has the user enter 2 numbers and ends only when half the product of the 2 numbers entered is less than either number

    Here is what i have so far:
    Code:
    #include <iostream.h>
    
    int main()
    {
    	// Declaring Variables
    	int hold;
    	int num1, num2;
    	int product;
    	
    	// Inputting Numbers
    	cout << "Enter 1st number : ";
    	cin  >> num1;
    	cout << "Enter 2nd number : ";
    	cin  >> num2;
    	
    	// While Loop
    	while (product !=0)
    	{
    		product = ((num1 * num2)/2);
    		cout << product << endl;
    	break;
    	}
    	cin >> hold;
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    changes are in bold
    Code:
    #include <iostream.h>
    
    int main()
    {
    // Declaring Variables
    int hold;
    int num1, num2;
    int product;
    
    // While Loop
     // while (product !=0)
    do
    {
    
    // Inputting Numbers
    cout << "Enter 1st number : ";
    cin  >> num1;
    cout << "Enter 2nd number : ";
    cin  >> num2;
    
    // product = ((num1 * num2)/2);
    // don't call this variable "product" if it doesn't hold the product
    product = num1 * num2;
    cout << product << endl;
     // break;
    }while(product/2 >= num1 && product/2 >= num2);
    cin >> hold; // this could just be cin.get(); 
    return 0;
    }
    EDIT: I didn't notice that just half the product was being stored in "product"
    Last edited by Codulation; 01-15-2003 at 03:49 PM.
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Codulation, that was a good try but the code you wrote does not do what i want it to do.Thanks for trying.
    I'm trying to write a program with a loop that has the user enter 2 numbers and ends only when half the product of the 2 numbers entered is less than either number

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Originally posted by sonict
    Codulation, that was a good try but the code you wrote does not do what i want it to do.Thanks for trying.
    I'm trying to write a program with a loop that has the user enter 2 numbers and ends only when half the product of the 2 numbers entered is less than either number
    My code prompts the user for two numbers, and does so until one-half the product of said two numbers is less than at least one of the two numbers entered.

    I suppose you want something different but are having trouble putting it into words.

    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Codulation, if you run the program and let's say for example you input 24 and 36, the program does ((24*36)/2) but I think I'm the one having trouble explaning my problem so I'll restate it.
    Let's use the example of inputting 24 and 36. I want the program to divide until that number is less than the numbers inputted which in this case would be 24 and 36. I'm sorry for not explaning the program correctly.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    >I want the program to divide until that number is less than the numbers inputted which in this case would be 24 and 36

    No prob.

    Code:
    #include <iostream.h>
    
    int main()
    {
    // Declaring Variables
    int hold;
    int num1, num2;
    int product;
    
    // Inputting Numbers
    cout << "Enter 1st number : ";
    cin  >> num1;
    cout << "Enter 2nd number : ";
    cin  >> num2;
    
    product = num1 * num2;
    
    // While Loop
    do
    {
      product /= 2;
      cout << "Current product is: " << product << endl;
    }while(product >= num1 && product >= num2);
    
    cin >> hold; 
    return 0;
    }
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    Thanks Codulation. I'm sorry for not explaning it correctly and I'm new to c++ and learning by myself. Thank you so much.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    No problem at all!

    > I'm new to c++ and learning by myself.

    I wish you the best of luck. Ask lots of questions and above all just try things over and over. You have a fun road ahead

    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM