Simple c program to read in input and display output won't compile in VS-08

This is a discussion on Simple c program to read in input and display output won't compile in VS-08 within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define MAXLINE 1000 int getline(char line[], int) int main() { int c, length; char line[MAXLINE]; while((length = ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Simple c program to read in input and display output won't compile in VS-08

    Code:
    #include <stdio.h>
    #define MAXLINE 1000
    int getline(char line[], int)
    
    int main()
    {
    	int c, length;
    	char line[MAXLINE];
    	
    	while((length = getline(line, MAXLINE)) > 0){
    		printf("%s",line);
    	}
    
    	getch();
    	return 0;
    }
    int getline(char s[],int lim)
    {
    
    	int i = 0;
    	int c;
    	while((c = getchar()) != EOF && c != '\n' && i < lim-1){
    		s[i] = c;
    		i++;
    	}
    	if (c == '\n'){
    		s[i] = c;
    		i++;
    	}
    	s[i] = '\0';
    
    	return i;
    	
    }
    I don't see what's wrong with this. Thanks for the help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    You're missing a terminating semi-colon for your function prototype. Also, getch is non-standard.

    Please post the errors reported by your compiler too.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Also you could replace your self-written getline function with the library's fgets() function... unless part of the exercise is to write your own stuff.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Thanks laser. The missing semi colon was the problem. Btw, I'm only using getch so that the program doesn't automatically terminate once the lines are displayed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to ...read in a hex value and display
    By MMC in forum C Programming
    Replies: 4
    Last Post: 04-11-2002, 02:38 PM
  2. Can't Compile a Simple Program
    By pmoscatt in forum C++ Programming
    Replies: 18
    Last Post: 02-10-2002, 05:14 AM
  3. File Input-output
    By ucme_me in forum C Programming
    Replies: 6
    Last Post: 12-14-2001, 02:27 PM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM
  5. Input/Output
    By PaulMelia in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 08:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21