Mar 3, 2015 at 6:44pm Mar 3, 2015 at 6:44pm UTC
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 8:07pm UTC
Mar 3, 2015 at 6:51pm Mar 3, 2015 at 6:51pm UTC
cin.getline requires a number of inserted characters - but it couldnt be known beforehand.
Mar 3, 2015 at 7:33pm Mar 3, 2015 at 7:33pm UTC
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:35pm UTC
Mar 3, 2015 at 7:44pm Mar 3, 2015 at 7:44pm UTC
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 Mar 3, 2015 at 7:48pm UTC
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 Mar 3, 2015 at 7:50pm UTC
Are you required to use dynamic arrays or can you use std::vector?
Mar 3, 2015 at 7:53pm Mar 3, 2015 at 7:53pm UTC
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:08pm UTC
Mar 3, 2015 at 8:01pm Mar 3, 2015 at 8:01pm UTC
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 Mar 3, 2015 at 8:22pm UTC
Maybe there is way to count entered by user characters?