14 Replies - 867 Views - Last Post: 16 April 2010 - 07:36 AM Rate Topic: -----

#1 Skylock3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 10

Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 10:57 AM

I am needing help on making this code use a while loop instead of a do while loop. I can't get it to work when I try to use a while loop. Any ideas?



#include<iostream>
using namespace std;
 
int main ()
{
 float speed=0;
 float time=0;
 float distanceTraveled=0;
 cout<<"what is the speed of the vehicle in M.P.H ?";
 cin>>speed;
 cout<<"How many hours did you travel ?";
 cin>>time;
 int i=1;
 
 if (speed>=1)
  {
   if (time>=1)
   {   
      do            
     {
      cout<< "Hour  Distance Traveled\n";
      cout<<"_________________________________"<<"\n";
 
    
      distanceTraveled = speed * i;
      cout <<i<<"   "<<distanceTraveled<< endl;
      i++;
     }
   while (i<=time);
   
   }
  else
   cout << " Time can not be less than 1 "<<"\n";
  }
  else
   cout<< " Speed can not be a negative number"<<"\n";
 
 return 0;
}






Is This A Good Question/Topic? 0
  • +

Replies To: Changing a "Do while" loop to just a "while" loop

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 11:04 AM

Step 1: define not working.
Step 2: Describe what you tried to solve your problem.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 11:41 AM

I see what he is saying...It works fine he just wants to use a while loop instead of a do while loop.

Is that correct???
Was This Post Helpful? 0

#4 amdcpp  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 06-February 10

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 11:42 AM


The only difference between a do-while and a while loop is that a do-while
loop doesn't check the continuation expression, in this case, "i <= time"
before actually entering the loop. A while loop WILL check the continuation
expression first, if it's false, it will just skip it. do-while is guaranteed
to run the loop at least once.

In your do-while loop you have the 'formatting' code:
Hour Distance Travelled
_________________________________

This should not be in the loop because you don't want it to print for each
iteration of the loop. Instead, you want that part before the loop executes.
Then to translate from a do-while to a while loop, you just take the continuation
expression in the do-while and use it for the while:
while (continuation-expression) {
// code here, might be skipped if the
// expression is false.
}



Instead of:
do {
// code ran at least once, guaranteed
} while (continuation-expression);



Was This Post Helpful? 0
  • +
  • -

#5 Guest_Guest*


Reputation:

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 11:54 AM

It looks like there may be some attempt at validation of information here...

Maybe he doesnt want the loop to execute if the information is not valid???

If that is the case would a while loop work here??

Sorry, I do not know how to code that maybe someone else does.
Was This Post Helpful? 0

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 12:14 PM

Simply copy the while from the bottom of the do-while loop over the do and delete the ending while.

while(i <= time)
{
 // code here
}

Was This Post Helpful? 0
  • +
  • -

#7 Guest_Guest*


Reputation:

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 12:18 PM

Tell you what man take the "do" out and compile it

Then post your errors ... These guys are good they can help
Was This Post Helpful? 0

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 12:20 PM

Quote

Tell you what man take the "do" out and compile it


Have you tried to run this?

{

// code

}while(i <= time);


Don't just take the do out. It won't do a dang thing except give you a syntax error.
Was This Post Helpful? 0
  • +
  • -

#9 Skylock3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 10

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 12:33 PM

View Posteker676, on 15 April 2010 - 11:20 AM, said:

Quote

Tell you what man take the "do" out and compile it


Have you tried to run this?

{

// code

}while(i <= time);


Don't just take the do out. It won't do a dang thing except give you a syntax error.


When I replace the "do" with the "while" from bottom of the do while loop, I compile it and it says enter mph and time traveled and after I do that, it does not go on to make speed and time traveled appear like it's supposed to.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Guest*


Reputation:

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 01:43 PM

Did you get it working
Did you try what EKER676 said


View PostSkylock3r, on 15 April 2010 - 11:33 AM, said:

View Posteker676, on 15 April 2010 - 11:20 AM, said:

Quote

Tell you what man take the "do" out and compile it


Have you tried to run this?

{

// code

}while(i <= time);


Don't just take the do out. It won't do a dang thing except give you a syntax error.


When I replace the "do" with the "while" from bottom of the do while loop, I compile it and it says enter mph and time traveled and after I do that, it does not go on to make speed and time traveled appear like it's supposed to.

Was This Post Helpful? 0

#11 jeremycstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-September 09

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 03:07 PM

View PostGuest, on 15 April 2010 - 12:43 PM, said:

Did you get it working
Did you try what EKER676 said


View PostSkylock3r, on 15 April 2010 - 11:33 AM, said:

View Posteker676, on 15 April 2010 - 11:20 AM, said:

Quote

Tell you what man take the "do" out and compile it


Have you tried to run this?

{

// code

}while(i <= time);


Don't just take the do out. It won't do a dang thing except give you a syntax error.


When I replace the "do" with the "while" from bottom of the do while loop, I compile it and it says enter mph and time traveled and after I do that, it does not go on to make speed and time traveled appear like it's supposed to.



I tried it both ways for you and could not get the loop to execute

I dont know enough about it to help but I tried sorry

    #include<iostream>
using namespace std;
 
int main ()
{
 int mph=0,hours=0,distanceTraveled=0;
 
 cout<<"How fast is the vehicle traveling in M.P.H ?";
 cin>>mph;
 cout<<"How many hours have you traveled ?";
 cin>>hours;
 int i = 1;
 
	if (mph>=1)
	{
		if (hours>=1)
		{ 
      
					while (i<=hours);
					{
					cout<< "Hour  Distance Traveled\n";
					cout<<"______________________"<<"\n";
 
    
					distanceTraveled = mph * i;
					cout <<i<<"   "<<distanceTraveled<< endl;
					i++;
					}
   
   
			}
		else
			cout << " The hours traveled cant be less than 1 "<<"\n";
		}
  else
   cout<< " The mph cant be a negative number"<<"\n";
 
 return 0;
}


This post has been edited by jeremycstephens: 15 April 2010 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#12 Galois  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 207
  • Joined: 27-March 10

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 03:53 PM

View Posteker676, on 15 April 2010 - 11:20 AM, said:

Quote

Tell you what man take the "do" out and compile it


Have you tried to run this?

{

// code

}while(i <= time);


Don't just take the do out. It won't do a dang thing except give you a syntax error.

Such code doesn't produce a syntax error. It will compile just fine. It defines a block scope (the stuff in braces) and a while-loop that does nothing (due to semicolon at the end of it). If its test-expression is true, then it will produce an infinite loop.
Was This Post Helpful? 1
  • +
  • -

#13 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Changing a "Do while" loop to just a "while" loop

Posted 15 April 2010 - 04:11 PM

well the easiest way to do it (without modification):
do {
   bar();
} while (foo);

//becomes

bar();
while (foo) {
   bar();
}

Was This Post Helpful? 0
  • +
  • -

#14 KBoogle  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 93
  • Joined: 15-April 10

Re: Changing a "Do while" loop to just a "while" loop

Posted 16 April 2010 - 06:33 AM

Did you get it working, Skylock3r?
All you do is put while instead of do, along with the testing condition.

So this code,
 do            
     {
      cout<< "Hour  Distance Traveled\n";
      cout<<"_________________________________"<<"\n";
 
    
      distanceTraveled = speed * i;
      cout <<i<<"   "<<distanceTraveled<< endl;
      i++;
     }
   while (i<=time);



Becomes this:
 while (i<=time)            
     {
      cout<< "Hour  Distance Traveled\n";
      cout<<"_________________________________"<<"\n";
 
    
      distanceTraveled = speed * i;
      cout <<i<<"   "<<distanceTraveled<< endl;
      i++;
     }



Just be sure that you have the condition defined before the program enters a while loop, or else it won't compile. In a do loop, you can assign within the loop body, because it will run at least once. But since a while loop first checks the condition before running it, you need the condition assigned beforehand.
Was This Post Helpful? 1
  • +
  • -

#15 Skylock3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-April 10

Re: Changing a "Do while" loop to just a "while" loop

Posted 16 April 2010 - 07:36 AM

View PostKBoogle, on 16 April 2010 - 05:33 AM, said:

Did you get it working, Skylock3r?
All you do is put while instead of do, along with the testing condition.

So this code,
 do            
     {
      cout<< "Hour  Distance Traveled\n";
      cout<<"_________________________________"<<"\n";
 
    
      distanceTraveled = speed * i;
      cout <<i<<"   "<<distanceTraveled<< endl;
      i++;
     }
   while (i<=time);



Becomes this:
 while (i<=time)            
     {
      cout<< "Hour  Distance Traveled\n";
      cout<<"_________________________________"<<"\n";
 
    
      distanceTraveled = speed * i;
      cout <<i<<"   "<<distanceTraveled<< endl;
      i++;
     }



Just be sure that you have the condition defined before the program enters a while loop, or else it won't compile. In a do loop, you can assign within the loop body, because it will run at least once. But since a while loop first checks the condition before running it, you need the condition assigned beforehand.


Yes, it works now. Thanks so much for the descriptive help. Much appreciated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1