2012-03-07 89 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