Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

int to float (C)


  • Please log in to reply
5 replies to this topic

#1 altair05

altair05

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:32 AM

Posted 06 February 2011 - 02:54 AM

I'm still working on my code hence the commented out line.

On line 29, I want a to store a float in the variable tv. I'm not sure what I'm doing wrong. I get the following error.

error: lvalue required as left operand of assignment

1 #include<stdio.h>
  2 
  3 
  4 int main()
  5 {
  6   int tv;       //The number of Televisions bought by the consumer
  7   int bluRay;   //The number of Blu-Ray Players bought by the consumer
  8   int remote;   //The number of Remote Controls bought by the consumer
  9   int mp3;      //The number of MP3 Players bought by the consumer
 10   int laptop;   //The number of Laptop Computers bought by the consumer
 11 
 12 
 13   printf("Enter quantity of televisions: ");
 14   scanf("%d", &tv);
 15 
 16   printf("Enter the quantity of Blu-ray players: ");
 17   scanf("%d", &bluRay);
 18 
 19   printf("Enter quanitity of remote controls: ");
 20   scanf("%d", &remote);
 21 
 22   printf("Enter quantity of MP3 players: ");
 23   scanf("%d", &mp3);
 24 
 25   printf("Enter quantity of laptops: ");
 26   scanf("%d", &laptop);
 27 
 28 
 29  (float) tv = tv *  599.99;
 30   bluRay = bluRay * 199.99;
 31 
 32   printf("\n%f",(float) tv);
 33 
 34 
 35 
 36 
 37   printf("\nQTY  DESCRIPTION      UNIT PRICE  TOTAL PRICE");
 38   printf("\n---  -----------      ----------  ----- -----");
 39   // printf("\n%2d  Televisions      $   599.99  $%4.2f", tv, 
 40 
 41 
 42 
 43   return(0);
 44 }


BC AdBot (Login to Remove)

 


#2 Blade

Blade

    Strong in the Bleepforce


  • Site Admin
  • 12,704 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:US
  • Local time:05:32 AM

Posted 06 February 2011 - 03:12 AM

Hello.

On line 29, I want a to store a float in the variable tv. I'm not sure what I'm doing wrong. I get the following error.


As the code is currently, you can't store a float in tv. You have tv declared as an int. Int variables can only hold ints, float variables can only hold floats, and so forth.

You're misusing typecasting here. Typecasting is used in a situation where you have a variable of one type, but need to store it's contents in a variable of another type. A typecast only applies for a single operation. You can find more information on typecasting here: http://www.cprogramming.com/tutorial/lesson11.html

Edited by Blade Zephon, 06 February 2011 - 03:19 AM.

Posted Image

If I am helping you, it has been 48 hours since your last post, and I have yet to reply to your topic, please send me a PM
Become a BleepingComputer fan: Facebook
Follow us on Twitter!
Circle us on Google+


#3 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:32 AM

Posted 06 February 2011 - 06:22 AM

Ok, that makes sense. I'm trying to store a float value in a int. So I would have to make a new float variable and store the answer there or initialize the variable as a float in the beginning, correct?

Edited by altair05, 06 February 2011 - 06:23 AM.


#4 Blade

Blade

    Strong in the Bleepforce


  • Site Admin
  • 12,704 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:US
  • Local time:05:32 AM

Posted 06 February 2011 - 04:04 PM

Bingo :thumbup2:

Posted Image

If I am helping you, it has been 48 hours since your last post, and I have yet to reply to your topic, please send me a PM
Become a BleepingComputer fan: Facebook
Follow us on Twitter!
Circle us on Google+


#5 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:32 AM

Posted 06 February 2011 - 11:09 PM

Thanks

#6 Blade

Blade

    Strong in the Bleepforce


  • Site Admin
  • 12,704 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:US
  • Local time:05:32 AM

Posted 06 February 2011 - 11:49 PM

Glad I could help :)

Posted Image

If I am helping you, it has been 48 hours since your last post, and I have yet to reply to your topic, please send me a PM
Become a BleepingComputer fan: Facebook
Follow us on Twitter!
Circle us on Google+





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users