problem with loop

This is a discussion on problem with loop within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <conio.h> #include <stdio.h> #include <windows.h> #include <time.h> #define sleep Sleep int main(void) { float grav = ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    problem with loop

    Code:
    
    #include <iostream.h>
    #include <conio.h> 
    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    
    
    #define sleep Sleep
    int main(void)
    {
    
    
    
    
    
    	float grav = 9.80;
    	float mass, acceleration,weight,mew, time,finalvelocity,initialvelocity, speed,gravity, force, torque, forcen, friction, forcep, distance, mass2;
    	int poop;
    	int butt;
    	int finger;
    	int me;
    
    
    
    
    
    
    	
    
    
    	
    	
    	
    	
    	while(poop)
    	{
    		
    		Sleep(2000);
    
    		{
    
    			
    			int i;
      
    			for (i = 0; i < 50; i++)
    			putchar ('\n');
        
    		
    	
    	cout<<endl<<"What would you like to find?"<<endl;
    	cout<<"(1) Acceleration or Speed"<<endl;
    	cout<<"(2) Weight or Fw "<<endl;
    	cout<<"(3) Mass "<<endl;
    	cout<<"(4) Force "<<endl;
    	cout<<"(5) Mew and the forces of friction "<<endl;
    	cout<<"(6) Exit the program"<<endl;
    
    
    	cout<<":";
    	cin>>poop;
    	
    		
        
    		}
    		
    		{
    
    			
    			int i;
      
    			for (i = 0; i < 50; i++)
    			putchar ('\n');
    	
    	if (poop == 1)
    	{
    		
    		cout<<"Which would you like to find?"<<endl;
    		cout<<"(1) Acceleration "<<endl;
    		cout<<"(2) Speed "<<endl;
    		cout<<":";
    		cin>>butt;
    	
    		if (butt == 1)
    		{
    
    			cout<<"What is the final velocity in m/s?:";
    			cin>>finalvelocity;
    			cout<<endl<<"What is the intial velocity in m/s?:";
    			cin>>initialvelocity;
    			cout<<endl<<"What is the elapsed time in seconds?:";
    			cin>>time;
    
    			acceleration = (finalvelocity - initialvelocity)/time;
    
    			cout<<"The acceleration is : "<<acceleration;
    		
    			cout<<" m/s^2"<<endl;
    		}
    	}
    	
    
    
    		if (butt == 2)
    		{
    
    			cout<<"Enter the total distance : ";
    			cin>>distance;
    			cout<<endl<<"Enter the total time : ";
    			cin>>time;
    
    			speed = distance/time;
    			cout<<"The speed is : "<<speed;
    			cout<<" m/s"<<endl;
    		}
    
    		
    	
    	
    
    	if (poop == 2)
    	{
    
    		cout<<"What is the mass in kilograms?:";
    		cin>>mass;
    		
    		weight = mass * grav;
    		cout<<"The weight in Newtons is: "<<weight<<endl;
    
    	}
    	
    	if (poop == 3)
    	{
    
    		cout<<"What is the weight in newtons?:";
    		cin>>weight;
    		mass = weight / grav;
    		cout<<"The mass of the object in kilograms is: "<<mass<<endl;
    		
    	}
    	
    	if (poop == 4)
    	{
    
    		cout<<"Which would you like to do?"<<endl;
    		cout<<"(1) Force and acceleration on bodies of known weight Force = Fw*a/g"<<endl;
    		cout<<"(2) Newton's second law of motion Force = ma"<<endl;
    		cout<<"(3) Law of universal gravitation Force = G*m1*m2/(d)^2"<<endl;
    		cout<<":";
    		cin>>finger;
    
    		if (finger == 1)
    		{
    
    			cout<<"Enter the weight (Fw):";
    			cin>>weight;
    			cout<<endl<<"Enter the acceleration :";
    			cin>>acceleration;
    			cout<<endl<<"Enter the force of gravity :";
    			cin>>gravity;
    
    			force = weight*acceleration/gravity;
    
    			cout<<"The force is "<<force;
    			cout<<" newtons"<<endl<<endl<<endl;
    
    		}
    
    		if(finger == 2)
    		{
    
    			cout<<"Enter the mass :";
    			cin>>mass;
    			cout<<endl<<"Enter the acceleration :";
    			cin>>gravity;
    
    			force = mass*acceleration;
    
    			cout<<"The force is "<<force;
    			cout<<" newtowns"<<endl<<endl<<endl;
    		}
    		
    		if(finger == 3)
    		{
    
    			cout<<"Enter the gravitational constant :";
    			cin>>gravity;
    			cout<<endl<<"Enter the mass of the first body :";
    			cin>>mass;
    			cout<<endl<<"Enter the mass of the second body :";
    			cin>>mass2;
    			cout<<endl<<"Enter the distance between their centers of mass :";
    			cin>>distance;
    			cout<<endl<<endl;
    
    			force = gravity*(mass*mass2)/(distance*distance);
    
    			cout<<endl<<"The force is "<<force;
    			cout<<" newtons";
    
    			cout<<endl<<endl;
    		}
    
    
    	
    	}
    
    	if (poop == 5)
    	{
    
    		cout<<endl<<"What would you like to do?"<<endl;
    		cout<<"(1) Find Mew (the coefficient of friction)"<<endl;
    		cout<<"(2) Find the force of friction "<<endl;
    		cout<<"(3) Find the normal force "<<endl;
    		cout<<"(4) dummie"<<endl;
    		cout<<":";
    		cin>>me;
    
    		if(me == 1)
    		{
    
    			cout<<endl<<"Enter the force of friction :";
    			cin>>friction;
    			cout<<endl<<"Enter the normal force :";
    			cin>>forcen;
    			
    			mew = friction/forcen;
    
    			cout<<endl<<"The coefficient of friction is "<<mew<<endl<<endl;
    		}
    			
    		if(me == 2)
    		{
    			cout<<endl<<"Enter the coefficient of friction :";
    			cin>>mew;
    			cout<<endl<<"Enter the normal force :";
    			cin>>forcen;
    			
    			friction = mew*forcen;
    
    			cout<<endl<<"The frictional force is "<<friction<<endl<<endl;
    		}
    
    	
    	
    	
    		if(me == 3)
    		{
    
    			cout<<endl<<"Enter the frictional force :";
    			cin>>friction;
    			cout<<endl<<"Enter the coefficient of friction :";
    			cin>>mew;
    
    			forcen = friction/mew;
    
    			cout<<endl<<"The normal force is "<<forcen;
    			cout<<" newtons";/*right here it should display the answer, clear the screen then loop 
    							 back to the main option menu, but for some reason it displays the answer 
    							 when the main menu is looped back*/
    		}
    		if(me == 4)
    		{
    
    			cout<<endl<<"you are a dummie!!!";
    
    		}
    
    	
    	
    	}
    
    	
    	if(poop == 6)
    	{
    
    		break;
    	}
    	
    
    }
    }
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Not sure of your exact problem becuase you really didnt specify.
    But i ran the program a few times and found the error in your exit statement. So take a look at the last line, becuase your exit portion didnt exit until it asked you for distance.
    Code:
    #include <iostream.h>
    #include <conio.h> 
    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    
    
    #define sleep Sleep
    int main(void)
    {
    
    
    
    
    
    	float grav = 9.80;
    	float mass, acceleration,weight,mew, time,finalvelocity,initialvelocity, speed,gravity, force, torque, forcen, friction, forcep, distance, mass2;
    	int poop;
    	int butt;
    	int finger;
    	int me;
    
    
    
    
    
    
    	
    
    
    	
    	
    	
    	
    	while(poop)
    	{
    		
    		Sleep(2000);
    
    		{
    
    			
    			int i;
      
    			for (i = 0; i < 50; i++)
    			putchar ('\n');
        
    		
    	
    	cout<<endl<<"What would you like to find?"<<endl;
    	cout<<"(1) Acceleration or Speed"<<endl;
    	cout<<"(2) Weight or Fw "<<endl;
    	cout<<"(3) Mass "<<endl;
    	cout<<"(4) Force "<<endl;
    	cout<<"(5) Mew and the forces of friction "<<endl;
    	cout<<"(6) Exit the program"<<endl;
    
    
    	cout<<":";
    	cin>>poop;
    	
    		
        
    		}
    		
    		{
    
    			
    			int i;
      
    			for (i = 0; i < 50; i++)
    			putchar ('\n');
    	
    	if (poop == 1)
    	{
    		
    		cout<<"Which would you like to find?"<<endl;
    		cout<<"(1) Acceleration "<<endl;
    		cout<<"(2) Speed "<<endl;
    		cout<<":";
    		cin>>butt;
    	
    		if (butt == 1)
    		{
    
    			cout<<"What is the final velocity in m/s?:";
    			cin>>finalvelocity;
    			cout<<endl<<"What is the intial velocity in m/s?:";
    			cin>>initialvelocity;
    			cout<<endl<<"What is the elapsed time in seconds?:";
    			cin>>time;
    
    			acceleration = (finalvelocity - initialvelocity)/time;
    
    			cout<<"The acceleration is : "<<acceleration;
    		
    			cout<<" m/s^2"<<endl;
    		}
    	}
    	
    
    
    		if (butt == 2)
    		{
    
    			cout<<"Enter the total distance : ";
    			cin>>distance;
    			cout<<endl<<"Enter the total time : ";
    			cin>>time;
    
    			speed = distance/time;
    			cout<<"The speed is : "<<speed;
    			cout<<" m/s"<<endl;
    		}
    
    		
    	
    	
    
    	if (poop == 2)
    	{
    
    		cout<<"What is the mass in kilograms?:";
    		cin>>mass;
    		
    		weight = mass * grav;
    		cout<<"The weight in Newtons is: "<<weight<<endl;
    
    	}
    	
    	if (poop == 3)
    	{
    
    		cout<<"What is the weight in newtons?:";
    		cin>>weight;
    		mass = weight / grav;
    		cout<<"The mass of the object in kilograms is: "<<mass<<endl;
    		
    	}
    	
    	if (poop == 4)
    	{
    
    		cout<<"Which would you like to do?"<<endl;
    		cout<<"(1) Force and acceleration on bodies of known weight Force = Fw*a/g"<<endl;
    		cout<<"(2) Newton's second law of motion Force = ma"<<endl;
    		cout<<"(3) Law of universal gravitation Force = G*m1*m2/(d)^2"<<endl;
    		cout<<":";
    		cin>>finger;
    
    		if (finger == 1)
    		{
    
    			cout<<"Enter the weight (Fw):";
    			cin>>weight;
    			cout<<endl<<"Enter the acceleration :";
    			cin>>acceleration;
    			cout<<endl<<"Enter the force of gravity :";
    			cin>>gravity;
    
    			force = weight*acceleration/gravity;
    
    			cout<<"The force is "<<force;
    			cout<<" newtons"<<endl<<endl<<endl;
    
    		}
    
    		if(finger == 2)
    		{
    
    			cout<<"Enter the mass :";
    			cin>>mass;
    			cout<<endl<<"Enter the acceleration :";
    			cin>>gravity;
    
    			force = mass*acceleration;
    
    			cout<<"The force is "<<force;
    			cout<<" newtowns"<<endl<<endl<<endl;
    		}
    		
    		if(finger == 3)
    		{
    
    			cout<<"Enter the gravitational constant :";
    			cin>>gravity;
    			cout<<endl<<"Enter the mass of the first body :";
    			cin>>mass;
    			cout<<endl<<"Enter the mass of the second body :";
    			cin>>mass2;
    			cout<<endl<<"Enter the distance between their centers of mass :";
    			cin>>distance;
    			cout<<endl<<endl;
    
    			force = gravity*(mass*mass2)/(distance*distance);
    
    			cout<<endl<<"The force is "<<force;
    			cout<<" newtons";
    
    			cout<<endl<<endl;
    		}
    
    
    	
    	}
    
    	if (poop == 5)
    	{
    
    		cout<<endl<<"What would you like to do?"<<endl;
    		cout<<"(1) Find Mew (the coefficient of friction)"<<endl;
    		cout<<"(2) Find the force of friction "<<endl;
    		cout<<"(3) Find the normal force "<<endl;
    		cout<<"(4) dummie"<<endl;
    		cout<<":";
    		cin>>me;
    
    		if(me == 1)
    		{
    
    			cout<<endl<<"Enter the force of friction :";
    			cin>>friction;
    			cout<<endl<<"Enter the normal force :";
    			cin>>forcen;
    			
    			mew = friction/forcen;
    
    			cout<<endl<<"The coefficient of friction is "<<mew<<endl<<endl;
    		}
    			
    		if(me == 2)
    		{
    			cout<<endl<<"Enter the coefficient of friction :";
    			cin>>mew;
    			cout<<endl<<"Enter the normal force :";
    			cin>>forcen;
    			
    			friction = mew*forcen;
    
    			cout<<endl<<"The frictional force is "<<friction<<endl<<endl;
    		}
    
    	
    	
    	
    		if(me == 3)
    		{
    
    			cout<<endl<<"Enter the frictional force :";
    			cin>>friction;
    			cout<<endl<<"Enter the coefficient of friction :";
    			cin>>mew;
    
    			forcen = friction/mew;
    
    			cout<<endl<<"The normal force is "<<forcen;
    			cout<<" newtons";/*right here it should display the answer, clear the screen then loop 
    							 back to the main option menu, but for some reason it displays the answer 
    							 when the main menu is looped back*/
    		}
    		if(me == 4)
    		{
    
    			cout<<endl<<"you are a dummie!!!";
    
    		}
    
    	
    	
    	}
    
    	
    	if(poop == 6)
    	{
    
    		exit(0); //// I CHANGED THIS
    	}
    	
    
    }
    }
    
    
    
    
    }
    If you notice i changed
    Code:
    if(poop == 6)
    	{
    
    		break;
    	}
    to this


    Code:
    (poop == 6)
    	{
    
    		exit(0); //// I CHANGED THIS
    	}

    NOTE: To make this much more easier for people to read your code, use different names instaed of POOP and FINGER AND BUTT.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    haven't you posted this same code before? anyhow...the while loop is wrong as int poop is not initialized to anything...

    either poot in a different variable(see code) in there or initialize poop to 1
    Code:
    bool notDone = true;
    
    while( notDone ){
            //loop body here...
    
           //check if done with maybe an if statement
           cout << "Again?: ";
           cin >> answer;    //char variable
       
           if( answer == 'N' ){   //many different possibilities to make this check
                 notDone = false;
           }
    }
    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    here's the downlo' on the problem

    alright... i am having trouble with this area of code

    Code:
    		if(me == 3)
    		{
    
    			cout<<endl<<"Enter the frictional force :";
    			cin>>friction;
    			cout<<endl<<"Enter the coefficient of friction :";
    			cin>>mew;
    
    			forcen = friction/mew;
    
    			cout<<endl<<"The normal force is "<<forcen;
    			cout<<" newtons";/*right here it should display the answer, clear the screen then loop 
    							 back to the main option menu, but for some reason it displays the answer 
    							 when the main menu is looped back*/
    		}
    		if(me == 4)
    		{
    
    			cout<<endl<<"you are a dummie!!!";
    it is supposed to display the answer to the "if(me ==3)" statement... then loop back to the main menu but for some reason it just loops back to main menu and displays the answer right above it. I cant figure it out.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    don't see any command to clear the screen .....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM

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