Display from 1 to 1000 in C++ without using a semicolon

Asked

Viewed 1,538 times

7

Make a program in C++ that displays on the screen the numbers from 1 until 1000, counting with these two, but without using the semicolon.

I already made a sketch here but I have to choose between appear the 1 or 1000.

To top it off, my code:

#include <iostream>
int main(int x = 0) {

  while (x <= 1000 && std::cout << x++ << std::endl) {}
 }

Why doesn’t he show the number 1? Guys, here, for example; https://ideone.com/4663hX, the output is correct. Could someone explain to me why g++ display from the 2?

  • 2

    Okay. This I read. And what do you try? Why without the semicolon? What’s the real problem? Is this some kind of codegolf or is it a homework assignment? Explain better.

  • It is a challenge for the staff. There is no real problem.

  • 1
  • I reopened the question to be consistent with the way the community handled another code golf question. See the discussion on the goal linked by Guilherme Bernal.

3 answers

6


If that’s the point, let’s go:

76 bytes

#include <cstdio>
int main(int a,char**){while(printf("%d ",a++)&&a<1001){}}

But if it is already in C, we can make it a little smaller (ignoring the warnings, of course):

41 bytes

main(a){while(printf("%d ",a++)&&a<11){}}
  • You can see why my code didn’t show 1 ?

  • @Zebradomal did not see it. And successive editions in less than 5 minutes do not enter the history.

  • I edited the question, look there.

  • @Zebradomal Your code works perfectly. It goes from 1 to 1000.

  • It won’t. I’m compiling on G++, it goes from 2 to 1000.

  • @ZebraDoMal http://coliru.stacked-crooked.com/a/22126005548133b0

  • William, why in the g++ part of the 2?

  • 1

    @Zebradomal x is the amount of arguments passed. If it is 1 argument your code starts showing from 1. But it seems that you are passing some additional argument to the executable. Configuration of the IDE maybe?

  • I write the codes in Gedit and compile in the standard Debian g++ .

  • @Zebradomal Open a question for this. This is already becoming a mess.

  • 1

    @Guilhermebernal So it is, and he opened it that one and now the mess has only gotten worse! hahaha :) The worst is that you had already answered (I also think he’s passing an extra parameter on the command line without realizing).

  • 2

    @Luizvieira I think now the thing ends http://answall.com/a/16516/429

Show 7 more comments

2

A possible solution:

#include <stdio.h>
#include <stdlib.h>

int main(int num=0,char**){
    if(printf("%d\t",++num) && (&main + (&exit - &main)*(num/1000))(num+1)){}
}

gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

0

#include <iostream>
#include <stdio.h>


using namespace std;

int main(int x = 0) {
    while (x <= 10) {
    cout << x++ << endl;
    }
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.