LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to make a grid for fun, what am I doing wrong? (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-make-a-grid-for-fun-what-am-i-doing-wrong-4175431201/)

carlosk711 10-08-2012 03:29 PM

Trying to make a grid for fun, what am I doing wrong?
 
Code:

#include <iostream>
using namespace std;
int main()
{

    int row, column;
    row == 0;
    column == 0;
    {if (((row % 2) == 0)
    (while ((row <= 9)
    {while ((column <= 17);
    cout << "-";
    column ++)}));
    row ++ ;
    cout << endl) ;

    else if ((row <= 9)
    while ((column <= 17)
    {cout << "|  " ;
    column = column + 4));
    row ++;}
    return 0;
}}

I'm trying to make it look like this:
-----------------
| | | | |
-----------------
| | | | |
-----------------
| | | | |
-----------------
| | | | |
-----------------

I want to do it using while and for loops too.
Help is much appreciated!!

Here are errors I'm getting
Code:

10: error: expected primary-expression before âwhileâ
13: error: â((((unsigned int)row) & 1u) == 0u)â cannot be used as a function
13: error: expected `)' before â;â token
15: error: expected `;' before â)â token
17: error: expected primary-expression before âelseâ
17: error: expected `;' before âelseâ

EDIT:
For some reason its not coming out as I want it to look on here, its;
17 "-"
5 "|"
9 rows
17 columns

suicidaleggroll 10-08-2012 03:42 PM

You really need to work on the formatting/indenting, that code is almost unreadable. It's also full of unbalanced parenthesis...at least I think, I can't tell because of the formatting. You have an else if inside a while, etc.

carlosk711 10-08-2012 03:47 PM

sorry here

Code:

#include <iostream>
using namespace std;
int main()
{

    int row, column;
    row == 0;
    column == 0;
    {if (((row % 2) == 0)

        (while ((row <= 9)
           
            {while ((column <= 17);
            cout << "-";
            column ++)}));
            row ++ ;

    cout << endl) ;

    else if ((row <= 9)
   
        while ((column <= 17)
        {cout << "|  " ;
        column = column + 4));

    row ++;}

    return 0;
}}

I just tried to condense it, hopefully this is better

suicidaleggroll 10-08-2012 03:53 PM

What about this? I completely rewrote the code, I couldn't really figure out what you were trying to do in the example.

Code:

#include <iostream>
using namespace std;

int main()
{
  int row, column;
  for (row=0;row<9;row++)
  {
      for (column=0;column<16;column++)
      {
        if ((row % 2) == 0)
        {
            cout << "-";
        } else {
            if ((column % 5) == 0)
            {
              cout << "|";
            } else {
              cout << " ";
            }
        }
      } 
      cout << endl;
  }
  return 0;
}


carlosk711 10-08-2012 04:14 PM

I like the idea of the for loop, why does it seem easier with the for instead of the while??

carlosk711 10-08-2012 04:17 PM

ok when i did yours I got,

----------------| | | | ----------------| | | | ----------------| | | | ----------------| | | | ----------------

I there should be an "endl" after each one where should I put them?

suicidaleggroll 10-08-2012 04:41 PM

Did you miss the "cout << endl" line in the code? It looks fine on mine:
Code:

$ g++ test.c
$ ./a.out
----------------
|    |    |    |
----------------
|    |    |    |
----------------
|    |    |    |
----------------
|    |    |    |
----------------



All times are GMT -5. The time now is 06:01 PM.