Alright, i just learned the loops (if then else, for, do while, ect.) and just before it i learned the input and output (cin, cout). Now the tutorial i am using, has no homework built in it, so if anyone would be so kind to tell me something, ill probally never use, based on the loops and what else i know, a quick project i could do to use and figure out the loops, as it is i barely understand them.

Recommended Answers

All 40 Replies

Using loops, draw the following shapes:

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

?Question?
Do i have to us * or can i use any symbol to fill in the *

>Do i have to us * or can i use any symbol to fill in the *
It doesn't matter as long as the shapes are printed correctly.

Ok, thankyou Narue, ill try and get it done ASAP, got some other stuff around the house to do.

Theres so many different loops i cant figure out wich one to use, im thinking of using the "For" loop, and using the "Break" and "Continue" loop commands to sip lines and what not, but i dont know if it will work, and don't wanna waste 30 minutes writing a program that won't work from start ;P

>but i dont know if it will work, and don't wanna waste 30 minutes writing a program that won't work from start ;P
Get over it, programming is all about failure. You screw up syntax and the compiler bitches at you, you screw up semantics and the compiler bitches at you, you screw up the logic and nothing works right. It goes with the territory, so get used to it. The best way to learn how a language feature works is to get it wrong, over and over, until you finally get it right.

Aight, ill just try to see if it works, pertty sure thats how i do it to.

//Project using loops
#include <iostream.h>

int main ()
{
    for (int n=5; n>5; n--) {
        cout << n;
    }
    cin.get ();
    return 0;
}

ok doing this just to get 54321 on the first line, but it wont show it....

>but it wont show it....
You initialize n to 5 with the intention of decrementing it, yet your condition says to loop only when n is greater than 5. The loop never executes because the condition fails immediately. Perhaps you wanted the condition to be n > 0?

NVM, re read it, set it up wrong on the line of
for (int n=5; n>5; n--) {
sposed to be
for (int n=5; n>0; n--) {

But, now comes the problem, how to instead of:

// countdown using a for loop
#include <iostream.h>
int main ()
{
  for (int n=5; n>0; n--) {
    cout << n;
    cout << n;
    cout << n;
    cout << n;
    cout << n;
  }
  cin.get();
  return 0;
}

Be able to loop the cout << n; inside of the for loop, i dont know how to ;P, just gimme the loop name, that imasposed to use, and ill figure out how to use the loop.

Ok, im now officialy lost in the code

// countdown using a for loop
#include <iostream.h>
int main ()
{
    for (int n=5; n>0; n--) {
        cout << n;
    }    
    for (int n=5; n>0; n--) {
        if (n==4) continue;
        if (n==3) continue;
        if (n==2) continue;
        cout << n;
    }
    cin.get ();
    return 0;
}

this all shows up as "5432151" but i need it all to show up as:
54321
5 1
5 1
5 1
54321
but i dont know how to get the loops to do this...

>just gimme the loop name, that imasposed to use, and ill figure out how to use the loop.
You can do these problems with any of the looping constructs in C++.

>im now officialy lost in the code
Forget about the code. Logic comes first, then practice. If you have no idea how to solve the problem then there's no way you can tell the compiler how to solve the problem.

>but i dont know how to get the loops to do this...
Start by drawing the filled box:

54321
54321
54321
54321
54321

Use a loop inside of a loop, that's the most obvious way of doing it:

# This is not C++
for m := 1 to 5 do
  for n := 5 to 1 do
    print n
  loop
loop

With that under your belt you can then work out how to draw the empty box:

54321
5   1
5   1
5   1
54321

Hint: Only print when m and n are at the edges of the box.

Thx, that got me outs the spot, it just hit me as i read that part, you gotta do a loop inside of a loop, lol

One other thing, is it possible for me to do it ALL inside ONE file?

>One other thing, is it possible for me to do it ALL inside ONE file?
All of what? The answer is likely going to be yes, but I still would like clarification. :)

All of the drawings, put them all in one file, and run it, and they all show up.

Oh, got it all compiled, but i ran into a spot of trouble, i dont know how to get the first loop "m" to endl; every time..

Here, I'll do the first for you so that you can see how it might be done. You've actually chosen a more difficult task than I gave you for the later drawings because the character being printed changes:

#include <iostream>

using std::cout;
using std::endl;

int main()
{
  cout<<"Full box:\n";
  for ( int m = 0; m < 5; m++ ) {
    for ( int n = 5; n > 0; n-- )
      cout<< n;
    cout<<endl;
  }
}

Sometimes even the good proggramers mess up, ok i tried it and it doesnt have cin.get (); and return 0; in it, so it just opens and closes, not knowing how to do it i just added em, but your using std::cout; using std::endl; makes it undeclared and it wont alow it...

>Sometimes even the good proggramers mess up
How true. Not this time though. :)

>i tried it and it doesnt have cin.get ();
Not everyone uses an IDE to develop their programs. There's no need for that extra pause at the end when you're running the program from a command line. Feel free to add it if you need to, but my programs typically won't include it.

>and return 0; in it
If you use a standard compiler then 0 is returned automatically from main when the program terminates. Some programmers like to return a value explicitly, others don't. But both are correct.

>but your using std::cout; using std::endl; makes it undeclared and it wont alow it...
The problem is on your end, I'm afraid. My code conforms to the C++ standard, so it should compile on your compiler (which is Dev-C++ if I recall correctly). I would imagine that you changed <iostream> to <iostream.h> and that--understandably--broke everything.

I asked one of my friends, hes been programming in C+ for over 20 years, says your supposed to use <isostream.h> and on my side, i guess i have to do the code slightly different, but your part ata help me get it done ;)

GOT IT, i used your code, to get cin.get () to work, i did using std::cin; and then added cin.get ();!!! IT WORKS, thankyou, i will ry to do the others now;)

OOOO, forgot, you never answered the question, can i do all of the drawings in one file? or am i going to have to do it in seperate files?

EEEK

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
    cout<<"Empty box:\n";
    for (int a=5; a>0; a--) {
        cout << a;
        cout << endl;
    }
    for (int b=5; b>0; b--) {
        if (b==4, b==3, b==2) continue
        cout << b;
        cout << endl;
    }
    for (int c=5; c>0; c--) {
        cout << c;
        cout << endl;
    }    
    cin.get ();
}

It says that cout << b; has an error...Whats wrong with the code? lol

>I asked one of my friends, hes been programming in C+ for over 20 years, says your supposed to use <isostream.h>
He's wrong. Yes, I'm very sure, and I would be happy to tell him personally and in exact detail how and why he's wrong.

>can i do all of the drawings in one file? or am i going to have to do it in seperate files?
Yes, you can do them all in one program. Just be sure to print an empty line between them so that they can be easily seen.

>if (b==4, b==3, b==2) continue
You forgot a semicolon after continue.

Thx, and its that iostream.h works, just not properly ;P

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
    cout << "Empty box:\n";
    for (int a=5; a>0; a--) {
        cout << a;
        cout << endl;
    }
    for (int b=5; b>0; b--) {
        if (b==4, b==3, b==2) continue;
        cout << b;
        cout << endl;
    }
    for (int c=5; c>0; c--) {
        cout << c;
        cout << endl;
    }    
    cin.get ();
}

It came out as:
5
4
3
2
1
5
4
3
2
1
5
4
3
2
1
No were NEAR what i had planned, something in the endl;'s i think.

fixed most of the error's, heres the first two ,almost perfect combind, just focus on the second one tho.

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
  cout<<"Full box:\n";
  for ( int m = 0; m < 5; m++ ) {
    for ( int n = 5; n > 0; n-- )
      cout<< n;
    cout<<endl;
  }
  cout << "******" << endl;
  cout << "Empty box:\n";
  for (int a=5; a>0; a--) {
      cout << a;
  }
  cout << "\n";
  for (int c=0; c<3; c++) {
      for (int b=5; b>0; b--) {
          if (b==4) continue;
          if (b==3) continue;
          if (b==2) continue;
          cout << b;
      }
      cout << "\n";
  }
  for (int a=5; a>0; a--) {
      cout << a;
  }
  cin.get ();
}

it shows up as:

Full box:
54321
54321
54321
54321
54321
******
Empty box:
54321
51
51
51
54321
gotta get the 3 spaces inbewtween 5 and 1, and not before them.

Disregard those post's, im working on the:
1
12
123
1234
12345
Right now ;P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.