I have the following program which takes in an array form such as: [12,34,55,6]
I am using a for and while loops to read character by character and if a number is read it is store in the vector SumVec
, the problem I am facing is that the vector stores only one number at a time is there any way to fix this.
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
vector <int> SumVec;
string Nums;
int ReadNums(__int32* Array);
__int32 ArraySize;
};
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
int Solution::ReadNums(__int32* Array){
ArraySize = 0;
Array = new int[ArraySize];
for (int i = 0; i < Nums.size(); ++i) {
if (Nums[i] == '[' || Nums[i] == ']'|| Nums[i] == ',' || Nums[i] == ' '); //do nothing
else {
int j = i;
while (1) {
++j;
if (Nums[j] != '[' && Nums[j] != ']' && Nums[j] != ',') {
SumVec.push_back(Nums[j]);
}
else
SumVec.push_back(Nums[i]);
}
}
}
return ArraySize;
}
int main()
{
std::cout << "Welcome to the Two Sum!\n";
Solution Soln;
cout << "nums = "; //[12,34,55,6]
cin>>Soln.Nums ;
__int32* ArrayRequst = new __int32[Soln.ArraySize];
Soln.ReadNums(ArrayRequst);
for (auto i = Soln.SumVec.begin();i!= Soln.SumVec.end(); ++i){
cout << *i << endl;
}
}
Current INPUT: [12,34,55,6]
Current OUTPUT:
1
2
3
4
5
5
6