Page 1 of 1 [ 4 posts ] 

LunarOfficial
Hummingbird
Hummingbird

User avatar

Joined: 25 Apr 2014
Gender: Male
Posts: 18

30 May 2014, 3:50 am

Code:
#include <iostream>
#include <string>

using namespace std;

// Usualy if I compiled this it would not work because "displayedIntegers" is not called "main" for the main argument.
int displayedIntegers ( int x )
{
    x == 1;
   
    while ( x < 100 )
    {
        cout << ++x;
        if ( x == 100 )
        {
            break;
        }
    }
}


Tada!

Take Note!
I did not mean to post this. I was looking at another post and "replied" this but I must have clicked "New post" instead of "Post Reply".


_________________
Goal :

This summer of 2014 I will learn over 14 programming languages, because I can. I know I can. No one say I can't because I can.


Last edited by LunarOfficial on 31 May 2014, 11:05 pm, edited 4 times in total.

MrElectron
Emu Egg
Emu Egg

User avatar

Joined: 23 Mar 2013
Age: 33
Gender: Male
Posts: 2

30 May 2014, 8:40 am

problems:
At first i thought:
While (x>100)
X++; //will not start due to x=1;
//and if it did, it would loop infinitely

X==1; //should be x=1;

The former is a comparison, the latter is an assignment.

Then I realized that you were leaving
code that did nothing uncommented
While this is a good means of obfuscation of code
It is not by any means good code.
I hope that clears things up.

Good luck with learning to program.
And compile before posting, be sure
To run from the cmd line because it will
Allow you to stop infinite looping programs
like the one in the code here by pressing Ctrl+c.



Kurgan
Veteran
Veteran

User avatar

Joined: 6 Apr 2012
Age: 37
Gender: Male
Posts: 4,132
Location: Scandinavia

30 May 2014, 8:54 am

Why not use a for loop instead? It serves the same purpose as a while loop that breaks when an incremented value reaches a certain number--and the code looks a lot more neat and clean with it.


_________________
“He who controls the spice controls the universe.”


drh1138
Velociraptor
Velociraptor

User avatar

Joined: 2 Dec 2012
Gender: Male
Posts: 498

30 May 2014, 11:00 am

This only barely works (ignoring the lack of an entry point) because x is uninitialized. '==' is equality comparison, not assignment. The break clause is completely unnecessary as well, since it is at the end of the code block, and only gets executed when the loop would have ended in the first place.

Something cleaner:

Code:
#include <iostream>

int main (int argc, char* argv[]) {
  for( int i = 0; i < 100; ++i ) {
    std::cout << i << std::endl;
  }

  return 0;
}


With some more fidgeting, you could supply via command-line argument a beginning and limit value.

Good luck with the learning process.