C++ strings and pointers

Discussion in 'Programming' started by texanweb, Oct 22, 2008.

  1. #1
    Could anyone tell me how I can use the getline cin function with pointers?

    Thanks,
    Adam
     
    texanweb, Oct 22, 2008 IP
  2. cybernetic07

    cybernetic07 Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    have a look at this, its not 100% specific but it should get you thinking of how to apply the same theory to the application your working on.

    
      main()
      {
        char s[1000];
        char *lastline;
      
        while (!cin.fail()) {
          cin.getline(s, 1000);
          if (!cin.fail()) {
            lastline = s;
          }
          cout << "Reading... " << s << endl;  
        }  
        cout << "The last line was '" << lastline << "'" << endl;  
      }
      
    Code (markup):
     
    cybernetic07, Oct 22, 2008 IP
  3. erhythm

    erhythm Banned

    Messages:
    77
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey the above coding is the easy way to clear your doubt, and it should work too.

    Regards
    Erhythm
     
    erhythm, Oct 22, 2008 IP
  4. Tarkan

    Tarkan Peon

    Messages:
    491
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can you give us an example of what you wanted to do with pointers?
     
    Tarkan, Oct 23, 2008 IP
  5. texanweb

    texanweb Active Member

    Messages:
    377
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #5

    I am not 100% understanding what this code does. Could you explain it to me.

    I am suppose to write a function that that uses arrays and pointers to allows users to enter three students information and print what they have entered.
     
    texanweb, Oct 30, 2008 IP
  6. Tarkan

    Tarkan Peon

    Messages:
    491
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #6
    well you can have char arrays.

    You should use std::string unless you can't use that. (use using namespace std)
    To get text from user you can use cin >> string... or getline(cin,string);

    To output you use cout << string << endl;
     
    Tarkan, Nov 1, 2008 IP
  7. Dj-iceman

    Dj-iceman Active Member

    Messages:
    606
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #7
    why would you want to use pointers? pointers reveal the location in memory of said value

    just use stdio and gets()/puts()
     
    Dj-iceman, Nov 15, 2008 IP