Thread: Problem returning values in user defined format

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    Problem returning values in user defined format

    I am getting an error when trying to pass temp1 back from the bounding_box function to my main function saying 'incompatible types in return' but the format of task1 is the same as the format of bound which I am trying to pass it too. They are both of the format boundingbox which was created in the struct command at the start of the program.

    I cannot for the life of me work out why this is happening, as I have passed circ through as a value which also uses my own datatype and I had no problems with this.

    Thanks in advance

    Code:
    #include <stdio.h>
    
    typedef struct{
    	int x,y;
    	int r;
    }circle;
    
    typedef struct{
    	int x1,y1;
    	int x2,y2;
    }boundingbox;
    
    circle makecircle(int x, int y, int r)
    {
      circle temp;
      temp.x = x;
      temp.y = y;
      temp.r = r;
      return temp;
    }
    
    int bounding_box(circle circ)
    {
        
      int xa,xb,ya,yb;
      xa = circ.x - circ.r;
      ya = circ.y - circ.r;
      xb = circ.x + circ.r;
      yb = circ.y + circ.r;
      
      boundingbox temp1;
      temp1.x1 = xa;
      temp1.y1 = ya;
      temp1.x2 = xb;
      temp1.y2 = yb;
      return temp1;
    }
    
    int main()
    {
        circle circ;
        boundingbox bound;
        circ = makecircle(1,2,3);    
        bound = bounding_box(circ);
        return 0;
    }

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    int bounding_box(circle circ)
    {
    ...
         return temp1;
    }
    You say it returns an int. Change int to boundingbox.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    thanks, I knew it would be something stupid

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    I now have another problem, this is my current update on my code below.

    When the user enters six integers it should print out the twoboxes result, the combination of the two bounding boxes of the circles. instead it prints the first x1,y1 coordinates correctly but the x2,y2 coordinates are miles out.

    I think this is something to do with the inputting of the data as it worked before I added this step. I am not sure entirely how to encode this to read the integers in properly.

    Code:
    #include <stdio.h>
    
    typedef struct{
    	int x,y;
    	int r;
    }circle;
    
    typedef struct{
    	int x1,y1;
    	int x2,y2;
    }boundingbox;
    
    circle makecircle(int x, int y, int r)
    {
      circle temp;
      temp.x = x;
      temp.y = y;
      temp.r = r;
      return temp;
    }
    
    boundingbox bounding_box(circle circ)
    {
        
      int xa,xb,ya,yb;
      xa = circ.x - circ.r;
      ya = circ.y - circ.r;
      xb = circ.x + circ.r;
      yb = circ.y + circ.r;
      
      boundingbox temp1;
      temp1.x1 = xa;
      temp1.y1 = ya;
      temp1.x2 = xb;
      temp1.y2 = yb;
      return temp1;
    }
    
    boundingbox twoboxes(boundingbox bound, boundingbox bound1)
    {
        boundingbox combination;
        if (bound.x1 < bound1.x1){
           combination.x1 = bound.x1;}
        else {
           combination.x1 = bound1.x1;}
        if (bound.y1 < bound1.y1){
           combination.y1 = bound.y1;}
        else {
           combination.y1 = bound1.y1;}
        if (bound.x2 > bound1.x2){
           combination.x2 = bound.x2;}
        else {
           combination.x2 = bound1.x2;}
        if (bound.y2 > bound1.y2){
           combination.y2 = bound.y2;}
        else {
           combination.y2 = bound1.y2;}
        
        return combination;
    }
    
    int printboundbox(boundingbox bound)
    {
        printf("(%d", bound.x1);
        printf(",%d", bound.y1);
        printf(") ");
        printf("(%d", bound.x2);
        printf(",%d", bound.y2);
        printf(")\n");
        return 0;
    }
    
    int main()
    {
        circle circ;
        circle circ1;
        boundingbox bound;
        boundingbox bound1;
        boundingbox combination;
        char userdata[10];
        
        printf("Please enter the co-ordinates and then radii of two cirles in turn: ");
        scanf("%s", userdata);
        
        circ = makecircle(userdata[0],userdata[1],userdata[2]);    
        bound = bounding_box(circ);
        
        circ1 = makecircle(userdata[3],userdata[4],userdata[5]);
        bound1 = bounding_box(circ1);
        
        combination = twoboxes(bound,bound1);
        printboundbox(combination);
        return 0;
    }

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    circ = makecircle(userdata[0],userdata[1],userdata[2]);    
        bound = bounding_box(circ);
        
        circ1 = makecircle(userdata[3],userdata[4],userdata[5]);
        bound1 = bounding_box(circ1);
    the function are expecting the int where the your are sending a char to those function as a parameter. When u do this, it gets converted the ASCII value of that char and rather than to be the actual number which u entered. Why don't u read int array rather than char array. which make your like bit easier. So that u can avoid converting all those char into int and sending it to the function. If u still follow the same tech you cna look through atoi function which converts string to int

    ssharish2005

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    I have tried trying to read it in as an integer, but I still cannot get it to work. Infact both the values don't work out then.

    Which condition should I use for the scanf command if scanning integers into an array, I used the "%d" but im not sure if this is correct.

    I have also tried to use the atoi function but with little use, I will post up the code for that in a minute to check I haven't made a mistake somewhere in that.

    Thanks again for your help

    Edit:

    here is the atoi code.

    Code:
    int atoi(const char *userdata);

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    no if u are reading numbers into the int array do like this.

    Code:
    for(i=0;i<6;i++)
        scanf("%d",userdata[i]);
    to convert char to int u can do this

    Code:
    int num = userdata[0] - '0';
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. Problem with values changing
    By patra_user in forum C++ Programming
    Replies: 19
    Last Post: 12-12-2007, 02:12 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. Format User Input When using strings
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 03:59 AM