1

My code takes input(str of ints essentially)

  input: 123 456 789 90  

I'm trying to take each number such as 123 456 etc. and adding them into one element of my array but I'm not sure how to add the entire 123.

some_int represents the number of ints. So in this case, it would be 4   

int* arr_func(int some_int)
{
  std::string str;
  std::getline (std::cin, str);
  int* p = new arr[some_int];
  for (i=0;i<str.length(),++i)
  { 
  if (str[i] != ' ')
    {
    arr[n] = str[i];
    }
  }
  return p;
}    

I know what my code does and it's incorrect, but that's all I have right now. I want my array to look something like this at the end when I return it.

[123] [456] [789] [90] each [] denoting an element/cell.    

I know how to convert str to int, but the main focus is instead of just each cell/element being [1] [2] [3] [4] [5], how do I make it so it appears like above. Thanks

Lrrr
  • 4,755
  • 5
  • 41
  • 63
Justin
  • 165
  • 3
  • 14
  • first try to fix syntax.dinamic array is created with keyword new and function should return something. secondly you did not pass string. thirdly, string and integer different, you should use stoi plus before it you have split to words (you can use stringstream), – qwr Nov 05 '14 at 05:53
  • 1
    `for (int i=0; i != str.length(); ++i)` I would start by checking syntax errors. `int* p = new int[some_int];` – Andreas DM Nov 05 '14 at 06:53
  • You may use `std::vector` instead of raw owning pointer `int*`. – Jarod42 Nov 05 '14 at 08:42

4 Answers4

0

You need to use atoi() method for converting char array (string) to int.

arr[n] = atoi(string[i])
Lrrr
  • 4,755
  • 5
  • 41
  • 63
vinod sahu
  • 67
  • 12
0

I dont like to give you the complete answer, but I'll show you how to solve this issue.

First you need to split your input with spaces that you can learn here.

You also need to create your array dynamic some thing like this :

int* input = new int[some_int];

After that use a for loop like

for(int i = 0 ; i<some_int ; i++){
    //some code here
}

And in above commented area use atoi() function to convert char* to integer that you can see the sample here.

Community
  • 1
  • 1
Lrrr
  • 4,755
  • 5
  • 41
  • 63
  • @Justin ssin is in library sstream, the answer in the first link is work correctly for me, see the running version : http://ideone.com/TLWO18 – Lrrr Nov 05 '14 at 06:33
  • Can you quickly explain the while loop please. I've been trying to figure it out but I can't seem to get it..especially what "ssin >> arr[i]" does and how that makes each element a word. Also in my case, I would need to change the str to an int and then add it to my array in the while loop correct? – Justin Nov 05 '14 at 06:46
  • @Justin good is nothing special, just read check the stream if its still good! i<4 is also obvious. and ssin>>arr[i] just put i'th element of split string with spaces in arr[i], for instance arr[0] = test , arr[1] = one , ... – Lrrr Nov 05 '14 at 06:52
  • That's what I was unsure of. So using ssin just automatically splits the string? And for the for loop you wrote, would I index the array's pointer (input[i]) and change it to an int? So it would be something like input[i] = atoi(input[i])? – Justin Nov 05 '14 at 07:04
  • @Justin there is no meaning in input[i] = atoi(input[i]) , in the input there must be a char* learn about atoi from the example I gave you ;) – Lrrr Nov 05 '14 at 07:06
  • @Justin also dont forget to mark it as answer if it helped so others with same problem could find it useful. – Lrrr Nov 05 '14 at 07:08
  • Sorry, I'm still trying to figure it out. I'm not sure why we use atoi in the for loop you wrote. Because the pointer input points to an Array of ints, then the while loop won't work because it's adding strs to the int Array. Would this work? while (ssin.good() && i < 4){ssin >> atoi(arr[i]);++i; Sorry if this is a hassle..I'm trying really hard to understand – Justin Nov 05 '14 at 07:30
  • @Justin no this wouldnt work, something like this maywork : while (ssin.good() && i < 4){ssin >> arr[i];++i; input[i] = atoi(arr[i]);} – Lrrr Nov 05 '14 at 07:41
  • isn't input = arr? You declared above that input was a pointer to an array so why are we using input and arr at the same time? Or are we using 2 arrays. Copying and converting 1 array to the other? arr -> input – Justin Nov 05 '14 at 08:08
  • @Justin no it is not, arr is array of string and input is array of integer. – Lrrr Nov 05 '14 at 08:11
  • so we first create an array of strings, then we convert the strings into int and store them in our second array? I think I'm getting it now – Justin Nov 05 '14 at 08:12
  • My function is returning the dynamically allocated array of ints, and because I'm only using the str array to copy/convert things over, the str array can be static? Thank you for your time – Justin Nov 05 '14 at 08:18
  • Do I need to say New Arr and delete it later? I think no becuse I'm only using Arr in that function and not returning it. Is this correct? I'm returning the other array: input – Justin Nov 05 '14 at 08:20
  • If you are worried about garbage collection and memory, it is better to delete the arr. – Lrrr Nov 05 '14 at 08:22
  • The arr will also delete itself after the function finishes correct? Unless I say "New" – Justin Nov 05 '14 at 08:23
  • I'm not really sure, but I think it allocate the memory anyway. But I'm not sure – Lrrr Nov 05 '14 at 08:25
0

I'd simply use std::cin instead of std::getline.

Also, AFAIK, when you allocace the memory, you should free it;
returning pointer and leaving responsibility for freeing it to the caller it's not the best idea,
because it's easy to forget about freeing the memory, which lead to memory leak.

I'd use std::vector instead. Alternatively, you can use some kind of smart pointer, but I won't tell you how to use it, because simply I don't know.

I'd write:

#include <iostream>
#include <vector>

std::vector<int> arr_func(const unsigned length) {
    std::vector<int> result;
    result.reserve(length); //optimization, can be skipped
    for(unsigned i=0; i<length; ++i) {
        int temp;
        std::cin >> temp;
        result.push_back(temp);
    }
    return result;
}

/* Other solution, may be less efficent */

std::vector<int> arr_func(const unsigned length) {
    std::vector result(length);
    for(auto& i:result)
        std::cin >> i;
    return result;
}
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
-1

You can try something like this.Use the string stream to acheive this.Split the string into integers

#include<sstream>
#include <iostream>
using namespace std;

int main() {
    stringstream stream;
    string str="123 456 789";
    stream <<str;
    int n[3];
    for(int i=0;i<2;i++){
    stream >> n[i];}
    cout<<n[0]<<endl<<n[1]<<endl<<n[2];
    return 0;
}
amudhan3093
  • 740
  • 9
  • 17