Small loop issue

#include <iostream>
using namespace std;

int main ()
{
double f, m;
int counter, number;

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?

also the comments are for my sake, not yours. =/
Last edited on
You forgot to initialize number.
Your if statement also don't seem to be inside the loop.
#include <iostream>
using namespace std;

int main ()
{
double f, m;
int counter, number;

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?
Last edited on
Here's some comments on your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace 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;
}


And here is my version of your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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();
}
Topic archived. No new replies allowed.