Thread: array problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    srry electricity was out my problem that i dont understand that getline function i understand some stuff but genrally i dont get its idea and K&R is a really great book i have just started c programming and it taught me alot of stuff.
    the getline() function takes a line with as much as 999 characters in it and the condition is that as oon as u press EOF the line will be finished and also if '\n' is pressed the line will be changed and the last character of the lin ewill be '\n'.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by BEN10 View Post
    the getline() function takes a line with as much as 999 characters in it and the condition is that as oon as u press EOF the line will be finished and also if '\n' is pressed the line will be changed and the last character of the lin ewill be '\n'.
    so in getline function s[i] = c it cr8tes the string but this check
    Code:
     if 
    (
    c == '\n') {s[i] = c;
    ++i;
    } why did he do this coz a string doesnt contain \n ?also in main
    Code:
    while ((len = getline(line, MAXLINE)) > 0)
    i thought EOF is -1 not bigger than zero ?

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    so in getline function s[i] = c it cr8tes the string but this check
    Code:
     if 
    (
    c == '\n') {s[i] = c;
    ++i;
    } why did he do this coz a string doesnt contain \n ?also in main
    Code:
    while ((len = getline(line, MAXLINE)) > 0)
    i thought EOF is -1 not bigger than zero ?
    if u press "ENTER" the line will be changed so the last character pressed is '\n' so the loop breaks and the last character pressed is '\n' which is give by if(c=='\n').
    also len is the length of the the characters inputted by the user which will always be >=0.if the first character inputted is itself EOF then also len=0 not -1 coz -1 is the value of EOF, it doesn't give u the length of the line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM