inputed by user string in dynamic array

Mar 3, 2015 at 6:44pm
Hello,

I want to write a program that takes inserted by user string (any characters plus spaces) in new dynamic multi-dimensional array (any row is word).
In other words, I want to divide strings on words and work with it later.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstring>

using namespace std;

int main() {
        //array that consists of characters from user-entered string
        char *cArrInput = new char[0];

        cin.getline(cArrInput,256);    
//the question is: how to replace code line above to avoid definition of entered string length (only user knows it)
        cout << cArrInput << endl;

        //array of words
        char *cArrWords = new char[0];
        int **iArrWordsCounter = new int(0);
        while(*iArrWordsCounter < strlen(cArrInput) ) { 
                cArrWords[*iArrWordsCounter] = new int[0];
        };  

        delete cArrInput[];
        delete cArrWords[];
        *iArrWordsCounter = 0;
        while(*iArrWordsCounter < strlen(cArrInput) ) { 
                delete cArrWords[];
        };  
        delete iArrWordsCounter; 
        return 0;
}


compilers error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
program.cpp: In function ‘int main()’:
program.cpp:15:36: error: cannot convert ‘int*’ to ‘int**’ in initialization
  int **iArrWordsCounter = new int(0);
                                    ^
program.cpp:16:44: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
  while(*iArrWordsCounter < strlen(cArrInput) ) {
                                            ^
program.cpp:17:30: error: invalid types ‘char*[int*]’ for array subscript
   cArrWords[*iArrWordsCounter] = new int[0];
                              ^
program.cpp:20:19: error: expected primary-expression before ‘]’ token
  delete cArrInput[];
                   ^
program.cpp:21:19: error: expected primary-expression before ‘]’ token
  delete cArrWords[];
                   ^
program.cpp:23:44: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
  while(*iArrWordsCounter < strlen(cArrInput) ) {
                                            ^
program.cpp:24:20: error: expected primary-expression before ‘]’ token
   delete cArrWords[];


Can someone help?
Last edited on Mar 3, 2015 at 8:07pm
Mar 3, 2015 at 6:51pm
cin.getline requires a number of inserted characters - but it couldnt be known beforehand.
Mar 3, 2015 at 7:08pm
 
  int *iArrInputSize = new int(0);


iArrInputSize has not been initialized. The integer value it points to could be anything.

 
  char *cArrInput = new char[*iArrInputSize];


because iArrInputSize is unknown, this array is also. Could allocate any number of bytes or no bytes at all.

Consider this:
1
2
  int iSize;    // what value is iSize?
  char *cArray = new char[iSize];  // what size is cArray? 


Vs:

1
2
  int iSize = 5; // Value of iSize is 5.
  char *cArray = new char[iSize]; // cArray can hold 4 characters and a null terminator 
Mar 3, 2015 at 7:33pm
 
int *iArrInputSize = new int(0);


This mean "create new pointer to int and link it to cells that mean number 0". So, array cArrInput is initialized.
Last edited on Mar 3, 2015 at 7:35pm
Mar 3, 2015 at 7:44pm
Ah sorry, totally missed that 0; So you want to take input from user for how big the array will be or do you just want the user to add items and expand the array dynamically?
Mar 3, 2015 at 7:48pm
Thanks for answer,

I want to write a program that takes inserted by user string (any characters plus spaces) in new dynamic multi-dimensional array (any row is word).
In other words, I want to divide strings on words and work with it later.
Mar 3, 2015 at 7:50pm
Are you required to use dynamic arrays or can you use std::vector?
Mar 3, 2015 at 7:53pm
For some reasons I couldnt use vector header.
I had updated the first post. And I think for better convenience I will constantly update my code in my first post.

Now the question is: how to replace cin.getline(cArrInput,256); to avoid definition of entered string length (only user knows it).
Last edited on Mar 3, 2015 at 8:08pm
Mar 3, 2015 at 8:01pm
You can't. You have to tell cin.getline the maximum number of characters it can expect to find before the /n character (user presses enter).
Mar 3, 2015 at 8:22pm
Maybe there is way to count entered by user characters?
Topic archived. No new replies allowed.