C++ Counter help

Ok so im building a counter that will continuosley count up. There is no limit but i cant figure out how to not put a limit cap on it?

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;


int Time = 0;

int main()
{
    while(Time <1){
    Time++;

        cout << Time;

        if(Time == 24){
        cout << Time;
        }
    }

    return 0;
}


so basically i want the code to increment forever. not sure how to do this though.
Last edited on
Just make sure the loop condition can never become false? (hint: "true" is never false).
for also allows omitting the loop condition, which causes it to loop forever: for (;;)
How would that look in code? I tried this but got errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;


int Time = 0;

int main()
{
    while(Time <1){
        Time++;

        for(Time){
            cout << Time;
        }
    }

return 0;
}


Errors:

C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|16|error: expected ';' before ')' token|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|18|warning: statement has no effect|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|19|error: expected primary-expression before '}' token|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|19|error: expected ';' before '}' token|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|19|error: expected primary-expression before '}' token|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|19|error: expected ')' before '}' token|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|19|error: expected primary-expression before '}' token|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|19|error: expected ';' before '}' token|
||=== Build finished: 7 errors, 1 warnings ===|
Last edited on
Easy...


And never put your variable names with first letter on caps.It is illegal.

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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;


int i = 2;

int main()
{
    while(i > 1){
    i++;

        cout << i;

        if(i == 24){
        cout << i;
        }
    }

system("PAUSE");
    return 0;
}


Ah it works thanks. Also i think system("PAUSE"); is illegal also or its not good to use, cant remember, but cin.get() is better to use.
It's fine if the first letter is uppercase, but it's rather unusual style.
I just wanted to add this:

This is an infinite loop:

while(true) or while(1)

And so is this:

for(;;) or for(;i++;)
Topic archived. No new replies allowed.