Thread: processing a string troubles

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    processing a string troubles

    Needing a little advice: I have a program here and I can't figure out how to use a pointer to process a string of characters. I need to add a function assert() inside read_in() to test if MAXLINE characters are exceeded when a sting is put it, but I'm not sure how to return what I get to read_in or if I need to really, and how to access the elements of the string entered. Here is the code:

    Code:
    include <stdio.h>
    
    #define  MAXLINE  100
    void     read_in(char*);
    
    int main(void)
    {
          char   line(MAXLINE),  
    
          printf("\nWhat is your favorite line? ");
          read_in(line);
           
          return 0;
    }
    
    
    void read_in(char s[])
    {
          int     c, i = 0;
    
          while ((c = gethcar()) != EOF && c != '\n')
                s[i++] = c;
          s[i] = '\0';
    {


    I've tried some things and can't get them to work.
    Any help would be greatly appreciated.
    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char line(MAXLINE),
    Well this won't even compile.

    Try
    char line[MAXLINE];

    As for an assert, using assert() to check any kind of user input is a bad idea.
    1. assert in debug mode kills the program
    2. assert in release mode does nothing.
    Neither are desirable for checking user input.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    Would this be close or am I way off?


    Code:
    void read_in(char s[])
    {
          int     c, i = 0;
    
          while ((c = gethcar()) != EOF && c != '\n')
                s[i++] = c;
          s[i] = '\0';
          assert( s);
    {
    
    
    char* assert(char*s)
    {
          char *pi, *tail;
       
          tail = s + strlen(s) - 1
          for(pi = s; pi < tail; ++pi)
              ++cnt;
          if(cnt > MAXSTRING)
               printf("out of bounds of memory.");
          else
               // i don't know what to do here//
    }

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    that is what someone else told me also.

    Thanks Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. segmentation fault when processing a string
    By Nakel in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 04:02 PM
  4. segmentation fault when processing a string
    By EMC2 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2003, 02:56 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM