Loading
submit to reddit
May 22, 2013, 09:27:43 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome!
 
   Forum Home   Help Search Forum Login Register  
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: New to C++ need some help?  (Read 1670 times)
spookie
Newbie
*
Posts: 4


« on: September 04, 2010, 05:38:32 PM »

Hi I'm brand new to C++ trying to make a program to convert miles and feet into kilometers.


Code:
int main ()
{
double kilometers, miles, feet, meters;

cout << "Enter miles ";
cin>> miles;

cout << "Enter Feet";
cin>> feet;

feet = miles * 5280;
meters = feet / 3.28;
kilometers = meters / 1000;

meters = feet / 3.28;
kilometers = meters / 1000;

cout << miles <<" miles and "<< feet <<" feet is " << kilometers <<" kilometers " << endl;
return 0;
}

It's not giving me the correct input for feet when I display the conversion at the end. Help please!
Logged
spookie
Newbie
*
Posts: 4


« Reply #1 on: September 04, 2010, 06:17:01 PM »

Sorry correction its not reading my feet at all also I'm using quincy
Logged
David W
Newbie
*
Posts: 3


WWW
« Reply #2 on: September 04, 2010, 07:10:11 PM »

This may help get you started in C++ ...

Code:
#include <iostream>

using namespace std;


int main()
{
    cout << "Enter miles: " << flush;
    double miles;
    cin >> miles;

    cout << "Enter feet: " << flush;
    double feet;
    cin >> feet;
    
    cout << miles << " miles and " << feet << " feet = ";

    miles = miles + feet/5280; // convert feet to miles and get total miles ...
    
    double kilometers = miles * 1.609344;

    cout << miles <<" miles or " << kilometers <<" kilometers " << endl;

    cout << "\nPress 'Enter' to continue " << flush;
    while( cin.get() != '\n' ) ; // flush cin stream ...
    cin.get();
}

Quote
Effective July 1, 1959 the length of the international yard in the United States and countries of the Commonwealth of Nations was defined as 0.9144 meters.
« Last Edit: September 06, 2010, 11:47:41 AM by bluetoo » Logged

http://developers-heaven.net/forum/index.php/topic,46.0.html

http://sites.google.com/site/andeveryeyeshallseehim/home/he-comes
spookie
Newbie
*
Posts: 4


« Reply #3 on: September 04, 2010, 09:40:35 PM »

Thank you for the reply david but I was wondering if you could tell me what I'm doing wrong with my code? Everything is working its just not reading my feet at all.  Huh?
Logged
Michael Hall
Administrator
Hero Member
*****
Posts: 901



« Reply #4 on: September 04, 2010, 11:29:06 PM »

The problem is here:

Code:
cout << "Enter Feet";
cin>> feet;

feet = miles * 5280;

After you input feet, the next line writes over the value that it holds.  Wink

Mike
Logged
Michael Hall
Administrator
Hero Member
*****
Posts: 901



« Reply #5 on: September 04, 2010, 11:43:16 PM »

BTW, I think you want to create another variable:

Code:
double dTotalFeet = 5280*miles + feet;

Then use that for the conversion to meters.

Mike
Logged
spookie
Newbie
*
Posts: 4


« Reply #6 on: September 05, 2010, 09:48:34 AM »

Thank you Mike I actually solved it last night. Appreciate the help though.  Grin
Logged
David W
Newbie
*
Posts: 3


WWW
« Reply #7 on: September 05, 2010, 11:59:03 AM »

Thank you for the reply david but I was wondering if you could tell me what I'm doing wrong with my code? Everything is working its just not reading my feet at all.  Huh?

Program flow is very important in programming ... If you input or assign a value to a variable, any previous value that was in that variable is replaced by the new value  ...

I had wondered if ... by printing out the miles and feet values after they were input, but before the program updated the miles via ...

miles = miles + feet/5280;

you might 'see the flow' ?

I trust that you 'see it' now Smiley

Also ... C++ style is to declare your variable(s) just before use, with just enough scope as necessary. (Look up, i.e Google 'scope' ... you will need to understand that concept very soon.)


Also ... it is preferred to use constants rather than 'magic numbers' in your program ...
Code:
// this could be in Global Scope ...
const double milesToKm = 1760*0.9144/1000; // since 0.9144 meters = 1 yard

int main()
{

   // ...

   double kilometers = miles * milesToKm;

   // ...
}
« Last Edit: September 05, 2010, 12:16:03 PM by David W » Logged

http://developers-heaven.net/forum/index.php/topic,46.0.html

http://sites.google.com/site/andeveryeyeshallseehim/home/he-comes
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!