-2

I'm currently trying to convert a string into three integers. I need to do some math with "11", "5" and "2". At least in this example. I tryed to convert the string using stoi() into integers, but I couldn't do the part where I take only a part of the string, like in my second example down below. Is there a simple solution for this? I started programming in C++ this week, so I don't have much knowledge.

#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;

int main() {
  int seconds 01 = 0;
  int seconds 10 = 0;           // somehow either one was beeing set to 35 by default, so I had to do 
                               this
  int minutes = 0;          
  string time = "11:52"; // mm:ss format
  
  // I need to convert the string into three integers
  
  cout << minutes << "  " << seconds10 "  " << seconds01;
  return 0;
}

Second example:

string time = "01:50"; // mm:ss format
  int seconds10 = stoi(time[3]);
// [Error] call of overloaded 'stoi(char&)' is ambiguous

Function:

minutes should output "11"
seconds10 = 5
seconds01 = 2

Casey
  • 10,297
  • 11
  • 59
  • 88

3 Answers3

1

stoi(time[3]) won't work, as that is passing a single char to stoi(), which wants a null-terminated char* string instead. You can use stoi(&time[3]) instead (since string's inner buffer is guaranteed to be null-terminated in C++11 and later), or better stoi(time.c_str()+3), eg:

string time = "01:50"; // mm:ss format

int seconds = stoi(time.c_str()+3);
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;

Otherwise, you can split up the std::string into the desired substrings via string::find() and string::substr(), then you can convert the substrings to integers, eg:

string time = "11:52"; // mm:ss format

auto pos = time.find(':');
int minutes = stoi(time.substr(0, pos));
int seconds = stoi(time.substr(pos+1));
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;

Or, you could just put the std::string into a std::istringstream and then use operator>> to extract values from it, eg:

string time = "11:52"; // mm:ss format
int minutes, seconds;
char colon;

istringstream iss(time);
iss >> minutes >> colon >> seconds;
int seconds10 = seconds / 10;
int seconds01 = seconds % 10;

That being said, you could alternatively extract the seconds10 and seconds01 values like this instead (see How to convert a single char into an int), eg:

string time = "11:52"; // mm:ss format
int minutes, seconds10, seconds01;

int minutes = ...;
int seconds10 = time[3] - '0';
int seconds01 = time[4] - '0';
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

first part of the problem: splitting string

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main() {
  string time = "11:52"; // mm:ss format
  
  int pos = time.find(':');
  int minutes = stoi(time.substr(0, pos));
  int seconds = stoi(time.substr(pos, 5));
  
  cout << minutes <<":" << seconds<< endl;
  return 0;
}

You get the integers for minutes and seconds.

part 2: get the tens of seconds

One easy way is to split your integer:

// seconds is 52

int seconds01 = seconds % 10; // gives 2
int seconds10 = (int)seconds / 10; // gives 5

You can then apply your math to the seconds.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
midugh
  • 608
  • 5
  • 21
  • `time.substr(pos, 5)` is wrong. `pos` holds the index of `:`, and the 2nd parameter is a count not an index. It needs to be `time.substr(pos+1, 2)` instead, or just `time.substr(pos+1)` – Remy Lebeau May 05 '21 at 22:02
0

You could also create a function like this that splits the string and converts it to a vector.

#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>

std::vector<int> to_ints(std::string const& s, char delim)
{
    std::vector<int> result;
    std::istringstream iss(s);
    std::string token;
    while(std::getline(iss, token, delim))
        result.push_back(std::stoi(token));
    return result;
}

int main()
{
    std::string str{ "01:50:34:2341" };
    auto ints = to_ints(str, ':');

    for (auto number : ints)
    {
        std::cout << number << "\n";
    }

    return 0;
}

output

1
50
34
2341
Jimmy Loyola
  • 189
  • 10