0

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.

Kara
  • 6,115
  • 16
  • 50
  • 57

2 Answers2

2

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

so

cout << endl << "username: " << username.str() << endl << "password: " << password.str() << endl;
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
1

The standard streams have output operators for addresses: when you try to print a pointer, it will just print the pointer's address. In addition, the streams have a conversion to pointer which used to indicate if the stream is in a good state: when it is in good state, i.e., stream.fail() == false, it converts to a suitable non-null pointer, normally just this. When it is in failure state it return 0 (the reason it isn't converting to bool is to avoid, e.g., std::cout >> i to be valid: if it would convert to bool this code would be valid).

Assuming you want to print the content of the string stream, you'd just use stream.str() to get the stream's std::string.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380