Search code examples
c++ostringstreamistringstreamsstream

Why is this returning an address in the console?


I'm trying to wrap my head around ostringstreams and istringstreams. So, as I always do, I made a log-in program out of it. but every time I try to cout the contents of the username and password variables, it returns the address!

Purpose for program: to create a simulated log-in screen using input and output stringstreams

code:

#include<iostream>
#include<string>
#include<conio.h>
#include<stdio.h>
#include<sstream>

using namespace std;

int main(int argv, char *argc[]){

char ch;
ostringstream username,
    password;
ostringstream *uptr, 
    *pptr;

uptr = &username;
pptr = &password;

cout << "Welcome" << endl << endl;

cout << "Enter a username: ";
do{

    ch = _getch();
    *uptr << ch;
    cout << ch;

}while(ch != '\r');


cout << endl << "Enter a Password: ";
do{
    ch = _getch();
    *pptr << ch;
    cout << "*";

}while(ch != '\r');

//if(username == "graywolfmedia25@gmail.com" && password == "deadbeefcoffee10031995"){
    cout << endl << "username: " << *username << endl << "password: " << *password << endl;
//} else {
    //cout << endl << "ACCESS DENIED" << endl;
//}



return 0;
}

I tried using the *uptr and *pptr last, but before that I tried just writing and reading straight from the variables.


Solution

  • you should use str to get std::string from ostringstream

    so

    cout << endl << "username: " << username.str() << endl << "password: " << password.str() << endl;