help please!!!!!

This is a discussion on help please!!!!! within the C++ Programming forums, part of the General Programming Boards category; I am only a beginner to C++, but i have to write code to input a poem from the user ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    help please!!!!!


    I am only a beginner to C++, but i have to write code to input a poem from the user and output it backwards (line by line). Here is my attempt, can you set me in the right direction to get it working. Any help much appreciated.

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>


    int getline(char line[],int max);

    int main(void)
    {
    int nch = 0;
    int c;
    max = max - 1;

    while((c = getchar*()) !=EOF)
    {
    if(c=='\n')
    break;

    if(nch<max)
    {
    line[nch] = c;
    nch = nch + 1;
    }
    }
    if(c==EOF&& nch ==0)
    return EOF;

    line[nch] = '\0';
    return nch;
    }

    main()
    {
    char *temp_line_ptr;
    char *line_ptr;
    char **poem_ptr;
    char **temp_poem_ptr;

    printf("Enter a line of a poem\n",nch);

    char *temp_line_ptr = nch;
    len = malloc(strlen(*temp_line_ptr)+1);
    strcpy(line_ptr,temp_line_ptr);

    line_ptr = malloc(sizeof(CHAR)*len);
    if(line_ptr == NULL)
    {
    printf("out of memory\n");
    return
    }

    poem_ptr = malloc(sizeof(char*))
    *poem_ptr = line_ptr;

    while(getline(line,CHAR) != EOF)
    {

    *line_ptr = &temp_line_ptr;
    **temp_poem_ptr = &poem_ptr;
    }

    temp_poem_ptr = realloc(poem_ptr,sizeof(char*));
    if(temp_poem_ptr !=NULL)
    {
    line_ptr = temp_poem _ptr;
    }
    else
    {
    printf("out of memory\n");
    }

    *poem_ptr = line_ptr;

    printf("\n,line_ptr)

    free(line_ptr)

    line_ptr = NULL;

    }

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Use code tags, read the rules, and use meaningful topic names.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Your code is messed up! You have two mains?
    Do you understand this? This doesn't look like a beginners
    program.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Please dont double post.

    Also, use code tags

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    int getline(char line[],int max);
    
    int main(void)
    {
      int nch = 0;
      int c;
      max = max - 1;
    
      while((c = getchar*()) !=EOF)
      {
        if(c=='\n')
          break;
        
        if(nch<max)
        {
          line[nch] = c;
          nch = nch + 1;
        }
      }
    
      if(c==EOF&& nch ==0)
        return EOF;
    
      line[nch] = '\0';
        return nch;
    }
    
    main()
    {
      char *temp_line_ptr;
      char *line_ptr;
      char **poem_ptr;
      char **temp_poem_ptr;
    
      printf("Enter a line of a poem\n",nch);
    
      char *temp_line_ptr = nch;
      len = malloc(strlen(*temp_line_ptr)+1);
      strcpy(line_ptr,temp_line_ptr);
    
      line_ptr = malloc(sizeof(CHAR)*len);
      if(line_ptr == NULL)
      {
        printf("out of memory\n");
        return
      }
    
      poem_ptr = malloc(sizeof(char*))
      *poem_ptr = line_ptr;
    
      while(getline(line,CHAR) != EOF)
      {
        *line_ptr = &temp_line_ptr;
        **temp_poem_ptr = &poem_ptr;
      }
    
      temp_poem_ptr = realloc(poem_ptr,sizeof(char*));
      if(temp_poem_ptr !=NULL)
      {
        line_ptr = temp_poem _ptr;
      }
      else
      {
        printf("out of memory\n");
      }
    
      *poem_ptr = line_ptr;
      printf("\n,line_ptr)
    
      free(line_ptr)
    
      line_ptr = NULL;
    }
    Your code looks pretty messed up.

    You declare getline(..) at the start of the program but you never actually make the method.

    You however have two main methods.

    You should really try doing this in small steps.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    And as far as I know, your code is C and not C++.

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Rewrite your code in C++... I mean, you can really shorten your stuff and make it easier to maintain using C++ std libraries like std::string, std::vector, std::ifstream, std::cout...
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

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