Problem to read char arrays

So, I need to write a code where it reads 2 char arrays dynamically allocated, and then output what is in array1 that is not in array 2.
BUT, my input Must have spaces, like:

input:
10
q w e r t y u i o p
9
m n b v c x z q w

output:
e r t y u i o p

and i did this but it does not work

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
#include <cstring>
using namespace std;

char* Filtro(char* arr1, char* arr2, int tam, int tam2, int& counter){
    char* aux = new char[tam+tam2];
    for (int i = 0; i < tam; i++){
	bool found = false;
	for (int j = 0; j < tam2 and !found; j++){
	    if (arr1[i]==arr2[j]){
		found = true;
	    }
	}
	if (!found){
	    aux[counter] = arr1[i];
	    counter++;
	}
    }    
    
    char* news = news char[counter];
    for (int i = 0; i < counter; i++){
	news[i] = aux[i];
    }
    
    delete[] arr1;
    delete[] arr2;
    delete[] aux;
    return news;
}




int main(){
    int tam;
    cin >> tam;//size of arr1
    
    char* arr1 = new char[tam];
    cin.getline(arr1, 2*tam);
    int tam2;
    cin >> tam2;//size of arr2
    char* arr2 = new char[tam2];
    cin.getline(arr2, 2*tam2);
    int counter=0;
    
    char* done = Filtro(arr1, arr2, tam, tam2, counter);
    
    
    for (int i = 0; i < counter; i++){
	cout<<done[i]<<' ';
    }
    
    delete[] done;
    return 0;
}
> but it does not work
we know, otherwise you wouldn't be here.
be more descriptive: compile error, wrong output, crash, freeze... ¿what's the problem?

1
2
    char* arr1 = new char[tam];
    cin.getline(arr1, 2*tam);
you reserve enough space for `n' objects, but try to store `2*n'
good way to shot your foot.


Be careful with your memory management. ¿why is `Filtro()' deleting its input arrays? ¿should it be destructive? ¿can't it be used with static-allocated arrays?
I'm not sure I entirely understand your assignment. You say
1) Read array 1
2) Read array 2
3) Output array 1

But your output only shows the last 8 characters of array 1.

3) ¿Output the last N characters in array 1?

What I don't understand is why you then need to "filter" the arrays. How are the arrays filtered?

BTW.
Line 39, Line 43: ¿Why are you multiplying tam by 2? This permits a buffer overrun. The argument should be exactly the maximum number of characters available:

    cin.getline(arr1, tam);

You also have a filtered/unfiltered input problem between lines 36,39 and 41,43. Fix it by getting rid of the Enter key left in the input from when the user typed in a number for tam.

36
37
38
39
40
    cin >> tam;
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );

    char* arr1 = new char[tam];
    cin.getline(arr1, tam);

Also, you can reuse tam -- no need to create a new variable.

41
42
43
44
45
46

  cin >> tam;
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

  char* arr2 = new char[tam];
  cin.getline(arr2, tam);

Hope this helps.

[edit] Alas, I took a shortcut and made a mistake. See mbozzi's post below. Fixed here. [/edit]
Last edited on
output what is in array1 that is not in array 2.



it seems that you've got an array of chars, not a string.
read character by character with a loop
1
2
for(int K=0; K<tam; ++K)
   std::cin >> arr1[K];
@Duthomhas
Don't use ~0UL there!

The result of the conversion from ~0UL to std::streamsize is implementation-defined[1, 2] because std::streamsize is a signed integral type[3, 4].

At least on my platform, static_cast<std::streamsize>(~0UL) is -1, but std::numeric_limits<std::streamsize>::max() is positive (e.g., it's 0x7FFFFFFFFFFFFFFF on my computer). static_cast does indeed perform the same implicit conversion as would occur in the call to istream::ignore[5].

I don't know what passing a negative value is supposed to do: a bug in the standard?
See LEWG Issue 423: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#423

1: http://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_conversions
2: http://eel.is/c++draft/conv.integral#3
3: http://en.cppreference.com/w/cpp/io/streamsize
4: http://eel.is/c++draft/stream.types#2
5: http://en.cppreference.com/w/cpp/language/static_cast
Topic archived. No new replies allowed.