Simple simple simple

im tryin to write my first loop, very simple but cant get it to run. What is wrong?


#include<iostream>

using namespace std;

int main();

int i=0;
while (i<10);
{
system.out.println(i);
i++;
}
Your int main(); does nothing. You need to put it like this:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
{
     // rest of the code
}


Happy coding!

Edit: BTW, I've never heard of C++ function "system.out.println", I've only seen it in Java. Try using : cout << i << endl;
Last edited on
Also, your while loop shouldn't have a semicolon after it.
closed account (zb0S216C)
I'll clarify what your code is doing here.

kcomp11 wrote:
 
int main();

Here, you've created a prototype of main( ) but you never defined its body. You should receive an undefined reference to main( ) here.

kcomp11 wrote:
1
2
int i=0;
while (i<10);

Here, you've declared (and initialized, which is surprising) a single integer variable. You then create a while loop construct that does absolutely nothing. Instead, you make no time for the CPU, hence the excessive CPU usage (check Task Manager (if you're running Windows)). To fix this, simply remove the semi-colon after the test condition and place a set of braces ({...}) after the test condition. Then, within those braces, place i++. Your loop never increments i, so your loop will be going forever plus one.

kcomp11 wrote:
1
2
3
{
    // ...
}

This is a separate scope. Since you placed the semi-colon after the test condition of while, this scope is not a part of the while loop construct.

kcomp11 wrote:
 
system.out.println(i);
system is not a class or a namespace. Assuming this was a namespace, you would use the scope resolution operator (::), not the member access operator (.).

Wazzak
Last edited on
iostream isn't needed if you don't use cout or cin or something from that library.

This program enters a loop for roughly two seconds and then exits.
1
2
3
4
5
6
7
8
9
10
11
#include <ctime>

int main()
{
    while (clock() < 2000)
    {
     // empty loop
     }

     return 0;
}
Last edited on
system.out.println is used in Java; in C++ it is meaningless.
Use cout<<whatever
closed account (zb0S216C)
Cong wrote:
It's not compiling because main isn't returning a value even though you have said it returns a integer value. (sic)

By default, the compiler returns the value of 0 if the programmer hasn't explicitly specified a returning value. However, the compiler doesn't provide this for any other function. The program will fail to compile if any other function, other than main( ), fails to specify a return value (if not void). As for main( ), the compiler gives a warning regarding the missing return value, but it provides one anyway.

Cong wrote:
1
2
3
4
while (clock() < 2000)
    {
     // empty loop
     }
(sic)

While this is a possible solution, it's also a heavy one. Allow me to elaborate. Since the loop does nothing but count, the CPU has no time for any other threads. Using this solution causes excessive CPU usage on the users machine. A better alternative would be this:

1
2
3
4
5
int Wait( )
{
    std::cin.ignore( std::numeric_limits< std::streamsize >::max( ), '\n' );
    return 0;
}


Wazzak
Topic archived. No new replies allowed.