2012-03-07 103 views
0

我得到一个程序,该标准使用字符串的值。 我如何更改它,以便用户可以输入值?字符串的用户输入

问候

Joriek

#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 

std::string id_strnormal = "4a"; 
std::string code = "10 4a 00 11 22 33 44 55 66 77 88 99 10 03"; 

std::string start_str = code.substr(0, 2); 
std::string id_str = code.substr(3, 2); 

int str_length = code.length(); 
std::string stop_str = code.substr(str_length-5); 


if (id_str == id_strnormal) 
{ 
      std::cout << code << std::endl; 
} 
if (id_str != id_strnormal) 
{ 
    std::cout << "package id isn't 4a" << std::endl; 
} 
system ("pause"); 
return 0; 
} 
+0

难道你不打算在你的代码中添加'using namespace std;'的目的吗? ^^ – 2012-03-07 07:57:56

+0

你可以采取任何基本的C++书籍,并通过阅读或谷歌找到答案!这是一个非常基本的问题 – 2012-03-07 08:02:02

回答

0

通过使用cin>>input 如:

std::cin>>id_strnormal; //instead of id_strnormal = "4a"; 
1

您可以使用:函数getline(CIN,字符串)用户输入。如果用户的输入是4a asd,std :: cin将不起作用,但上面的例子将起作用。

+0

谢谢你的作品 – Joriek 2012-03-07 08:03:57

相关问题