Hi everybody,
I bought the book "Deitel C++ How to Program, edition 5", and i am doing the exercises of the chapter 4, the exercise 4.28 ask you to write a program that print the following figure:
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
You must use only three output streams:
cout << "* ";
cout << " ";
cout << endl;
I wrote the program, but i think i did it by a hard way.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int counter = 8; // Contador para o loop
int hcounter = 1; // Contador para linhas horizontais
int test = 1; // Variavel para controle da linha 4, onde o contador tem que ser alterado
while ( counter > 1 ) // Enquanto contador maior que 1
{
if ( counter == 5 && test == 1 )
{
counter = 6;
test = 0;
}
if ( counter % 2 != 0 )
cout << " ";
while ( hcounter <= 7 )
{
cout << "* ";
hcounter++;
}
cout << endl;
hcounter = 1;
if ( counter == 6 && test == 0 )
counter = 5;
else
counter--;
}
return 0;
}
This code works, but i think i am having difficult with the programming logic, what can you recommend me?
Thanks and sorry the bad english.