A
A
Artem2016-05-14 08:29:27
C++ / C#
Artem, 2016-05-14 08:29:27

How to get an int from the value of the r1_input parameter of a string like "GET /set?r1_input=123?&nocache=885898.3828703746 HTTP/1.1"?

Actually, subject. I can’t figure out pointers myself, and how, using strtok, to get the part of the string I need in order to convert it to int

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#algooptimize #bottize, 2016-05-14
@user004

auto end = str.find("?", start);
This is incorrect, or rather, the question is incorrectly formulated.
You need to look for "&"
Is the real value passed in the field "123?"

A
AtomKrieg, 2016-05-14
@AtomKrieg

Take full advantage of the STL in C++. strtok is not really needed in it.

#include <string>
#include <iostream>
#include <stdexcept>

int r1_input(const std::string& str)
{
  using namespace std;	
  string varname = "r1_input=";

  auto start = str.find(varname);
  if (start == string::npos)
    throw invalid_argument(varname+ " not found");

  start += varname.size();

  auto end = str.find("?", start);
  size_t len = (end == string::npos) ? string::npos : end-start;
  return stoi(str.substr(start, len));
}

int main()
{
  std::string input = "GET /set?r1_input=123?&nocache=885898.3828703746 HTTP/1.1";
  std::cout << r1_input(input) << std::endl;

  system("pause");
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question