1

I'm fairly new to all of this and I somehow can't seem to find a solution. Although I have no syntax errors, the program doesn't work as its supposed to.

I want the users to input two firmnames [int read ()] which get saved under the char arrays x and y. From there I want to compare them both if they are equal or not [int equal()]. After that, if they are equal, I want to print out accordingly[int stringconcatenate()]

I want **read() ; equal() ; stringconcatenate() to be connected with the main program and work accordingly.

I'm trying to take the entered "firmnames" and then save them under the array name x and y. (which doesn't work as it should).. Am I missing something?

this is what I get if I enter "test" for both firmnames:

Please type in Firmname 1:      test
Please type in Firmname 2:      test
Strings are unequal.
 a & ³■a are different.

Any tips are very much appreciated. Btw, I'm not allowed to use strcmp, hence my unorthodox code.

#include <stdio.h>
#include <ctype.h>


int read (){
char x[50]; 
char y[50];

    printf("Please type in Firmname 1:\t");
    scanf("%s", &x);
    printf("Please type in Firmname 2:\t");
    scanf("%s", &y);


}
int Equal (char x[], char y[]){

char *p1,*p2;
int f = 0;

p1=x;
p2=y;

while (*p1!= '\0' || *p2!='\0'){
    if(*p1 != *p2){
        f=1;
        break;
    }
    p1++;
    p2++;
}
if (f==0)
    printf("\nStrings are equal.\n");
else
    printf("Strings are unequal.");

}

int stringconcatenate(char x[], char y[]){ 


char *p1,*p2;

p1=x;
p2=y;


if (*p1==*p2){
    printf ("\n %s is the only one.", x);
    
}
else 
printf ("\n %s & %s are different.", x, y);
return 0;
}

int main(){

char s1[50], s2[50];
    printf("Program Compare\n\n");
    read ();
    Equal (s1, s2);
    stringconcatenate(s1, s2);

return 0;

}

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
namine07
  • 13
  • 4
  • 1
    1. Which input do you provide to your program? 2. What does the program output? 3. What do you want it to output? You can [edit] your question to clarify. See also [mcve]. – anatolyg Jan 03 '21 at 11:47
  • Do you want to compare two strings ? – MED LDN Jan 03 '21 at 11:49
  • I want the users to input two firmnames[read ()] which gets saved under the char array x and y. From there I want to compare them both if they are equal or not [equal()]. After that, IF they are equal, I want to print out accordingly[stringconcatenate()] – namine07 Jan 03 '21 at 11:51
  • @namine07 please [edit] your question and put all relevant information _there_ – Jabberwocky Jan 03 '21 at 11:54
  • 1
    Your `read` function reads and throws away input. It also has undefined behavior because it doesn't return a value as its type indicates. Please [turn on your compiler warnings and treat them as errors](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – n. m. could be an AI Jan 03 '21 at 11:57
  • I now have editted as much as I could.. any help is appreciated. – namine07 Jan 03 '21 at 12:18
  • 2
    @n.'pronouns'm.: It is not undefined behavior in C for a function declared with a non-void return type to return without returning a value (by letting program control flow to its terminating `}`). It is only undefined behavior of an attempt to use the value of the function call is made. This can be used in functions that sometimes return a value and sometimes do not, such as a function that gets or sets a stored configuration value based on whether a parameter requests getting, setting, or some other action. – Eric Postpischil Jan 03 '21 at 12:40

2 Answers2

1

The basic problem is your read function which is wrong. In your original code, x and y are local variables which exist only during the execution of the read function; afterwards the are discarded and cannot be used any more. Furthermore there is no reason why x and y would be magically copied to s1and s2.

This is the corrected version of read:

int read(char x[50], char y[50]) {  //
  printf("Please type in Firmname 1:\t");
  scanf("%s", x);   // use x and not &x, x is already an address
  printf("Please type in Firmname 2:\t");
  scanf("%s", y);   //
}

and call it like this from main:

read(s1, s2);

There are more problems in your code:

  • Equal and read are int function, but they don't return anything, so they should rather be void functions.
  • Rather than displaying if the strings are equal or not, Equal should return e.g. 1 if the strings are equal and 0 if they are not and the display of the result should be done in main. This is not a programming error but rather a design error.
  • your strconcatenate function doesn't even attempt to do a string concatenation. I'm not sure what you're trying to achieve with this code.
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

Note, I assume this is an assignment, so I will only include some pointers (no puns intended) so that you can correct the mistakes by yourself.

Ok, let's address problems function by function.

int read (){
    char x[50]; 
    char y[50];

    printf("Please type in Firmname 1:\t");
    scanf("%s", &x);
    printf("Please type in Firmname 2:\t");
    scanf("%s", &y);


}

This function simply reads two strings from standard input (stdin from now on) and then it throws them away. Neither x nor y are in some ways returned to the main function, that means that, when you call read in the main, and I assume you expect s1 and s2 to have, respectively, the value of x and y, s1 and s2 do not change. What you can do to address this problem is to pass a pointer to s1 and s2 to the read function. In fact, in the C language arrays used in expression have the same value of the pointer to their first element (for instance, if I use s1 in an expression, it gets converted to the pointer to the first element of s1). That being said, please pass |(the pointers of) s1 and s2 to the read function and use scanf on them. Another problem with this function is that it says that it returns an int, but it fact it returns nothing. Please change the function to address this problem. (There is another problem with the read function that is "what if I input, for instance, the entire GPL license? The program will not be happy and it might crash. Please have a look at cppreference).

The second function I see is this:

int Equal (char x[], char y[]){

char *p1,*p2;
int f = 0;

p1=x;
p2=y;

while (*p1!= '\0' || *p2!='\0'){
    if(*p1 != *p2){
        f=1;
        break;
    }
    p1++;
    p2++;
}
if (f==0)
    printf("\nStrings are equal.\n");
else
    printf("Strings are unequal.");

}

Please, don't use general purpose functions like this to print to standard output. Make equal return a value (for instance 0 if the strings are equal, 1 otherwise). The while loop condition is wrong: what you're saying is that "if *p1 is not '\0' OR *p2 is not '\0', then go forward with the loop. If one is '\0' but the other is not, the loop will go forward.

I can't figure out what you want to achieve with the stringconcatenate function. Please explain.

LuxGiammi
  • 631
  • 6
  • 20