Hi guys, I am new to c++,
I have a homework and I have no idea how to do it. I need a program that get a character and a number between <>, word and a number between [] or a sentence between () and multiply it. Example:
Input: A<4>
Output: A A A A
Input: good[3]
Output: good good good
Input: A is good. (2)
Output: A is good. A is good.
The user should enter the string and number in a sentence like: it is a sentence(2)
and the program should print: it is a sentence. it is a sentence.
@Moschops
#include<string>
#include<iostream>
using std::cin;
using std::cout;
using std::string;
int main()
{
cout<<"Input : ";
string s;
getline(cin,s);
auto p1 = s.find_last_of("(<[");
auto p2 = s.find_last_of(")>]");
auto s1 = s.substr(0,p1);
int times = stoi(s.substr(p1+1,p2));
cout<<"Output: ";
while(times--)
std::cout<<s1<<" ";
}
As Thomas1965 said, you're not using a C++11 compiler, or have not turned on the C++11 option if your compiler supports that. stoi is only available in C++11.
If enabling C++11 is not an option, you can alternatively use stringstream.
1 2 3 4 5
#include <sstream>
... // Replace line 16 with the following:
stringstream ss ((s.substr(p1+1,p2));
int times;
ss >> times;