943,610 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 759
  • C++ RSS
Nov 25th, 2008
0

Help with Measurement converter

Expand Post »
Hey I am trying to teach myself C++ and i am having trouble with a measurement converter I am pretty sure my problem is in the line

C++ Syntax (Toggle Plain Text)
  1. Yards = Meters * 1.094;

but i don't know how else to write it, this is my code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. namespace std;
  3.  
  4. int main()
  5. {
  6. cout << "welcome to my measurement converter.";
  7. cout << "This converter will convert meters to yards.\n";
  8. cout << "Enter the number of meters.\n";
  9. int Meters;
  10. int Yards;
  11. cin >> Meters;
  12. Yards = Meters * 1.094;
  13. cout << Meters << " meters is equal to " << Yards << " yards";
  14. return 0;
  15. }

Your help would be greatly appreciated
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAaronxX is offline Offline
5 posts
since Oct 2008
Nov 25th, 2008
0

Re: Help with Measurement converter

2 things.
Change :
namespace std; to using namespace std; . Your code shouldn't compile without changing it.

then change:
C++ Syntax (Toggle Plain Text)
  1. int Meters;
  2. int Yards;
to:
C++ Syntax (Toggle Plain Text)
  1. double Meters;
  2. double Yards;

Int can only be integer values So: 1, 4 , 300, 5632 etc etc.
But you need some digits after the comma, so you need the double data-type
Last edited by Nick Evan; Nov 25th, 2008 at 6:45 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 25th, 2008
0

Re: Help with Measurement converter

The mistake is that you are using integers (the set 0,1,2,3 and -1 etc)
to represent floating point number.
int a=2*1.2; gives sets a to 2.

write
c++ Syntax (Toggle Plain Text)
  1. double Meters;
  2. double Yards;
instead of the two int declarations.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Nov 25th, 2008
0

Re: Help with Measurement converter

Thanks for the help niek_e and StuXYZ it works perfectly now and i cant believe i forgot to put using before namespace.
Last edited by XxAaronxX; Nov 25th, 2008 at 6:51 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAaronxX is offline Offline
5 posts
since Oct 2008
Nov 26th, 2008
0

Re: Help with Measurement converter

Hey I Use Visual 2008 C++ Express and i don't know how to link this code to a GUI so that when i type the number of meters into a text box and press a button it will return the answer in a second text box. If you know of a tutorial on GUI's i could use it would be great.

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAaronxX is offline Offline
5 posts
since Oct 2008
Nov 26th, 2008
0

Re: Help with Measurement converter

Hey I have decided that a GUI is to Advanced for me to try at the moment and am going to keep reading my book.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAaronxX is offline Offline
5 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with class constructor..
Next Thread in C++ Forum Timeline: How to include .lib files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC