Thread: error involving char type

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    error involving char type

    Code:
    #include <stdio.h>
    #include <stdlib.h>
          typedef struct
              {
              int x;
              int y ;
              }POINT;
    int main()
    {
          POINT ent;
          char quad;
          printf("\nEnter a point, x variable then y variable\n");
          scanf("%d %d", &ent.x, &ent.y);
          if ( ent.x >= 0, ent.y >= 0)
             {
             quad = "I";
             }
          else if ( ent.x < 0, ent.y >= 0 )
             {
             quad = "II";
             }
          else if ( ent.x < 0, ent.y < 0 )
             {
             quad = "III";
             }
          else if (ent.x >= 0, ent.y < 0)
             {
             quad = "IV";
             }
          printf("Your point is in quadrant: %d \n", quad);
          system("PAUSE");
          return 0;
    }
    For some reason this isnt working. It compiles but its giving me warnings about my statements in my if loops, and its not working.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A char holds one octet (one byte), And in C double quoted strings means a string (NULL terminated, = more than 1 byte). So In other words you can only fit one byte into quad.

    Consider making it a string, not a BYTE... then use strcpy() to fill it.

    Code:
    char quad[3];
    strncpy(quad, "II", sizeof(quad));
    http://www.cplusplus.com/reference/c...g/strncpy.html

    Just make sure quad is big enough...
    Last edited by zacs7; 05-18-2007 at 04:12 PM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't know how that compiles. You have more than one major error with it.

    First problem is that quad is a char not a char *, which is what you need for this, so that should change. Next, you have wrong conditions. Judging from the type of program, I would venture to say that you're trying to perform a logical AND on both of those conditions, which is done with the && operator (Don't use one & for this. That means something else in C.).

    Lastly, you're printing quad out as a numerical value when it should be printed as a string. The format specifier in printf() should be a %s.

    So with these changes, as long as a bit of formatting:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
    	int x;
    	int y ;
    }POINT;
    
    int main()
    {
    	POINT ent;
    	char *quad;
    	printf("\nEnter a point, x variable then y variable\n");
    	scanf("%d %d", &ent.x, &ent.y);
    	if((ent.x >= 0) && (ent.y >= 0))
    	{
    		quad = "I";
    	}
    	else if((ent.x < 0) && (ent.y >= 0))
    	{
    		quad = "II";
    	}
    	else if((ent.x < 0) && (ent.y < 0))
    	{
    		quad = "III";
    	}
    	else if((ent.x >= 0) && (ent.y < 0))
    	{
    		quad = "IV";
    	}
    	printf("Your point is in quadrant: %s \n", quad);
    	system("PAUSE");
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Yeah i fixed the &#37;s issue, i just forgot to change it here. I figured it had something to do with having the wrong type, i just couldnt find it in my book, thanks.

    Can someone explain how a pointer to quad fixed that? I get the string suggestion, but not the pointer suggestion.
    Last edited by Alphawaves; 05-18-2007 at 04:39 PM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    I have another problem ive been working on involving arrays.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
          int aryinput [10];
          int i;
          int a;
          int counter = 10;
          printf("\nInput a set of 10 numbers: \n");
          for (i = 0; i < 10; i++)
              scanf ("%d", &aryinput [i]);
          int a = aryinput [10]
          for (i = 0; i < 10; i++)
              counter--;
    
          printf("Reversed:");
          for (i = 0; i < 10; i++)
              printf("%d ", aryinput [i]);
          printf("\n");
          system("PAUSE");
          return 0;
    }
    What the program is sposed to do is take the user input array and reverse the values with respect to the spot they take in the array, the catch is im not allowed to use another array and i can use a variable to hold values in the array.
    The second loop is where im trying to make a loop which reverses the order. Iv thought about it for quite some time though and cant think of any way to do it. (i could make 10 variables and then reverse it that way but that is in essence using an array to reverse it.)

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about something like this:

    First iteration: temp = array[start]; array[start] = array[end]; array[end] = temp;
    Second iteration: temp = array[start + 1]; array[start + 1] = array[end - 1]; array[end - 1] = temp;

    Etc., until the indexes you're swapping meet each other. Throw it in a loop, and there you go.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Alphawaves View Post
    Can someone explain how a pointer to quad fixed that? I get the string suggestion, but not the pointer suggestion.
    When you include a block of letters inside double quotes, that's called a string literal. String literals are usually handled in a special way by your compiler and possibly placed in something like the .data segment of your program.

    Remember a string is just a sequence of chars in memory that end with a '\0' char. You can point to this block or memory by pointing to the first element of this block. That's what something like char *ptr = "sometext"; does. "sometext" is stored somewhere in your program, and ptr just points to the first char in the sequence.

    The only downside to using a pointer in that manner is that the strings are read-only. Do not alter them. If you wish to alter them, use an array where the strings are copied to that array.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Alphawaves View Post

    What the program is sposed to do is take the user input array and reverse the values with respect to the spot they take in the array, the catch is im not allowed to use another array and i can use a variable to hold values in the array.
    The second loop is where im trying to make a loop which reverses the order. Iv thought about it for quite some time though and cant think of any way to do it. (i could make 10 variables and then reverse it that way but that is in essence using an array to reverse it.)
    Imagine an int array from 0 to 9, 10 elements.

    Code:
    int i, j, temp;
    
    for ( i = 0, j = 9; i < j; i++, j--)  {
       temp = a[i];
       a[i] = a[j];
       a[j] = temp;
    }

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Haha you are a smart one! I didnt really think about setting up two conditions in a for loop.
    Thanks for explaining the sting/pointer question.
    Last edited by Alphawaves; 05-18-2007 at 05:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM