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).
#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?
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.
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.
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.
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.