counter = 0; //line counter is initially set to zero.
for(f = 1.0; f <= 100.0; f++, number++, counter++ ) {
m = f / 3.28; //converts it to meters
cout << f << " feet is " << m << " meters.\n";
}
// every 10th line, print a blank line
if(counter == 10) {
cout << "\n"; // outputs a blank line
counter = 0;
}
if(number == 100) {
system ("pause");
}
return 0;
}
I want it to pause when it gets to 100 (for obvious reasons). But this doesn't seem to work and i've been at this for an hour now and i've get to figure it out. Any ideas?
counter = 0; //line counter is initially set to zero.
for(f = 1.0; f <= 100.0; f++ ) {
m = f / 3.28; //converts it to meters
cout << f << " feet is " << m << " meters.\n";
for(number=1; f <= 100; number=number+1)
if(number == 100)
system ("pause");
}
// every 10th line, print a blank line
if(counter == 10) {
cout << "\n"; // outputs a blank line
counter = 0;
}
return 0;
}
I messed around with it a little
also: Athar which if statement are you refering to?
#include <iostream>
usingnamespace std;
int main ()
{
double f, m; // Hard to notice variables.
int counter, number; // Not needed.
counter = 0; // Could do this in definiton.
for(f = 1.0; f <= 100.0; f++ ) // Usually avoid floats in loops.
// Experience has shown that floats and doubles don't play well with condition statements.
{
m = f / 3.28;
cout << f << " feet is " << m << " meters.\n";
for(number=1; f <= 100; number=number+1) // Because there is no semicolon here it looks to the
// next statements to loop
if(number == 100) // You are now looping this if statement.
system ("pause"); // So when you get to 100 you pause. If the user hits enter here,
// they will hit an infinite loop.
}
// This is not in the loop. So this does nothing really.
if(counter == 10) {
cout << "\n";
counter = 0;
}
// Look below for corrected code.
return 0;
}
#include <iostream>
int main ()
{
double feet = 1.0, meters; // Easier top know what variables are for what and where they are in code with a glance
for (int i = 0; i < 100; ++i) // Its usually better to avoid loops with floats or doubles
// They don't play well with comparison operators
{
meters = feet / 3.28; // Convert
std::cout << feet << " feet is equal to " << meters << " meters.\n"; // Output
if (!(i % 10) && (i != 0)) // On every 10th line, add a newline. Note the modulous operator.
// !(i % 10) means if the remainder of i/10 is 0 then it is true in terms of a bool operator.
// The second condition makes sure its not done when i == 0 because 0/10 has a remainder of 0.
std::cout << '\n';
feet += 1.0; // Increment feet by 1.0
}
std::cin.ignore();
std::cin.get();
}