printing out message n times!


I need to modify the following program so that after a correct n has been input, it prints out n times a message such as "Hello".

heres my code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include<iostream>
using namespace std;

int main()

{

int n;

do {

cout << "Please input a number:";
cin >> n;

if(n>=1 && n>=10) {

cout << "You have input n";
exit(1);

                   }

else {

std::cout << "\7";
cout << "Not in range, please input another number:";
cin >> n;

     }

while(n>=1 && n>=10)

return 0;

}


Think step by step. This is an easy problem.

First, make a loop that repeats until a valid number is entered by the user.

Then, independent of the first loop, make a second loop that counts from 1 to N inclusive and outputs a message each time through the loop.

Firstly, I think you've got some of your assignment operators mixed. On line 16 and 31 it says:
(n>=1 && n>=10) which basically says "if n is bigger than or equal to 10" - completely ignoring n>=1. I assume this is meant to be less than 10?

Secondly, I haven't been programming for too long myself but after a quick search it looks like the exit(1) function is used for closing file streams. although i'm still not sure why it's in there.

Thirdly, std:: ... only needs to be used when you dont have the line using namespace std;.

Try re-writing your program and see if you can get it to work. Bare in mind that you will need to use a loop to output "Hello" n amount of times.
yeah its suppose to be n<=10.


jsmith:

Then, independent of the first loop, make a second loop that counts from 1 to N inclusive and outputs a message each time through the loop.


thats what I dont know how to do



jsmith could you post an example of your solution please?
1
2
3
int sum = 0;
for( int i = 1; i <= 10; ++i )
    sum = sum + i;


adds up the numbers 1 through 10 and stores the result in sum.

I can't really go any simpler than that without just writing it for you.

cheers
Topic archived. No new replies allowed.