Help with error Message?: error C2440: '=' : cannot convert from 'int' to 'string'

This is a discussion on Help with error Message?: error C2440: '=' : cannot convert from 'int' to 'string' within the C Programming forums, part of the General Programming Boards category; Hi, so im trying to create a string program in which the user types in "box", "cylinder", or "cube", then ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    15

    Help with error Message?: error C2440: '=' : cannot convert from 'int' to 'string'

    Hi, so im trying to create a string program in which the user types in "box", "cylinder", or "cube", then the program asks the user for the dimensions of the given shape, then outputs the volume.


    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    #include "strlib.h"
    
    int main()
    {
          int r,h,l,s,w,Volume1,Volume2,Volume3,Box,Cylinder,Cube;
          string choice;
          
    	  printf("Please Enter the shape:");
    	  choice=GetLine();
    if( GetLine()=Box)
    {
    printf("Enter Width:");
    		  w=GetInteger();
    printf("Enter Height:");
    		  h=GetInteger();
    printf("Enter Length:");
    		  l=GetInteger();
    		  Volume1=w*h*l;
    		  printf("The Volume is : %s\n",Volume1);
    }
    else if( GetLine()=Cylinder)
    {
    printf("Enter Radius");
    		  r=GetInteger();
    		  printf("Enter Height");
    		  s=GetInteger();
    		  Volume2=3.14*r*r*h;
    			  printf("The Volume is: %s",Volume2);
    }
    else if ( GetLine()=Cube)
    {printf("Enter the Side Length");
    				  s=GetInteger();
    				  Volume3=s*s*s;
    				  printf("The volume is: %s",Volume3);
    }
    		  
    }
    However, i keep getting this error message: error C2440: '=' : cannot convert from 'int' to 'string'. For example it is in line 24.

    Can someone please help?

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    15
    Like, I understand that the "int" is the problem. I have also tried using "double" or "float", but they still show the same error.

    What do I do?

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I'm assuming that getLine() returns a string and you are trying to compare that with an int in your if statement and the compiler has no standard way of doing that. It's like comparing a house to a book.

    What is the function signature of GetLine()?

    EDIT: ALSO note that your if comparison's are wrong, '=' is an assignment operator, '==' is an equality check.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    15
    What do you mean?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) What is the return type of GetLine()?

    2) change your if clauses so that = is replaced by ==.

    if(x = 3) will always be true because you are not comparing x to 3, you are assigning the value 3 to x and this evaluates to true.

    if(x == 3) is what you want.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    15
    Yea, I had == before, but I got 3 more error messages saying: error C2040: '==' : 'string' differs in levels of indirection from 'int'

    That's why I got rid of them.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Nothing595 View Post
    Yea, I had == before, but I got 3 more error messages saying: error C2040: '==' : 'string' differs in levels of indirection from 'int'

    That's why I got rid of them.


    Keep up the good work then!

    ROFL
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    15
    Seriously, what do I do to fix it?

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Not very sure how your GetLine function works. Assuming that it returns a proper string. You are assigning the returned string from GetLine() to Choice. Remember there is no datatype called string in C. Its only in C++. Which means the following statement is invalid

    Code:
    string choice;
    Moving further, the Choice is checked to be any one of those dimensions. But you’re using assignment operator to compare string. This is not correct. In C you cant compare string using '==' operator. Using strcmp function to compare two string.

    Code:
    if( GetLine()=Box)
    
    should have been
    
    if( choice == Box) // if c+ , if Box = "Box"
    
    if( strcmp( choice, "Box" )  == 0 ) // in C
    Ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,565
    Code:
    printf("The Volume is : %s\n",Volume1);
    Find the problem with above line and correct it; correct all the other problems starting with the first one you know how to fix.

    Hint: What type is Volume1?

    If you do not see the problem; read up on the printf method of printing different variable types.

    What type should be used for Volume2? Is int the correct type?



    Tim S.
    Last edited by stahta01; 10-08-2010 at 02:57 PM.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Should some what look like this

    Code:
    char *choice;
    
          if( strcmp(choice,"Box") == 0 )
          {
            printf("Enter Width:");
    	    w=GetInteger();
            printf("Enter Height:");
            h=GetInteger();
            printf("Enter Length:");
            l=GetInteger();
            Volume1=w*h*l;
            printf("The Volume is : %s\n",Volume1);
          }
          else if( strcmp(choice, "Cylinder") == 0 )
          {
            printf("Enter Radius");
            r=GetInteger();
            printf("Enter Height");
            s=GetInteger();
            Volume2=3.14*r*r*h;
            printf("The Volume is: %s",Volume2);
          }
          else if ( strcmp(choice,"Cube") == 0 )
    I haven't compiled the code. Your code indentation is horriable. If you look for any help on this form. You should first learn how to indent your code properly.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    15
    Quote Originally Posted by stahta01 View Post
    Code:
    printf("The Volume is : %s\n",Volume1);
    Find the problem with above line and correct it; correct all the other problems starting with the first one you know how to fix.

    Hint: What type is Volume1?

    If you do not see the problem; read up on the printf method of printing different variable types.

    What type should be used for Volume2? Is int the correct type?



    Tim S.
    The %s means to display the result of a string. I know that. I tried to change it to %d for any luck, but it didn't work.

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    The first thing to work on is datatype string and the functioin GetLine. Are they working as expected?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    2,565
    Quote Originally Posted by ssharish2005 View Post
    The first thing to work on is datatype string and the functioin GetLine. Are they working as expected?

    ssharish
    I agree; you need to FIX the known problems!
    Post the result is a properly indexed code.
    Either just edit the original post; Or, do a new post.

    Luck has nothing to do with programming!!!


    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

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