#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;
}
14 Replies - 719 Views - Last Post: 16 April 2010 - 07:36 AM
#1
Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 10:57 AM
Replies To: Changing a "Do while" loop to just a "while" loop
#2
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 11:04 AM
Step 2: Describe what you tried to solve your problem.
#3 Guest_Guest*
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 11:41 AM
Is that correct???
#4
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);
#5 Guest_Guest*
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 11:54 AM
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.
#6
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 12:14 PM
while(i <= time)
{
// code here
}
#7 Guest_Guest*
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 12:18 PM
Then post your errors ... These guys are good they can help
#8
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 12:20 PM
Quote
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.
#9
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 12:33 PM
eker676, on 15 April 2010 - 11:20 AM, said:
Quote
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.
#10 Guest_Guest*
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 01:43 PM
Did you try what EKER676 said
Skylock3r, on 15 April 2010 - 11:33 AM, said:
eker676, on 15 April 2010 - 11:20 AM, said:
Quote
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.
#11
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 03:07 PM
Guest, on 15 April 2010 - 12:43 PM, said:
Did you try what EKER676 said
Skylock3r, on 15 April 2010 - 11:33 AM, said:
eker676, on 15 April 2010 - 11:20 AM, said:
Quote
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
#12
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 03:53 PM
eker676, on 15 April 2010 - 11:20 AM, said:
Quote
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.
#13
Re: Changing a "Do while" loop to just a "while" loop
Posted 15 April 2010 - 04:11 PM
do {
bar();
} while (foo);
//becomes
bar();
while (foo) {
bar();
}
#14
Re: Changing a "Do while" loop to just a "while" loop
Posted 16 April 2010 - 06:33 AM
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.
#15
Re: Changing a "Do while" loop to just a "while" loop
Posted 16 April 2010 - 07:36 AM
KBoogle, on 16 April 2010 - 05:33 AM, said:
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.
|
|

New Topic/Question
Reply




MultiQuote





|