hi all,
i am trying to generate a pattern

1
      121
    12321
   1234321
 123454321
  1234321
    12321
     121
       1

but the code is not compiling
i am getting the foolowing errors
C:\Dev-Cpp\files\patterns.cpp:102: error: name lookup of `j' changed for new ISO `for' scoping
C:\Dev-Cpp\files\patterns.cpp:98: error: using obsolete binding at `j'
C:\Dev-Cpp\files\patterns.cpp:115: error: name lookup of `p' changed for new ISO `for' scoping
C:\Dev-Cpp\files\patterns.cpp:112: error: using obsolete binding at `p'

now i have not tackled such errors before . pls help.
here is the code

for(int i=1;i<=5;i++)
                {
                        for(int s=4;s>=i;s++)
                        cout<<" ";
                        for(int j=1;j<=i;j++)
                        cout<<j;
                        if(i>=2)
                        {
                                for(int k=j-2;k>=1;k--)
                                cout<<k;
                        }
                        cout<<endl;
                }
                for(int m=1;m<5;m++)
                {
                        for(int sp=1;sp<=m;sp++)
                        cout<<" ";
                        
                        for(int p=1;p<5;p++)
                        cout<<p;
                        
                        for(int t=p-2;t>=1;t--)
                        cout<<t;
                        
                        cout<<endl;
                }

Dani AI

Generated

was on the right track — the compile warnings come from using loop variables outside their loop scope — and the actual logic bug is using cumulative counters (fr / tp) so the descending side uses the wrong start value. Fix both by computing the descending start per row (it should be one less than the current row width) and by keeping loop variables local to the row.

Algorithm (simple, reliable)

  • Pick n = maximum row (5 for your sample).
  • For rows i = 1..n: print (n-i) spaces, then numbers 1..i, then numbers (i-1)..1.
  • For rows i = n-1..1: do the same (that produces the lower half).
  • Do not accumulate counters across rows — compute the descending start from i.

A concise, corrected C++ example (drop nonstandard headers and fflush calls; use this skeleton in your case 4):

#include <iostream>
using namespace std;

int main() {
    int n = 5; // set to desired peak
    for (int i = 1; i <= n; ++i) {
        for (int s = 0; s < n - i; ++s) cout << ' ';
        for (int a = 1; a <= i; ++a) cout << a;
        for (int d = i - 1; d >= 1; --d) cout << d;
        cout << '\n';
    }
    for (int i = n - 1; i >= 1; --i) {
        for (int s = 0; s < n - i; ++s) cout << ' ';
        for (int a = 1; a <= i; ++a) cout << a;
        for (int d = i - 1; d >= 1; --d) cout << d;
        cout << '\n';
    }
    return 0;
}

Troubleshooting tips

  • Test with small n (3 or 4) and print marker lines to see spacing.
  • If your compiler warns about loop-scope, declare temporaries inside the for headers as above or declare them fresh where needed.
  • To convert numbers to *, replace printing a/d with cout << '*' (do that only after the numeric version works).

pointed out the scoping issue; , your fr/tp approach accumulates across rows — switch to per-row logic (as shown) and the pattern will print correctly.

Recommended Answers

All 7 Replies

According to new ISO standard, variables declared inside for loop are not accessible after its scope end...for example

for(int i=0;i<5;i++)
{
 cout<<i;
}// memory for i releases and won't be accessible after this...you need to declare i again to use it.

According to new ISO standard, variables declared inside for loop are not accessible after its scope end...for example

for(int i=0;i<5;i++)
{
 cout<<i;
}// memory for i releases and won't be accessible after this...you need to declare i again to use it.

thanks i got it and made suitable changes but there is a logical error in the prog can anyone point that out. it compiled well but i gotta prob making this pattern. actually i was making a big program having 4 patterns but its over an hour working on this pattern . the other three are working fine. i am including the code . check case 4.

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
char opt;
int main()
{   using namespace std;


    do
    {
    int n,fr=0,tp=0;   
    cout<<endl;
    cout<<"\n................MENU"<<endl;
    cout<<"Enter 1 for printing 1st pattern  "<<endl;
    cout<<"Enter 2 for printing 2nd pattern  "<<endl;
    cout<<"Enter 3 for printing 3rd pattern  "<<endl;
    cout<<"Enter 4 for printing 4th pattern  "<<endl;
    cout<<"Enter 0 for exit                  "<<endl;
    cout<<"Enter your choice ";
    fflush(stdin);
    cin>>opt;
    fflush(stdin);
    
    statement:
    if(isalpha(opt))
    {cout<<"You suck ";
    cout<<"Enter the correct option ";
    cin>>opt;
    fflush(stdin);
    goto statement;
    }
    fflush(stdin);
    
        
    switch(opt)
    {
    
      case '0':
              return 0;
              break;
      
      case '1':cout<<endl<<endl;
               cout<<"Enter the no. of lines to be printed ";
               cin>>n;
               fflush(stdin);
               for(int i=1;i<=n;i++)
               {
                       for(int j=1;j<=i;j++)
                       cout<<"*";
                       
                       cout<<endl;
               }
               
               
               break;
    
      case '2':cout<<endl<<endl; 
               cout<<"Enter the no. of lines to be printed ";
               cin>>n;
               fflush(stdin);
               for(int i=1;i<=n;i++)
               {
                       for(int j=n;j>=i;j--)
                       cout<<"*";
                       
                       cout<<endl;
               }
               
             break;
               
               
      case '3': cout<<endl<<endl; 
                cout<<"Enter the no. of lines to be printed ";
                cin>>n;
                fflush(stdin);
                
                
                                
                 for(int i=1;i<=n;i++)
                 {     
                       
                       for(int k=n-1;k>=i;k--)
                       cout<<" ";
                       
                       for(int j=1;j<=i;j++)
                       cout<<"*";
                       
                       cout<<endl;
                }
                 break;
      
      case '4': cout<<endl<<endl;
                for(int i=1;i<=5;i++)
                {
                        for(int s=4;s>=i;s++)
                        cout<<" ";
                        for(int j=1;j<=i;j++)
                        {fr++;
                        cout<<j;}
                        if(i>=2)
                        {
                                for(int k=fr-2;k>=1;k--)
                                cout<<k;
                        }
                        cout<<endl;
                }
                for(int m=1;m<5;m++)
                {
                        for(int sp=1;sp<=m;sp++)
                        cout<<" ";
                        
                        for(int p=1;p<5;p++)
                        {
                                cout<<p;
                                tp++;
                        }
                        
                        for(int t=tp-2;t>=1;t--)
                        cout<<t;
                        
                        cout<<endl;
                }
                
                break;
      default:  cout<<endl<<endl;
                cout<<"Wrong choice ";
               
    }
    
  
 }while(opt!=0);
  return 0;
}

There are lot of issues with ur code...

#include<conio.h>
#include<stdio.h>
#include<ctype.h>

don't mix c-headers with c++ headers....<conio.h> is non-standard...use<cctype> instead of<ctype.h>.

fflush(stdin)

is wrong...fflush() is not meant to clear input stream...
see the change

case '4': cout<<endl<<endl;
                for(int i=1;i<=5;i++)
                {
                        for(int s=4;s>=i;s--)

I Am Getting Funny Results (in Case 4 Only )
I Was Using Nums To Make It A Bit Easier
Finally I Have To Convert It Into * As In The Other Segments .
Its A Tough One Any One Plz Help
Try Compiling N Editing.
By The Way Sunny You Are Cool .
What To Use In Place Of Conio.h
I Dont Get It Why We Are Taught These Obselete Stuff At School .
These People Dont Take Care Of Iso Standards . Cbse Needs Reforms.
My Oxford (course Book Is Using Ctype.h) . Recommend Some Book Following New Standards.

You Know I Once Told My Prof That Conio.h Is Non Standard . He Has Taught Us That Conio.h Has The Clrscr(); ,, While I Was Using It On Dev-cpp It Didnt Work , I Checked The Help Website , It Said That They Have Included A Reduced Version Not Including Clrscr();,
I Told Even This To My Prof . He Said That If U R Asked That Clrscr() Belongs To Which Header Then U Have To Write Conio.h . He Said That It Is Cbse Exam That We Are Preparing U For. We Dont Want U To Be Professionals.

Tell Me How To Copy Output In Dev Cpp
I Know How To Do It In Turbo But Not In Dev Cpp

I can understand ur problem...you'll find such teachers everywhere...you won't believe there are certain things which i have to use in my code to satisfy them...though they are completely wrong concepts....but they are stupid teachers...in my course they don't show us the papers to cross question them....so i need to write what they feel is right and so that my scores are right...but there is no problem if you know what is right and what is wrong...use them in ur professional life...
Why do u need to use <conio.h> anyways in ur code....it is meant for borland compilers...if u want to clear the screen use

system("cls");

though it non-portable code....works fine if u r working on windows...but i don't think you have any good reason to clear the screen.

hey i never suceeded in making dat pattern . check if it has a logical error.

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.