Noob alert! I am learning C++ through an online class and a Dummies book. I am unsure if I should be using a do while loop for my program. I have to put in a value for "a" and it will compute how long it will take for it to become something else (x) at a rate of 2%. Should this be a do while loop or a for loop?...or something entirely else. Here's what I have so far:
/*------------------------------------------------------------------------------------------------------------------------*/
/* Write a program to determine how many years are required for the # of acres to be completely reforestation */
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
// Declare and initialize objects.
double a; // acres of reforestation.
double x; // time it took for reforestation.
do
{
cout << "Number of acres to determine \n";
cin >> a;
x = ((a+.02)*100);
} while (a>0; ++a);
cout << x << "Number of years for acres to be reforested"<< endl;
// Wait to close the program.
system ("PAUSE");
return 0;
}
/*-------------------------------------------------------------------------------------------------------------------------*/
Your program does not compile.
I suggest you to use the tutorials and learn the syntax http://cplusplus.com/doc/tutorial/control/
The only difference is that do-while loop is executed at least one time.
Then understand that it is while and not until
Your code makes little sense. Make a desk test.
Dummies book
I'd heard bad critics about that book. system ("PAUSE"); is one of them.
/*----------------------------------------------------------------------------------------------------------*/
/* Write a program to determine how many years are required for the # of acres to be reforested */
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
// Declare and initialize objects.
double acre; // acres of reforestation.
double forest; // time it took for reforestation.
//Prompt user for input.
cout << "Please enter the numbers of acres to be reforested \n";
//Input of number of acres.
cin >> acre;
while (acre > 0);
{
forest = ((acre+.02)*100);
cin >> acre;
}
cout << "Number of years for reforestation is \n" << endl;
// Wait to close the program.
system ("PAUSE");
return 0;
}
/*-------------------------------------------------------------------------------------------------------------------------*/