字符串的用户输入

user input for string

本文关键字:输入 用户 字符串      更新时间:2023-10-16

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

致以最诚挚的问候

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

您可以使用:getline(cin,string)进行用户输入。如果用户的输入是4a-asd,std::cin将不起作用,但上面的示例会起作用。

看一下处理标准输入的std::cin

通过使用cin>>input例如:

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