C programing problem. Any assistance would be greatly appreciated.

I'm In my first C programing class and so far everything has been going well although now I'm stuck on an exercise. I'm not looking for someone to do it for me, I just don't understand why I can't get this to work.

So the question is.

Write a program that uses function strcmp to compare two strings input by the user. The program should state whether the frist string is less than, equal to or greater than the second string.

That is the easy part. I have that part coded and it runs perfectly. However my professor wants the program to allow for multiple inputs. For example allowing you to run a verses b and then run b verses c and so on untill the user decided to quit.

My idea was to use a simple while statement. I asked the question "Would you like another comparison? press y to continue". I then used scanf to assign the users input to variable c. The while statement was while ( c == 'y'){. Unfortunatly the code works perfectly whout the while statment but as soon as i put it in I get very weird results from the program.

Here is the code:




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
#include <stdio.h>
#include <string.h>


int main(void)
{
char str1[25]; /*initialize string 1*/
char str2[25]; /* initialize string 2*/
int c = 'y';

while(c == 'y'){
printf(" Enter the first string:\n ");
scanf("%s", str1); /* reads string 1*/

printf(" Enter the second string:\n ");
scanf("%s", str2); /* reads string 2*/

if(strcmp(str1, str2) >= 1) { /* compare strings if first string is greater than second */
printf("The first string is greater than the second string\n%s is greater than %s\n", str1, str2);
}
if(strcmp(str1, str2) <= -1){ /* compare strings if first string is less than second */
printf("The first string is less than the second string\n%s is less than %s\n", str1, str2);
}
if(strcmp(str1, str2) == 0){ /* compare strings if the strings are equal */
printf("The first string is equal to the second string\n%s is equal to %s\n", str1, str2);
}

printf( "Would you like another comparison? press y to continue:\n" );
scanf( "%d", c );
}

return 0;

}




The output looks like this.




Enter the first string:
a
Enter the second string:
b
The first string is less than the second string
a is less than b
Would you like another comparison? press y to continue:
y
 Enter the first string:
   Enter the second string:




As you see the while statment works in making the program repeat, however when it repeats it does not give me the option to enter the first string, and the second string is indented. This makes no sence to me and I am at a complete loss. It is probably something simple right in front of me. Any help would be greatly appreciated.
Last edited on
scanf( "%d", c );

This is wrong for a few reasons.

#1: it should be &c (ie, you need to give it a pointer to c)

#2: %d takes a decimal number as input. If the user inputs "15", then c == 15. This does not read characters.

If you want to read individual characters, you should use %c. Also, you should change 'c' to be a char instead of an int.


So yeah... the fix:

1
2
3
4
5
char c;

//...

scanf( "%c", &c );
Last edited on
i think this one will work!,..use do while! And one thing more,look at your code at line 9,look at your data type that your trying to initialize!,and your using strcmp w/c i think unsuitable for your task,cause i ran it and there were bugs on it!,..try to debug it!,if you really want to be a good at programming use the right code,..
"strlen" is the appropriate one!,..goodluck!


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
#include <stdio.h>
#include <string.h>


int main(void)
{
char str1[25]; /*initialize string 1*/
char str2[25]; /* initialize string 2*/

char c;

do
{
printf(" Enter the first string:\n ");
scanf("%s", &str1); /* reads string 1*/

printf(" Enter the second string:\n ");
scanf("%s",&str2); /* reads string 2*/

if(strcmp(str1, str2) >= 1) { /* compare strings if first string is greater than second */
printf("The first string is greater than the second string\n%s is greater than %s\n", str1, str2);
}
if(strcmp(str1, str2) <= -1){ /* compare strings if first string is less than second */
printf("The first string is less than the second string\n%s is less than %s\n", str1, str2);
}
if(strcmp(str1, str2) == 0){ /* compare strings if the strings are equal */
printf("The first string is equal to the second string\n%s is equal to %s\n", str1, str2);
}

printf( "Would you like another comparison? press y to continue:\n" );
scanf( "%s",& c );
}while(c=='y'||c=='Y');

return 0;

}
Last edited on
I appreciate both of your help. I thought of the pointer and initilizing c as a char instead of an int. If i initiate it as a char and use %c regularly without the pointer the program crashes. When I made both corrections I came up with almost the same code that qtpan came up with. However now unfortunatly the program stops after asking if the user wants to continue.


 Enter the first string:
 a
 Enter the second string:
 b
The first string is less than the second string
a is less than b
Would you like another comparison? press y to continue:
Press any key to continue . . .


when it says Press any key to continue... thats the end of program. no matter what you press it closes
put your "Press any key to continue. . . " outside your loop!
@qtpan,
if you really want to be a good at programming use the right code,..

I recommend you do the same.

Also, he does want strcmp() not strlen().

What Disch said was correct.

@jaymilum,
First of all, I'm glad you're using code tags, but please indent your code to make it easy to read.
Secondly, you must be using system("pause"). This is bad. Please use getchar() instead.
Thirdly, are you initializing char c? If so, you should be initializing it with a 'y' or a 'Y'. If not, you should be initializing it with a 'y' or a 'Y'. If you don't initialize it correctly, the loop falls through.
Fourth, the reason that the "Would you like another comparison? press y to continue:" falls through is because of newlines left on stdin by scanf. Use getchar() after each scanf().
Finally, you do not need scanf() and the overhead of checking the format and what-not will make your code slower. Use getchar() instead of scanf().
Read: http://cplusplus.com/forum/beginner/22128/#msg116074
http://cplusplus.com/forum/articles/11153/
Last edited on
Topic archived. No new replies allowed.