Go Back   Hardware Forums > Software Support Forums > Web Development

Reply
 
LinkBack Thread Tools
Old 11-12-2006, 06:29 PM   #1 (permalink) Top
Big Geek
 
Join Date: Feb 2004
Age: 26 Male
Posts: 280
Status: Offline

My Computer

Default Simple C++ Problem?

The following is a very simple c++ program, that i believe should show the value of a variable x. since x = 1/3, i believe that the out put should be 0.3333, however, the output i get is x=0.
can anybody tell me why?

Code:
#include <iostream>
using namespace std;
int main()
{
 double x=10, dummy;
 
 x = (1/3);
 
 cout << "X : " << x << endl;
 cout << "Exit" << endl;
 cin >> dummy;
 return 0;
}

  Reply With Quote
Whats this? Microsoft Wireless Optical Desktop 3000
Microsoft Wireless Optical Desktop 3000
Seller Price (inc. VAT) Delivery Total Price Availability Seller Rating
Tekheads.co.uk £21.73 £4.95 £26.68 In Stock Rated: 3 out of 5 - Number of votes: 310
Microwarehouse.co.uk £72.53 £6.88 £79.41 In Stock Rated: 0 out of 5 - Number of votes:
PCWB.com £74.01 £6.88 £80.89 In Stock Rated: 3 out of 5 - Number of votes: 33
Old 11-12-2006, 05:55 PM   #2 (permalink) Top
The King

 
Addis's Avatar
 
Join Date: Jan 2004
Age: 18 Male
Posts: 5,255
Times Helpful: 403
My Mood: Drunk
Status: Offline

My Computer

If you do
Code:
x = 1/3;
then it will interpret 1 and 3 as both integers, and put the result of the expression accordingly. To get it to display 0.333333 you need to change your code to
Code:
x = (1/3.0);
or alternatively
Code:
x = (1.0/3);
That way at least one of the values will be interpreted as floating point types, and so you'll get a floating point answer.
__________________
Never trust a program you don't have the source code for.

My website | Powerful Desktop Linux | Linux for human beings | Linux for power users | Linux for ricers
Send a message via MSN to Addis   Reply With Quote
Old 11-12-2006, 06:26 PM   #3 (permalink) Top
Big Geek
 
Join Date: Feb 2004
Age: 26 Male
Posts: 280
Status: Offline

My Computer

Thanks
  Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
major help needed with probaly a simple problem donkey42 Linux and Other OSes 4 28-09-2006 01:17 PM
Fast, Simple, And Tasty Kyle89 Munchie Bar 3 11-08-2006 12:44 AM
Simple Question revler Power Supplies and UPS's 5 13-04-2006 05:11 PM
Simple question Temporal Overclocking & Cooling 8 12-02-2004 06:39 PM
simple networking rixx Networking and Computer Security 14 05-03-2003 10:01 PM


All times are GMT +1. The time now is 05:37 AM.


Copyright © 2000 - 2008 · HARDWAREFORUMS.COM · All rights reserved