I need a program to read two numbers and store these number's digits in an array with a ';' in between them. I tried it using a char array but it didn't seem to work for me, and I also tried, as you can see below, by storing the numbers in a string first and putting a ';' in between then storing them in the array. How can I do that without the string?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a,b;
char v[99999];
string numTotal;
cin>>a>>b;
numTotal=to_string(a) ';' to_string(b);
for(int i=0;i<numTotal.length();i ){
v[i]=numTotal[i];
cout<<v[i];
}
}
CodePudding user response:
string is simplest way, but if you dont want to use string, you can use std::stack like this
int main() {
int a, b, n;
cin >> a >> b;
stack<int> aStack;
n = a;
while(n > 0) {
aStack.push(n % 10);
n /= 10;
}
stack<int> bStack;
n = b;
while(n > 0) {
bStack.push(n % 10);
n /= 10;
}
while(!aStack.empty()) {
cout << aStack.top() << ' ';
aStack.pop();
}
cout << '\n';
while(!bStack.empty()) {
cout << bStack.top() << ' ';
bStack.pop();
}
}
CodePudding user response:
You may want to use a function that's called getline(std::cin,) ( as long as you don't press a specific keyword like: Enter or sth) it will take your string all at once ( you can write 3;4 or sth and it will store it word-by-word).
getline(cin,numTotal);