954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

answer if u can

[TEX][/TEX]/* 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);     
       }
 }
 }
//***********************************
xlx16
Newbie Poster
10 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

http://www.catb.org/~esr/faqs/smart-questions.html#bespecific

And
http://www.catb.org/~esr/faqs/smart-questions.html#noprivate

Perhaps you could work on the indentation to being with. It might help you follow the code flow.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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]

dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 

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

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You