#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
char* arr [10];
int count = 0;
string inp;
//stringstream ss; <- this will accumulate the numbers
do {
stringstream ss;
ss << count;
inp = ss.str();
//cout << "1:" << inp << endl;
inp.append(" is ");
//cout << "2:" << inp << endl;
inp.append((count%2 == 1) ? "odd" : "even");
//cout << "3:" << inp << endl;
vector<char> writable(inp.begin(), inp.end());
writable.push_back('\0');
arr[count] = &writable[0];
cout << arr[count] << endl;
count++;
} while (count < 10);
return 0;
}
Part I: I used stringstream to convert int into string.
//#include <sstream>
stringstream ss;
ss << count;
inp = ss.str();
Part II: I appended the string using ternary if condition.
inp.append(" is ");
inp.append((count%2 == 1) ? "odd" : "even");
Part III: I used vector to convert the string into char.
//#include <vector>
vector<char> writable(inp.begin(), inp.end());
writable.push_back('\0'); //need to put \0
arr[count] = &writable[0]; //in order to access the existing element
cin, getline skipping input (0) | 2020.09.03 |
---|---|
비주얼 스튜디오에서 MinGW를 이용해 c++파일 컴파일하기 (Run c++ file in Visual Studio Code using MinGW) (0) | 2020.08.28 |
Stringstream: cuts the string into parts by space (0) | 2020.07.19 |
Ternary if statement (0) | 2020.07.19 |
Double to int cast (0) | 2020.07.18 |
댓글 영역