0

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
  • You will need to parse the number from a substring, your code only checks individual characters – UnholySheep Dec 29 '21 at 20:41
  • Don't use so-called "competition" or "online judge" sites to learn programming. You make several beginners mistakes in your code that could be easily avoided by reading [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and taking classes to learn properly. – Some programmer dude Dec 29 '21 at 20:42
  • 2
    @Omar Then they are giving you bad advice! There are just too many problems in the shown code, it's just not one or two simple things that will "magically" solve the problems with it. And I'm sorry to say that your knowledge of C++ is one of the main problems. C++ can't be taught by guessing, you really need to study it from the basic "hello world" program, and take baby steps to learn it properly. – Some programmer dude Dec 29 '21 at 20:55
  • @Someprogrammerdude cout << " Hello World" << endl; – Omar Dec 29 '21 at 20:56
  • @Omar By the way, is this your question? Did you post under an alias to avoid negative reputation in case of downvotes? That's really frowned upon and will not work out in the long run! – Some programmer dude Dec 29 '21 at 21:00
  • @Someprogrammerdude No, I am the senior developer that advised – Omar Dec 29 '21 at 21:06
  • 1
    Several problems in your code: What's `Array` used for? Why don't you free the memory allocated in `ReadNums`? Why do you allow unchecked access to `ArraySize` for the user? What happens, if the user enters a negative numer? Why the use of `__int32` instead of `size_t` or `int32_t` (non-compiler specific and available in the standard)? How do you exit the `while(1)` loop? Why use arrays, when you've got `std::vector` available that takes the burden of doing memory management? – fabian Dec 29 '21 at 21:08

2 Answers2

2

You could do this instead:

  • Parse your input string using a string stream, and
  • read your numbers directly into a vector of ints.

[Demo]

#include <iostream>  // cout
#include <sstream>  // stringstream
#include <string>
#include <vector>

int main()
{
    const std::string line{"[12,34,55,6]"};
    std::stringstream ss{line};
    
    char c{};
    ss >> c;  // read '['
    
    std::vector<int> nums{};
    int num{};
    while (ss >> num >> c)  // read <number>',' or <number>']'
    {
        nums.push_back(num);
    }
    for (auto& num : nums)
    {
        std::cout << num << "\n";
    }
}

// Outputs:
//   12
//   34
//   55
//   6
rturrado
  • 7,699
  • 6
  • 42
  • 62
0

I have made some changes and written comments throughout the code

#include <iostream>
#include <vector>
#include <cctype>
#include <string>

using namespace std; 
// first of all, using namespace std is generally not a great idea. 
// Because if it's in a header file, you can force it upon others(but it's not a problem here)

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    }

    vector <int> SumVec;
    string Nums;
    // changed all the __int32 to normal int's, it just makes the code incompatible in my opinion
    int ReadNums(); 
    //int ArraySize; this is not necessary since we can just do SumVec.size() 
   


};
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

int Solution::ReadNums(){
    for (int i = 0; i < Nums.size(); ++i) {
        if (Nums[i] == '[') continue;
        // if (Nums[i] == ']'|| Nums[i] == ',' || Nums[i] == ' ');  
        //do nothing, is a bit weird, just test for the other case (isdigit), or negate the result  
        if (std::isdigit(Nums[i])) {
            int j = i;
            string buffer;
            while (true) {
                if (Nums[j] == '[' || Nums[j] == ']' || Nums[j] == ','){
                    SumVec.push_back(std::stoi(buffer));
                    break;
                }
                // as long as we are reading digits, append them to a buffer
                else if (std::isdigit(Nums[j])){
                    buffer += Nums[j];
                    ++i; 
                    // increment the i value, we dont want to loop twice over the same digit
                }
                    
                ++j;
          }
      }
    }

 return SumVec.size(); // as said not necassary
}

int main()
{
  

    std::cout << "Welcome to the Two Sum!\n";
    Solution Soln;
    cout << "nums = ";  //[12,34,55,6]
    cin>>Soln.Nums ;
    Soln.ReadNums();


   for (auto i: Soln.SumVec){
        cout << i << endl;
    }

}

lastly i dont understand why you used __int32* ArrayRequst = new __int32[Soln.ArraySize]; so i just ignored it.

Robin Dillen
  • 704
  • 5
  • 11