Thread: "I'm stuck"

  1. #1
    Unregistered
    Guest

    Question "I'm stuck"

    Can someone help shed some light on what I'm doing wrong? I'm trying to create the following diamond which the size is determined by the user's input:

    *
    ***
    ******
    ***
    *
    When I try to compile my program, I get the error parse error at the end of input. Here's my code so far:
    #include <iostream>
    #include <stdlib.h>
    #include <iomanip>
    int main()
    {


    int size;
    int i;
    int j;
    int k;
    cout << "Enter size: 1-80" << endl;
    cin >> size;

    if (size > 0 && size < 80)
    {

    for (int i = 0; i < size; i++)
    {
    int j = i++;
    for (; k!= 0; k--)
    {
    cout << '*';
    }
    cout << endl;


    for (i = size; i > 0; i--)
    {
    int j= --i;
    for (; k!= 0; i--)
    {
    cout << '*';
    }
    cout << endl;
    }

    for (int k = 1; k <= 5; k++)
    {
    cout << setw(k) << ' ';
    for (int j = k; j <= 5; j++)

    cout << "*";
    cout << endl;



    system("PAUSE");
    return 0;
    }

    I would really appreciate the assist!

    Thanx

  2. #2
    Unregistered
    Guest
    After I posted this question, I noticed the right half of my diamond example was cut off! I was trying to display a full diamond

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Your '{' is not matching up and setw[] is not declared. You try to access the array setw but you are using ()..Or is setw a function? You are using i and j in your for loops in a weird way. not sure what you try to do
    Last edited by Barjor; 05-08-2002 at 09:55 AM.

  4. #4
    Unregistered
    Guest
    //This code run perfect!!!
    //HAVE FUN!!
    // [email protected]

    #include<iostream.h>
    #include<stdlib.h>
    #include<iomanip.h>

    int main()
    {
    int size;
    int i,j,k,m;

    cout << "Enter size: 1-80: ";
    cin >> size;

    if(size%2 == 0) // if 'size' is even
    m=0;
    else // if 'size' is odd
    m=1;


    if(size > 0 && size < 80)
    {
    for(i=1; i<=size; i=i+2)
    {
    for(j=1;j<=i;j++)
    cout<<"*";
    cout<<endl;
    }
    for(k=size; k>=0;k=k-2)
    {
    for(j=m;j<=k;j++)
    cout<<"*";
    cout<<endl;
    }
    }

    return 0;
    }

  5. #5
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Code:
    int j= --i;
    Why would you do something like that? Why not just do
    Code:
    i--; int j = i;
    This way is MUCH easier to read and it's easier to catch an error like this. When the code is compiled, both of the statements (I think) end up the same.

  6. #6
    Unregistered
    Guest
    Thanks alot. Man, having another set of eyes really helps. I've gotten rid of the errors, but now when I run my program, I'm only getting the right side of the diamond. My end result has to have both right and left sides. Thanks again. :^ )

  7. #7
    Unregistered
    Guest
    Think of the diamond display as a multidimensional array. In the example below I substituted 0 for spaces to make it clearer.

    000*000
    00***00
    0*****0
    *******
    0*****0
    00***00
    000*000

    You can see that you need to know not only how many * per line to use, but how many 0s (or spaces if you prefer) to use on each line, in order to get the diamond display, rather than a triangle display. Whether you use spaces after the *s on each line or not is up to you. When you view the result you won't be able to tell.

    If you look at the above and maybe a couple other examples, you will be able to see a pattern as to how many 0s(spaces) you need per line, and where.

  8. #8
    Unregistered
    Guest
    Here's some code that works:

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
      int nrows=0, even=0, width, ss;
      while(nrows<1 || nrows>80)
      {
        cout<<"Enter a number between 1 and 80: ";
        cin>>nrows;
      }
      
      if(nrows%2==0)even=1;
    
      if(even)width=nrows-1;
      else width=nrows;
      if(even)ss=(nrows/2)-1;
      else ss=nrows/2;
    
      for(int y=0;y<nrows;y++)
      {
        for(int u=0;u<ss;u++)
        {
          cout<<" ";
        }
        for(int v=0;v<width-(ss*2);v++)
        {
          cout<<"*";
        }
        if(even)
        {
          if(y>(nrows/2)-1)ss++;
          else if(y==(nrows/2)-1)ss=0;
          else ss--;
        }
        else
        {
          if(y>=nrows/2)ss++;
          else ss--;
        }
        cout<<"\n";
      }
    }

  9. #9
    Unregistered
    Guest

    oops

    err, excuse the #include <conio.h>, I was gonna add a getch() in there but forgot

  10. #10
    Unregistered
    Guest
    Thanks for everyone's insight! It really helped!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "I'm go-ing craaazy..." (fin input problems
    By danneth in forum C Programming
    Replies: 4
    Last Post: 10-06-2003, 02:32 AM