一个关于string问题求解答
程序代码:
#include<iostream> #include<string> using namespace std; main() { string Type; string T_int = "int"; string T_char = "char"; string T_float = "float"; string T_unsignedint = "usigned int"; string T_longint = "long int"; char a; cout << "请输入这个变量的类型:" << endl; cin >> Type ; getchar(); cout << "请输入这个变量值:" << endl; cin >> a; getchar(); if(("int")) (int) a; else if (("char")) (char) a; else if (("float")) (float) a; else if (("unsigned int")) (unsigned int) a ; else if (("long int")) (long int) a; else cout << "我们无法识别这个类型,请您输入int、char、float、unsigned int、long int 其中一种:\n" << endl; cout << "The int a is " << a << endl; cout << "int: " << (int) a <<"\n" << "char: " << (char) a << "\n" << "unsigned int:" << (unsigned int) a << "\n" << "float: " << (float) a <<"\n" << "long int: " << (long int) a <<"\n" << endl; system("pause"); } 这个代码的意思是要输入一个变量值、然后转换成其他变量并输出值。现 问题如下:1、char类型定义的a只能接收一个字符、但是定义成string强制转换在下不会写; 2、判断语句最后是当你输入的变量类型不是指定的那几个就输入这段话、结果也是无法实现; 3、求大神指教更深层的string的用法、比如变量类型和变量一次性输入的方法; 新手接触C++一个月求指教