0

我正试图围绕 ostringstreams 和 istringstreams。所以,像往常一样,我用它制作了一个登录程序。但是每次我尝试计算用户名和密码变量的内容时,它都会返回地址!

程序目的:使用输入和输出字符串流创建模拟登录屏幕

代码:

#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;
}

我最后尝试使用 *uptr 和 *pptr,但在此之前我尝试直接从变量中写入和读取。

4

2 回答 2

2

你应该str用来std::stringostringstream

所以

cout << endl << "username: " << username.str() << endl << "password: " << password.str() << endl;
于 2013-11-28T23:39:23.923 回答
1

标准流具有地址的输出运算符:当您尝试打印指针时,它只会打印指针的地址。此外,流具有到指针的转换,用于指示流是否处于良好状态:当它处于良好状态时,即 ,stream.fail() == false它转换为合适的非空指针,通常只是this。当它处于故障状态时,它会返回0(它不转换为的原因bool是为了避免,例如,std::cout >> i为了有效:如果它将转换bool为该代码将是有效的)。

假设您要打印字符串流的内容,您只需用于stream.str()获取流的std::string.

于 2013-11-28T23:08:39.387 回答