Compare char array with char array from struct

Hi, im new here because my frustration made me sign up. :D
Further: sorry for my possible bad englisch.
My Problem: I want to compare an char array with an char array from a struct.
The following if() is just being ignored, but the varialbes in the arrays seem to show the same value (using Visual Studio Express).

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 <stdlib.h>
 #include <cstring>
 #include <fstream>
  struct Produkt
	{
		char prod[20];
		int stueck[1];
		int monat[1] ;
		int teile[100];
	};
 /*...*/

 cout << str[0][i] << endl;
 cout << produkte[nummer].prod[i] << endl;

 while (str[0][i] != ' ' || produkte[nummer].prod[i] != '\0' || produkte[nummer].prod[i] != ' ')
	{
		if (str[0][i] == produkte[nummer].prod[i])
		{
			test == 1;
		}
		else
		{
			test = 0;
			break;
		}
		i++;
        }


I already tried it with strcmp(& ,& ),strncmp(& ,& ,1), isalpha() == isalpha(), atoi() == atoi() but it jumps everytime to else
Is it somehow more complicated since the one array is from a struct, but why is the cout showing the same value for both of them?
strcmp should work for your application. returns 0 when both strings are equal.

Why is there produkte[nummer].prod[i] and not produkte[nummer].prod in cout?
I dont use char strings, I only copy/compare/read every character in the char array seperatly. I know its a ***** to do, but this way I don't get confused.

I already tried it:
if (strcmp(&str[0][i] , &produkte[nummer].prod[i]) == 0)
---> it goes directly to else

if (strcmp(&str[0][i] , &produkte[nummer].prod[i]) != 0)
---> it goes directly to i++; ---> the if() is somehow broken..

I really don't want to use a loop like while() to avoid the if(). I have never seen something like that before.
Note: I checked, every if() before and after that is working correctly.
Last edited on
Maybe I misunderstand your intention but you use the strcmp wrongly. The string starts @

produkte[nummer].prod

Look at the example: http://www.cplusplus.com/reference/cstring/strcmp/
Update:
Strange case: a few lines above i have

1
2
3
4
if (produkte[nummer].prod[0] != str[0][0])
		{
			continue;
		}


and it works correctly. The only difference is the variable i and nummer and when i follow them, they do exactly what they should.
I really don't know what to do.
I think you misunderstand strings. You compare the 1st character of both strings with each other, instead of the full string.
Oh, I got your answer too late. I can't use strcmp() with produkte[nummer].prod because my str[0] has an unspecific lenght. Even with strncmp() i get the same error. I know it's weird to use <cstring> on one letter chars but I ran out of options, since the normal operators == and != seem to don't work.
Update:
Strange, but I got it. I wrote the same thing again exept I changed it from
1
2
3
4
if (str[0][i] == produkte[nummer].prod[i])
		{
			test == 1;
		}

to
1
2
3
4
if (produkte[nummer].prod[i] == str[0][i])
			{
				test = 1;
			}

and now it works. I just don't understand why.
"str[0] has an unspecific length."

I am lost now.
- What type does str[0] have"
- Why would the length be a problem for strcmp?

Sorry for my bad explanations.
char str[0];
then
t.getline(str[0], 900);
but after the few first characters str[0] is totally different to produkte[nummer].prod. So I just compare the first chars bit by bit to a ' ' or '\0' and exit a loop. Now my comparison works, but I didn't knew the order was important. Maybe its just VSExpress.
Topic archived. No new replies allowed.