Thread: modify my code

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    modify my code

    halo all,
    i've two problems.
    1.i want my program to take input few lines and output those lines in whose character length is greater than 8.here's my code
    Code:
    #include<stdio.h>
    #include<conio.h>
    #define MAX 1000
    int main(void)
    {
    	int i,c[MAX],j,len;
    	for(i=0;(c[i]=getchar())!=EOF;i++);
    	i=0;
    	while(c[i]!=EOF)
    	{
    		for(len=0;c[i]!='\n';len++,i++);
    		if(len>8)
    		{
    			for(i=0;c[i]!='\n';i++)
    				putchar(c[i]);
    		}
    	}
    	getch();
    	return 0;
    }
    but when i run this program,it gives wrong output.its output is based on the first line inputted.if the first line has more than 8 chars then it gets printed otherwise not.it doesn't goes to the second line.so please tell me what i should modify in the program to get proper output.

    2.i've recently switched to microsoft visual studio 2005.i want to know how can i debug the program step by step or in other words how do i trace the program.

    Thank You

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why not to use fgets to enter the line? it will make code logic look a lot simplier

    about debuggin - have you read this Debugging in Visual Studio
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i've recently switched to microsoft visual studio 2005.i want to know how can i debug the program step by step or in other words how do i trace the program.
    i've understood how to debug.
    why not to use fgets to enter the line?
    thanks for the reply.i wanna know wheres i'm getting wrong with the code.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by BEN10 View Post
    thanks for the reply.i wanna know wheres i'm getting wrong with the code.
    you read the string only once...

    to make it easier to debug, even if you do not want to use fgets - taks the part of your code reading string into separate function - call it somethink like

    readString(char* buf, size_t bufLen, FILE* fp)

    and this will show you the error more easely
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM