How to write a Loop Program

What a relief it was to have found this place...Maybe you guys can help me...
I simply never have had what it takes to be a programmer...and have now found
myself stuck in a course I have to take in order to graduate (Network design/Tech)

We have to write the following program...

Use For Loops to create a program that prints
out a structure that looks like this:


*
**
***
****
*****
****
***
**
*

So far all I have is this code...


# include <iostream>
using namespace std;

void main ()
{

int star;


for (int star = 1; star <=5; star = star + 1)
cout << " * " << endl;
}
Not sure where to go from here..

Thanks
Last edited on
closed account (zb0S216C)
This again? Try searching this site for the previous 50 exact post :)

Thank you.
Last edited on
So you don't know how a loop works?
Well there are for loops, while loops, do loops, and some other misc. methods to loop, like goto, but which are too less practiced to worth mentioning.
Basically, everything inside a loop repeats a couple of times, until some condition is met.

The for loop:
for(int i=0;i<10;i++){...};
- You declare a variable i and you set it to 0.
- You set the loop condition to i<10. This way, the statements inside the loop block will be repeated until i is no longer smaller than 10.
- You set the progression to i++ (or i+=1). This way, every time the loop ends, it increases 1 to variable i. This way, after 10 times, if the value of i is not modified from within the loop, the execution will end and the program will continue executing further statements.

The while loop:
1
2
3
4
5
int i=0;
while(i<10){
i++;
...
};

- Pretty much the same thing, except that this time you have more flexibility in declaring the loop condition and you have to update things yourself.

The do loop:
1
2
3
4
5
int i=0;
do{
i++;
...
}while(i<10);

- It's very similar to the while loop, except that this time the statements inside the loop will be executed at least once, in the case that the condition isn't met, unlike the other two, in which the loop will be skipped in a case like this:

for(int i=0;i<0;i++){...};

Got it?

Later edit: seems like the first time I've read this details weren't given...
Last edited on
here is a way to do it, how ever it does not print out EXACTLY what u put, however its an easy fix. please look through the code i made and try and teach your self what is happening and how it is doing it. it really is not that hard. when you approach a program try writing it out on paper first it makes things much easier.
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
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
    int i=0;
    
        while(i<10){
        i++;
        
        for(int j=0;j<i;j++){
            cout<<"*";
            }
            cout<<endl; 
            }
            while(i>0){
            i--;
            
             for(int x=i;x>0;x--){
             cout<<"*"; }
            cout<<endl;   
             }
              system("PAUSE");
                            }
Last edited on
oh and you dont need the system("PAUSE") thats only for the c++ compiler i use
sorthon, actually you never need the system pause if you just start the program from the terminal. Console programs should, in fact, not pause at the end of the program. I am aware that many people do this to keep the console from shutting down if you launch the program from explorer in windows (or other places in other OS'es), but you really shouldn't do that.
i use dev-C++ and so does my professor and when u try and run with out the pause it will close out your run window so idk of any other way to keep it from closing in a split second but as i have learned my professor is an idiot but yea thats the only reason it is there, because again my professor stated it needed to be there.
Well, even devC++ (an horribly outdated and buggy IDE btw) creates executables, and you can call those executables from a terminal. I always have a cmd.exe open when I program in windows (and a bash in Ubuntu).
Got this little gem from the book I'm learning from.

void entertoExit()
{
std::cout << "\nPress the Enter key to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
}




Right before return
entertoExit();

BAM!
Yes of course that does work, but:
1. Who actually runs console programs from desktop?
2. For people who run it from a shell it's just annoying cause you have to enter an additional button to actually quit the program. Console programs shouldn't show that kind of behaviour. Imagine if our compilers did that- yeah right, invoke make and hit 300 times enter to compile your program. Sounds like a good idea, no?
3. Less (sorry for bad words) shitty IDE's than DevC++ actually do prevent the console from closing all on their own if you call them from the IDE.
Last edited on
i agree with you im not making an argument haha just saying what i have been told
Thx for all the responses....I didn't expect to get so many responses this quickly.....Im going to try some of the suggestions out tonight and see what I come up with..

hanst99 (348) Mar 7, 2011 at 1:25pm
Well, even devC++ (an horribly outdated and buggy IDE btw) creates executables, and you can call those executables from a terminal. I always have a cmd.exe open when I program in windows (and a bash in Ubuntu).


my teacher also uses dev C++ so, i am too. what program do you use?
Down with the teachers' unions!

Seriously, though. Six years ago, Pentium IVs were still being used (just to demonstrate how far computers have advanced in that time). I'd suggest you upgrade, and maybe quietly suggest to your teacher that he/she shouldn't use an IDE that hasn't been updated in six years?

http://wxdsgn.sourceforge.net/

-Albatross
closed account (S6k9GNh0)
There are a horrible amount of reasons to not use Dev C++. Actually, it's become a rather annoying problem among beginners. Here is more information and suggestions for new IDE's. There's even a successor to Dev C++! Please, move on: http://cplusplus.com/forum/articles/36896/

Also, it's good that you're willing to teach others the C++ language! However, giving answers to these questions are looked down upon. Here for more reasons to not give answers away: http://cplusplus.com/forum/articles/31015/

void main ()
This is a nono. There is no such thing as a default entry point defined as void main(). It is deprecated and the *only* reason it *sometimes* works is simply because of backwards compatability. Instead, be sure and always use int main() or int main(int, char**). Moving on:

1
2
3
4
5
int star;


for (int star = 1; star <=5; star = star + 1)
cout << " * " << endl;


Please note that you only need one declaration of star. Here, you've declared it once before the for-loop and once inside of the for-loop. This will not compile because of conflicting identifiers (variable names). As for the logic of your program, this is actually more to do with logic rather than syntax. The for-loop is a very basic function in most languages and you can find plenty of information and documentation here: http://cplusplus.com/doc/tutorial/control/

Keep at it and eventually, this stuff will be second nature to ya. :D

Last edited on
no one is replying to my thread cause, so maybe some one can help me with my code on this problem too.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Assignment 4.2
//Triangle
#include <iostream>

    using namespace std;

    int main()
{ 

	const int NUM_ROWS = 6;          
	const int MAX_symbols = 80;	                  

	int baseNum;	
    int numSpaces;                    
	int	numsymbols;                   
    char symbol;
    
        cout <<"Enter a value to represent the base of a triangle shape" 
             <<"(not to exceed 80): "<<endl;
          cin>>baseNum;
if (baseNum<=0)
   {  
        cout<<"Please enter a positive integer "<<endl;
   }
else if (baseNum>=80)
  {
       cout<<"please enter a number less than 80."<<endl;
  }
else if(!baseNum%2==1)                         
  {
     baseNum=baseNum+1;
  }
else
        cout <<"Enter the character to be used to generate the triangle shape"
             <<" (for eg., #, * $): "<<endl;
          cin>>symbol;
          




for (int row = 0; row < NUM_ROWS; row++)//num of rows
{
		
		numsymbols = 2 * row - 1;     
        numSpaces = (baseNum - numsymbols) / 2; 

	
for (int space = 0; space <(numSpaces-row); space++)
		{
            	cout << ' ';
         }
           cout<<endl;
		
for (int plus = 0; plus < baseNum; plus++)
      {
for (int temp = 0; temp < plus-numsymbols; temp++)
      {
             cout << symbol;
      }
      }
      cout<<endl;
}

	system("PAUSE");	
    return 0;
}


im trying to get a triangle with space away from the side. the max amount of stars is 80. and it asks for the number of base stars to be entered not the number of rows. basically i do not know how to get my space on the side or how to make my base num actually be 11. it keeps increasing.
this is my output right now, which is liek the best ive gotten it to look so far lol

Enter a value to represent the base of a triangle shape(not to exceed 80):
11
Enter the character to be used to generate the triangle shape (for eg., #, * $):

*

******************************************************************

*********************************************

****************************

***************

******

*
Press any key to continue . . .
http://www.cplusplus.com/src/

Triangle.cpp


explains it pretty well
Topic archived. No new replies allowed.