problems with if() or scanf() i think...

This is a discussion on problems with if() or scanf() i think... within the C Programming forums, part of the General Programming Boards category; Hi I'm writting this post because this program I wrote hangs in it's while() after the user input I'm including ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    4

    problems with if() or scanf() i think...

    Hi I'm writting this post because this program I wrote hangs in it's while() after the user input I'm including the program code and I can't figure out why it hangs. I think the error might also could be with scanf(), I've included the code any help would be greatly apprecaited.
    Code:
    /***************************************************************************************/
    /* Metric.C - Converts English length units to their metric equivalent */
    #include <stdio.h>
    
    char line[100];			/* line of input data */
    char unit_type[1000];	/* the type of unit you want it converted from */
    double english;			/* the number of english units to be converted */
    double result;			/* the result of the conversion */
    double inches;
    double feet;
    double yards;
    double miles;
    
    int main()
    {
    	printf("Please enter the number of you units you want converted and their unit type in the format units then unit type,\n");
    	printf("possible conversion types are: inches to centimeters, feet to meters, yards to meters, miles to kilometers.\n");
    	printf("Please enter the number of units to be converted followed by their type or type q to quit: ");
    	fgets(line, sizeof(line), stdin);
    	scanf(line,"%lf %s", &english, &unit_type);
    	
    	while (1) 
    	{
    		
    		if (unit_type == "q") 
    			break;
    		
    		if (unit_type == "inches")
    		{
    			result = english * 2.54;
    			printf("The number of centimeters are %lf\n", result);
    		} else if (unit_type == "feet") {
    			result = english * 0.3048;
    			printf("The number of meters are %lf\n", result);
    		} else if (unit_type == "yards") {
    			result = english * 0.9144;
    			printf("The number of meters are %lf\n", result);
    		} else if (unit_type == "miles") {
    			printf("The number of kilometers are %lf\n", result);
    			result = english * 1.609347;
    		}
    		
    	}	
    	return (0);
    }
    Last edited by Salem; 07-30-2004 at 06:33 AM. Reason: Tagging the code - please read the announcements to find out more

  2. #2
    Code Ripper
    Join Date
    Jun 2004
    Posts
    30
    nostrum,

    i'm not a c expert, but seens to me that:

    1) your fgets()/scanf() should be inside the while loop. else it only will read the user values once, and then always use same values over and over;

    2) you cant use == to compare strings... strings are arrays of chars, so, you should compare each char individually, or use a function like strcmp() or strcmpi()

    i hope that help.

    jmgk

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
     scanf(line,"%lf %s", &english, &unit_type);
    should be
    Code:
     sscanf(line, "%lf", &english);
     sscanf(line, "%s", &unit_type);

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by sand_man
    Code:
     scanf(line,"%lf %s", &english, &unit_type);
    should be
    Code:
     sscanf(line, "%lf", &english);
     sscanf(line, "%s", &unit_type);
    No. It should be
    Code:
     sscanf(line, "%lf", &english);
     sscanf(line, "%s", unit_type);
    or
    Code:
     sscanf(line, "%lf%s", &english, unit_type);
    and the returned code might be tested against 2.
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    Okay, basically your problem is that in C, you can't compare strings that easily. Instead, you have to use strcmp():

    Code:
    if ( strcmp (unit_type, "q") == 0 )
       break;
    
    if ( strcmp (unit_type, "inches") == 0 ) {
       ...
    }
    strcmp() returns a negative number if the first parameter is lexicographically less than the second, 0 if the strings are the same (equal), and a positive number if the first parameter is lexicographically greater than the second.

    Have fun, and happy coding.
    *** TITANIC has quit (Excess Flood)

  6. #6
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    Just to add to that: when you attempt to compare strings with the == operator, you're comparing their pointers. So basically, you were comparing the memory address of unit_type with the memory address of "q", which will never return true. Here's some examples of how it works:

    Code:
    char a[10] = "jingle";
    char b[10] = "bells";
    char c[10] = "jingle";
    char *d = b;
    
    if ( a == c ) // This returns false since a and c are at different memory locations
    
    if ( strcmp (a, c) == 0 ) // This is what you want; it compares the strings (don't forget the '== 0'!)
    
    if ( b == d ) // This returns true, because d is just a pointer to b, meaning that they have the same memory addresses
    
    d[0] = 'f';
    if ( strcmp (b, d) == 0 ) // Also returns true, because modifying d also modifies b, since they both point to the same string. Now they are both "fells".
    
    d = a;
    if ( strcmp (a, d) == 0 ) // Now d points to a, so they are both "jingle".
    
    if ( a == d ) // This is true as well
    
    if ( d == c ) // False, because d only points to a. Their content is the same, but they point at different places.
    
    if ( strcmp (c, d) == 0 ) // This, however, is true.
    I hope that didn't confuse you too much

    P.S.: Oh, and you didn't collect input from inside the loop, which you need to do or it will keep parsing the same input (and printing the same thing over and over). Put it in the while loop and you should be fine!
    *** TITANIC has quit (Excess Flood)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Calculating
    By danepporambo in forum C Programming
    Replies: 6
    Last Post: 04-25-2006, 01:04 AM
  2. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  3. scanf to struct pointer
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 12-20-2004, 09:22 AM
  4. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21