Joriek
Joriek

Reputation: 39

user input for string

I got a program that standard uses a value for the string. How can i change it so the user can input the value?

Best Regards

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

Upvotes: 0

Views: 170

Answers (3)

ddacot
ddacot

Reputation: 1210

You can use : getline (cin , string) for user input. If user's input will be 4a asd , std::cin will not work, but the exemple from above will work.

Upvotes: 1

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

By using cin>>input eg:

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

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96927

Take a look at std::cin for handling standard input.

Upvotes: 0

Related Questions