confused

closed account (G28URXSz)
how to write a C++ program to print out hello world ten times using a for loop
You can do it like this:

for (int i = 0; i < 10; i++){
cout << "hello world" << endl;
}
Last edited on
Impress your professor! Count down from 10 to zero!

1
2
3
4
for(int i = 10;i > 0; i--)
{
   cout << "Hello World!" << endl;
}
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.
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
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
closed account (G28URXSz)
wait which is the best way?
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/
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.

@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;
}
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
@Cubbi,

Your variant has a big disadvantage! Too many headers!:)
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
@Catfish2
$5.2.2[expr.call]/9 says "Recursive calls are permitted, except to the function named main"
Fun idea, though.
Last edited on
@Catfish2

In C++ the main may not call itself. It is suppressed by the C++ standard!
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
Topic archived. No new replies allowed.