Thread: help with basics

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

    help with basics

    My task is to get the user to input a poem and then output it backwards(line by line). As a beginner i havent a clue where to start. The following are required to be in the code, the function getchar, if the user enters too many bytes more memory has to be allocated using pointers to pointers, check for null pointer, and reallocate more memory if there isn't enough. Therefore a code using C++ and also using getchar, malloc, realloc and print out the poem backwards. Where do i start, can anybody help me please!!!!!!!!!!!

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

    ~

    Instead of malloc, realloc, why not use std::vector and std::string? The C++ standard library shall simplify your task. Bask in it's warmth.

    Code:
    #include <vector>
    #include <fstream>
    #include <string>
    
    /* ... */
    
    std::vector <std::string> v;
    
    /* ... */
    
    std::ifstream i("poem.txt");
    /* ... */
    
    // get a line from the file
    char buf[256];
    
    i.getline(buf,256,'\n');
    v.push_back(std::string(buf));
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    Things must be different here in Ireland so rmullen3, but that was the assignment i was given by our computers lecturer in uni. Im only a first year and really havent a clue. Do you know anything bout doing it using malloc and realloc etc. Here's my attempt but there is a lot of error's. Can you see what i'm at!!!!
    It probably dosent make sense but the standard libraries are declared at the top for strings, pointers and cout.

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


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

    main()
    {
    int nch = 0;
    int c;
    max = 20


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

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

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


    {
    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;

    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    This is the <out of range> time i have noticed i have to notice
    this, USE CODE TAGS.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    What do you mean by code tags, we probably have a different name for them. Can you show me an example and how it would be appropriate for my code. CHEERS!!!!!

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by pwbtech
    What do you mean by code tags, we probably have a different name for them. Can you show me an example and how it would be appropriate for my code. CHEERS!!!!!
    I lost the link at the moment but put a 'code' between [] where
    the code starts and a '/code' between [] where it ends.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    will you have a look at my new thread, its explained a little better there.

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by pwbtech
    What do you mean by code tags, we probably have a different name for them. Can you show me an example and how it would be appropriate for my code. CHEERS!!!!!
    I sent you a PM hours ago giving you links to the FAQ, rules, code tag information, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Modern C++ and Basics of OO Programming" (e-learning course)
    By Erhard Henkes in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2004, 03:01 PM
  2. Win Api Basics...
    By Devil Panther in forum Windows Programming
    Replies: 19
    Last Post: 09-09-2004, 11:28 AM
  3. C++ Basics part two
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-22-2003, 11:34 AM
  4. The Basics
    By Granger9 in forum Windows Programming
    Replies: 5
    Last Post: 09-13-2002, 05:12 PM
  5. OO Basics
    By DISGUISED in forum C++ Programming
    Replies: 3
    Last Post: 01-04-2002, 07:25 PM