我有一些非常簡單的事情,我試圖提示用戶輸入字符&,將該字符保存到字符串中。然後我打印整個字符串。向wstring添加TCHAR:不起作用
該程序是爲Windows,但我希望程序工作的ASCII碼& Unicode這就是爲什麼我用戶TCHAR,& wstring。
我的問題:我無法將字符(用戶輸入)添加到wstring中。它只是不會被存儲在該變量中。爲什麼?
我簡單的代碼:
#include <windows.h>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
// I am using wstring for unicode compatibility but in Windows(MSDN) is there a general
// string variable? You know how there is char, wchar & then TCHAR(which is a general variable
// for both ASCII & unicode). Is there a TSTRING or something?
wstring chStr = L"abc";
TCHAR ch = L'a';
while (ch != '!')
{
printf("Enter a character: ");
ch = getch();
chStr += ch; // the string never takes on a char it always remains as "a"
printf("\nThe characters entered so far are: %s \n", chStr.c_str());
}
system("PAUSE");
return 0;
}
爲什麼你需要ASCII支持?你還在使用Windows 3嗎? –
沒有'std :: tstring'。你可以這樣做:'typedef std :: basic_string tstring' –
Nubcase
'TCHAR'是一個typedef,可以是'char'或'wchar_t',所以你必須以某種方式解釋它。這是一個[小咆哮](http://stackoverflow.com/questions/6300804/wchars-encodings-standards-and-portability)我廣泛的字符通用。 –