Simple For Loop Program

Sep 10, 2014 at 7:01pm
Hi, I wonder if anyone can answer a simple question.

Is it possible to write a program where for example: You write a simple "Hello World" program with a twist. The user can input an integer and "Hello World" is displayed as many times as the value entered?

This is without having to write: for(n==1) cout << "Hello World" << endl; around a million times as you are only covering one number at a time.

I hope this is clear for anyone to understand the question and appreciate all responses.
Sep 10, 2014 at 7:06pm
Review the structure of a for loop:
http://www.cplusplus.com/doc/tutorial/control/#for
Sep 10, 2014 at 7:10pm
You mean something like:
1
2
3
4
5
cout<<"Enter number of times to echo: ";
cin>>x;   //x is unsigned

for(int i=0;i<x;++i)
     cout<<"Hello World";


Where "Hello World" is displayed x number of times??? Otherwise explain.

Aceix.
Sep 10, 2014 at 7:14pm
@Aceix: if x is unsigned then why is i signed?
Sep 10, 2014 at 7:35pm
Thanks for both answers.

I've looked over the structure of a for loop. The only part I am struggling with is coming up with a shorter way of writing the program without having to create 1000's of lines the same (only changing the number that is equal to that entered by the user).

It has to be a for loop as that is what has been requested in a tutorial I am doing.

I think you're on the right line Aceix. It is literally something that says: "Enter a number: "
Then the output is "Hello World" x number entered.
Sep 10, 2014 at 8:03pm
Not only is he on the right track, but he actually gave you the full answer. That is actual working code.

Demo:
http://ideone.com/CQP7w6
Last edited on Sep 10, 2014 at 8:03pm
Sep 11, 2014 at 9:21am
That's fantastic. Much appreciated.

I know it works 100%. Just one question and it's just to do with my understanding. In the loop the:

for(int i=0;i<x;++i)

How does it read the value and know to output "Hello World" that many times? When I read this, I would assume that if the value "i" is less than "x" then increment "i" and output "Hello, World" once only.

Thanks and here is my working code thanks to your help/writing it for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
	unsigned n = 0;
	cout << "How many times will I output the sentence?";
	cin >> n;
	{
		for(unsigned i=0; i<n; ++i)
		cout << "Hello, World!" << endl;
	}
	return 0;
}
Sep 11, 2014 at 11:54am
Scratch that last question. It is understood 100% now.
I have also wrote similar codes for "while" & "do" loops which are posted below for comment negative or otherwise.

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

int main()
{
	unsigned n;
	unsigned i=1;
	cout << "How many times will I output the sentence?";
	cin >> n;
	while (i <= n)
	{
		cout << "Hello, World!" << endl;
		i++ ;
	}
	return 0;
}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	unsigned n, i = 1;
	cout << "Tell me how many times to output Hello World: " ;
	cin >> n ;
	
	do
	{
		cout << "Hello, World!" << endl ;
		i++;
	}
	while( i <= n );
	
	return 0;
}


Thanks for the info.
Sep 11, 2014 at 12:04pm
You need to know that "do + while" vs "while" are different.

"do + while loop" : the program is doing something into "do" and then it will check the condition into "while"

while : the program is checking the condition before going into the loop to do something.

That's is a big different and You need to learn clear about C++.
Sep 11, 2014 at 3:13pm
I am aware of that @luckybc - Thanks
Topic archived. No new replies allowed.