/* I need some advice on program bellow,this program is text program
in this program by pressing arrow key you can change your position
in the text and by enter key you can break a line and move the rest of the
line to a new line but this program dosen`t work properly.*/

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void pos(char);
void text();
int i, j,k,x=0,y=0,s=0;
char txt[41][79],st[79],ch;
int main()
{
clrscr();
for(j=0;j<41;j++)
 for(i=0;i<79;)
 txt[j][i]=32;

text();
return 0;
}
//******************************************((functions))********************************
void text(){

for(j=0;j<41;j++){
 for(i=0;i<79;){
 ch=getch();
 if(isalpha(ch) || isdigit(ch)|| ch==32){
x++;
i++;
 txt[j][i]=ch;
 putch(ch);
 }else
 if (ch==13||ch==0){
 pos(ch);
 }else
 if (ch=='=')
 goto last;
 }//end of for
 y++;
 }//end of outer for
 last:
}
//****************************
void pos(char ch){
if(ch==13){

for( int k=i;k<=76;k++,s++)
st[s]=txt[j][k];
y++;
x=0;
printf("\n");
puts(st);
for( int t=j;t<41;t++){
 for(int i=0;i<=78;i++){
putch(txt[t][i]);
}
printf("\n");
}
}//end of enter

 if(ch==0){
    ch=getch();

     if(ch==72){
       y=y-1;
       gotoxy(x,y);
       }
    else if(ch==80){
       y=y+1;
       gotoxy(x, y);
       }
    else if(ch==75){
       x=x-1;
       gotoxy(x,y);
       }
    else if(ch==77){
       x=x+1;
       gotoxy(x,y);     
       }
 }
 }
//***********************************

What are you forgetting here?

for(j=0;j<41;j++)
 for(i=0;i<79;)
 txt[j][i]=32;

Hint: it's an infinite loop . . . .

[edit] And it's a lot easier to use character constants like ' ' and '\r' in your code instead of magic numbers like 32 and 13, don't you think? [/edit]

Well, I should say that, I don’t even understand on what your are trying to do here. You will have to give some more information. And your code i just such a lot of mess. You are using stuff which you shouldn't be. Few which i pointed out.

1. Using a very old compiler
2. for(i=0;i<79;) --> why are you not incr i ( include i++)
3. goto <-- dont you know that these statement shouldn't be used. Its a bad programming practice.
4. gotoxy is not a standard C function
5. and clrscr getch !
6. Code indentation is horrible. But I thought this time I will do it for you

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

void pos(char);
void text();

int i, j,k,x=0,y=0,s=0;
char txt[41][79],st[79],ch;

int main()
{
    clrscr();
    
    for(j=0;j<41;j++)
      for(i=0;i<79;)
         txt[j][i]=32;

    text();
    return 0;
}

void text()
{
     for(j=0;j<41;j++)
     {
        for(i=0;i<79;)
        {
           ch=getch();
           
           if(isalpha(ch) || isdigit(ch)|| ch==32)
           {
              x++;
              i++;
              txt[j][i]=ch;
              putch(ch);
           }
           else
               if (ch==13||ch==0)
               {
                  pos(ch);
               }
               else
                   if (ch=='=')
                      goto last;
        }//end of for
        y++;
     }//end of outer for
 last:
}

void pos(char ch)
{
   if(ch==13)
   {
       for( int k=i;k<=76;k++,s++)
            st[s]=txt[j][k];
       y++;
       x=0;
       printf("\n");
       puts(st);
       
       for( int t=j;t<41;t++)
       {
          for(int i=0;i<=78;i++)
          {
             putch(txt[t][i]);
          }
          printf("\n");
       }
   }//end of enter

     if(ch==0)
     {
        ch=getch();
    
         if(ch==72)
         {
           y=y-1;
           gotoxy(x,y);
         }
         else if(ch==80)
         {
           y=y+1;
           gotoxy(x, y);
         }
         else if(ch==75)
         {
           x=x-1;
           gotoxy(x,y);
         }
         else if(ch==77)
         {
           x=x+1;
           gotoxy(x,y);     
         }
     }
}

ssharish

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.