confused

Jun 7, 2012 at 11:20am
closed account (G28URXSz)
how to write a C++ program to print out hello world ten times using a for loop
Jun 7, 2012 at 11:23am
You can do it like this:

for (int i = 0; i < 10; i++){
cout << "hello world" << endl;
}
Last edited on Jun 7, 2012 at 11:23am
Jun 7, 2012 at 3:53pm
Impress your professor! Count down from 10 to zero!

1
2
3
4
for(int i = 10;i > 0; i--)
{
   cout << "Hello World!" << endl;
}
Jun 7, 2012 at 5:42pm
Impress your professor and do it in an even sillier way!
1
2
3
4
5
int n = 0;
while (n < 10) {
cout << "Hello World!" << endl;
n++;
}


This way shows more concepts. Although it's useless since for loops work better.
Jun 7, 2012 at 6:30pm
Or don't impress your professor and get sillier still!

1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0; i < 1; i++){
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
cout << "hello world" << endl;
}
Last edited on Jun 7, 2012 at 6:30pm
Jun 7, 2012 at 6:37pm
I recommend you do it this way, actually..
Except your professor may kick you out of the class.

1
2
3
4
5
6
7
8
9
10
bool tenTimes = true;
int count = 1;
do {
    cout << "Hello World!";
    if (count == 10) {
        tenTimes = true;
    }
    count++;
}while (tenTimes == false);
Last edited on Jun 7, 2012 at 6:39pm
Jun 7, 2012 at 7:14pm
closed account (G28URXSz)
wait which is the best way?
Jun 7, 2012 at 7:29pm
georgette, I'd suggest you read up on basic C++ control structures, of which a for loop is one. http://www.cplusplus.com/doc/tutorial/control/
Jun 7, 2012 at 8:59pm
We're just messing around since this is obviously your homework and we're not just going to give you the answer. It's very simple though and you have more than enough hints given to you. Look at the tutorial linked by twistermonkey and try to put all the pieces together. Then try to compile and run it. If it works, you're done. There is really no best way to print the same thing ten times. It's too trivial to optimize.
Jun 7, 2012 at 9:13pm

@georgette
wait which is the best way?


In my opinion the best variant is the following because 1) it has been written by me; 2) it demonstrates two uses of for statement:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
   char s[] = "Hello World";
   const int N = 10;

   for ( int i = 0; i < N; i++ )
   {
      for ( char *p = s; *p; ++p ) std::cout << *p;
      std::cout << std::endl;
   }

   return 0;
}
Jun 7, 2012 at 9:20pm
Why loops if C++ has perfectly good fill_n?

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
    fill_n(ostream_iterator<string>(cout, "\n"), 10, string("Hello, world"));
}

online demo: http://ideone.com/Fz8wl
Jun 7, 2012 at 9:27pm
@Cubbi,

Your variant has a big disadvantage! Too many headers!:)
Jun 7, 2012 at 9:58pm
Look ma, no loops!
1
2
3
4
5
6
7
#include <iostream>

int main(int _, char *__[])
{
    _++<0xA?0x0>=!main(_,__):std::cout<<std::endl;
    std::cout << "Hello, World!\n";
}


http://ideone.com/9hH7S

Edit: I keep finding things to tweak.
Last edited on Jun 7, 2012 at 10:08pm
Jun 7, 2012 at 10:11pm
@Catfish2
$5.2.2[expr.call]/9 says "Recursive calls are permitted, except to the function named main"
Fun idea, though.
Last edited on Jun 7, 2012 at 10:12pm
Jun 7, 2012 at 10:13pm
@Catfish2

In C++ the main may not call itself. It is suppressed by the C++ standard!
Jun 7, 2012 at 11:24pm
Thanks killjoys, it has no soul anymore!
But at least it's still convoluted and hard to understand.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <functional>
#include <iostream>

int main()
{
    std::function<int (int)> hello = [&](int i)->int {
        if (--i != 0) hello(i);
        std::cout << "Hello, World!" << std::endl;
        return 0;
    };

    return hello(10);
}


http://ideone.com/OkPMR
Last edited on Jun 7, 2012 at 11:26pm
Topic archived. No new replies allowed.