Thread: Create a pointer to a full text line

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Create a pointer to a full text line

    It's really simple to say to a pointer to point to a sigle character or a single number.

    Code:
    int *ptr;
    int a = 5;
    cout << *ptr << endl;
    But I want for example a pointer to change a whole text line. My idea is that the user enters which language he speaks.

    Code:
    cout << "English" << endl;
    cout << "Swedish" << endl;
    But I don't know how to this. I could have done this without pointers with if statments but I want to do it with pointers because it were a exercise to do. Please some hints?

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    A pointer to a character can be a pointer to the first character in a whole text line, for example (using cin.get to get a line):

    Code:
    // ...
    
        char * pchr = new char[80];
        cout << "enter a text line" << endl;
        cin.get(pchr, 80);
    
    //  after pchr is no longer needed:
    
        delete [] pchr;
    Last edited by rcgldr; 06-03-2013 at 03:27 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about a pointer to std::string instead?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    That worked really great to have a string pointer ! That must have been the easiest way of doing this. Thanks!.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        string *ptr;
        string  a = "TEXT1";
        string b = "TEXT2";
    
    
        ptr = &a;
        cout << *ptr << endl;
        ptr = &b;
        cout << *ptr << endl;
    
    
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-11-2011, 10:32 PM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. Position FILE pointer to a specified line of Text file
    By jessica_xie in forum C Programming
    Replies: 2
    Last Post: 02-04-2005, 03:52 PM
  4. Input full line from file
    By Unregis3 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 01:05 AM
  5. create a text file with data using text editor
    By fried egg in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 09:11 PM