943,884 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 614
  • C RSS
Jul 19th, 2008
0

answer if u can

Expand Post »
/* 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.*/
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<ctype.h>
  4. void pos(char);
  5. void text();
  6. int i, j,k,x=0,y=0,s=0;
  7. char txt[41][79],st[79],ch;
  8. int main()
  9. {
  10. clrscr();
  11. for(j=0;j<41;j++)
  12. for(i=0;i<79;)
  13. txt[j][i]=32;
  14.  
  15. text();
  16. return 0;
  17. }
  18. //******************************************((functions))********************************
  19. void text(){
  20.  
  21. for(j=0;j<41;j++){
  22. for(i=0;i<79;){
  23. ch=getch();
  24. if(isalpha(ch) || isdigit(ch)|| ch==32){
  25. x++;
  26. i++;
  27. txt[j][i]=ch;
  28. putch(ch);
  29. }else
  30. if (ch==13||ch==0){
  31. pos(ch);
  32. }else
  33. if (ch=='=')
  34. goto last;
  35. }//end of for
  36. y++;
  37. }//end of outer for
  38. last:
  39. }
  40. //****************************
  41. void pos(char ch){
  42. if(ch==13){
  43.  
  44. for( int k=i;k<=76;k++,s++)
  45. st[s]=txt[j][k];
  46. y++;
  47. x=0;
  48. printf("\n");
  49. puts(st);
  50. for( int t=j;t<41;t++){
  51. for(int i=0;i<=78;i++){
  52. putch(txt[t][i]);
  53. }
  54. printf("\n");
  55. }
  56. }//end of enter
  57.  
  58. if(ch==0){
  59. ch=getch();
  60.  
  61. if(ch==72){
  62. y=y-1;
  63. gotoxy(x,y);
  64. }
  65. else if(ch==80){
  66. y=y+1;
  67. gotoxy(x, y);
  68. }
  69. else if(ch==75){
  70. x=x-1;
  71. gotoxy(x,y);
  72. }
  73. else if(ch==77){
  74. x=x+1;
  75. gotoxy(x,y);
  76. }
  77. }
  78. }
  79. //***********************************
Last edited by Narue; Jul 19th, 2008 at 10:30 am. Reason: Code tags -- removed email
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xlx16 is offline Offline
10 posts
since May 2008
Jul 19th, 2008
0

Re: answer if u can

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

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

Perhaps you could work on the indentation to being with. It might help you follow the code flow.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 22nd, 2008
0

Re: answer if u can

What are you forgetting here?
  1. for(j=0;j<41;j++)
  2. for(i=0;i<79;)
  3. 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]
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Jul 23rd, 2008
0

Re: answer if u can

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

”C” Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<ctype.h>
  4.  
  5. void pos(char);
  6. void text();
  7.  
  8. int i, j,k,x=0,y=0,s=0;
  9. char txt[41][79],st[79],ch;
  10.  
  11. int main()
  12. {
  13. clrscr();
  14.  
  15. for(j=0;j<41;j++)
  16. for(i=0;i<79;)
  17. txt[j][i]=32;
  18.  
  19. text();
  20. return 0;
  21. }
  22.  
  23. void text()
  24. {
  25. for(j=0;j<41;j++)
  26. {
  27. for(i=0;i<79;)
  28. {
  29. ch=getch();
  30.  
  31. if(isalpha(ch) || isdigit(ch)|| ch==32)
  32. {
  33. x++;
  34. i++;
  35. txt[j][i]=ch;
  36. putch(ch);
  37. }
  38. else
  39. if (ch==13||ch==0)
  40. {
  41. pos(ch);
  42. }
  43. else
  44. if (ch=='=')
  45. goto last;
  46. }//end of for
  47. y++;
  48. }//end of outer for
  49. last:
  50. }
  51.  
  52. void pos(char ch)
  53. {
  54. if(ch==13)
  55. {
  56. for( int k=i;k<=76;k++,s++)
  57. st[s]=txt[j][k];
  58. y++;
  59. x=0;
  60. printf("\n");
  61. puts(st);
  62.  
  63. for( int t=j;t<41;t++)
  64. {
  65. for(int i=0;i<=78;i++)
  66. {
  67. putch(txt[t][i]);
  68. }
  69. printf("\n");
  70. }
  71. }//end of enter
  72.  
  73. if(ch==0)
  74. {
  75. ch=getch();
  76.  
  77. if(ch==72)
  78. {
  79. y=y-1;
  80. gotoxy(x,y);
  81. }
  82. else if(ch==80)
  83. {
  84. y=y+1;
  85. gotoxy(x, y);
  86. }
  87. else if(ch==75)
  88. {
  89. x=x-1;
  90. gotoxy(x,y);
  91. }
  92. else if(ch==77)
  93. {
  94. x=x+1;
  95. gotoxy(x,y);
  96. }
  97. }
  98. }

ssharish
Last edited by ssharish2005; Jul 23rd, 2008 at 11:54 pm.
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Want to receive data thru serial port
Next Thread in C Forum Timeline: Sorting strings - segmentation fault





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC