The Geek Forum

  • April 28, 2024, 03:47:08 AM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Due to the prolific nature of these forums, poster aggression is advised.

*

Recent Forum Posts

Shout Box

Members
Stats
  • Total Posts: 129553
  • Total Topics: 7150
  • Online Today: 189
  • Online Ever: 1013
  • (January 12, 2023, 01:18:11 AM)

Author Topic: C++ For You++  (Read 4480 times)

Chris

  • Administrator
  • Hacker
  • *
  • Coolio Points: +286/-8
  • Offline Offline
  • Gender: Male
  • Posts: 3892
  • IT'S A TARP
    • View Profile
    • The Geekery
C++ For You++
« on: February 04, 2007, 10:55:37 PM »

I seem to be having a brain fart when it comes to designing this short program to convert centimeters to yards, feet, and inches. Below is the code that I have so far:
Code: [Select]
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int cent, inches, feet, yards;
cout<<"Enter the amount in centimeters:";
cin>>cent;
inches = cent / 2.54;
yards = inches / 36;
feet = inches % 36;
inches = feet / 12;
cout<<yards<<" Yards "<<feet<<" Feet and "<<inches<<" Inches\n";
     system("PAUSE");
    return EXIT_SUCCESS;
}

Anyone have any insignt on where I went wrong? I know it's something really stupid, like the conversion itself - not the syntax.

First person to post the correct solution/helpful advice leading to the correct answer gets 100 coolio points!
Logged

Vespertine

  • The VSUBjugator
  • Forum Moderator
  • Hacker
  • *
  • Coolio Points: +371/-38
  • Offline Offline
  • Posts: 1255
    • View Profile
Re: C++ For You++
« Reply #1 on: February 04, 2007, 11:30:10 PM »

Do you need a final semi-colon after your last curly brace?
Logged
I have come here to chew bubble gum and kick ass.  And, I'm all out of bubble gum.

Chris

  • Administrator
  • Hacker
  • *
  • Coolio Points: +286/-8
  • Offline Offline
  • Gender: Male
  • Posts: 3892
  • IT'S A TARP
    • View Profile
    • The Geekery
Re: C++ For You++
« Reply #2 on: February 04, 2007, 11:43:41 PM »

Nope. The syntax of that code is fine. It compiles and executes without any errors. It's just got a few logic errors here and there, I am sure.
Logged

BizB

  • Forum Moderator
  • Hacker
  • *
  • Coolio Points: +439/-15
  • Offline Offline
  • Gender: Male
  • Posts: 4324
  • Keep making circles
    • View Profile
Re: C++ For You++
« Reply #3 on: February 05, 2007, 07:44:46 AM »

inches = cent * 2.54;
yards = inches / 36;
feet = inches / 12;
inches = inches - ((yards * 36) + (feet*12));
« Last Edit: February 05, 2007, 07:57:05 AM by BizB »
Logged
Without me, it's just 'aweso'.

milifist

  • Troll
  • *
  • Coolio Points: +36/-1
  • Offline Offline
  • Posts: 233
  • Beam me up!
    • View Profile
    • HaXor Central
Re: C++ For You++
« Reply #4 on: February 05, 2007, 09:24:53 AM »

Wouldn’t it be better to use float?
The calculations would be much easier and far more accurate.

And what was the purpose of the second inches calculation?




#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
float cent, inches, feet, yards;
cout<<"Enter the amount in centimeters:";
cin>>cent;
inches = cent * 0.393700787;
yards = cent *  0.010936133 ;
feet = cent * 0.032808399;
cout<<yards<<" Yards "<<feet<<" Feet and "<<inches<<" Inches\n";
     system("PAUSE");
    return EXIT_SUCCESS;
}
Logged

BizB

  • Forum Moderator
  • Hacker
  • *
  • Coolio Points: +439/-15
  • Offline Offline
  • Gender: Male
  • Posts: 4324
  • Keep making circles
    • View Profile
Re: C++ For You++
« Reply #5 on: February 05, 2007, 09:55:25 AM »

I'm pretty sure that he's trying to get it to read:

Yards = Total number of complete yards
Feet = Remaining feet
Inches = Remaining inches

Such that 130 Centimeters converts to

Yards: 1
Feet: 1
Inches: 3


My first line above is wrong.  It was right with /  and not *.
« Last Edit: February 05, 2007, 09:57:24 AM by BizB »
Logged
Without me, it's just 'aweso'.

milifist

  • Troll
  • *
  • Coolio Points: +36/-1
  • Offline Offline
  • Posts: 233
  • Beam me up!
    • View Profile
    • HaXor Central
Re: C++ For You++
« Reply #6 on: February 05, 2007, 11:25:44 AM »

Ah, I see..



Then it should be something like this:

#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char *argv[])
{
float cent, inches, round_inches, feet, round_feet, yards, round_yards, holder;
cout<<"Enter the amount in centimeters:";
cin>>cent;
yards = cent *  0.010936133;
round_yards = floor(yards);
holder = yards - round_yards;
holder = holder * 91.44;
feet = holder * 0.032808399;
round_feet = floor(feet);
holder = feet - round_feet;
holder = holder * 30.48;
inches = holder * 0.393700787;
round_inches = floor(inches);

cout<<round_yards<<" Yards "<<round_feet<<" Feet and "<<round_inches<<" Inches\n";
     system("PAUSE");
    return EXIT_SUCCESS;
}
Logged

milifist

  • Troll
  • *
  • Coolio Points: +36/-1
  • Offline Offline
  • Posts: 233
  • Beam me up!
    • View Profile
    • HaXor Central
Re: C++ For You++
« Reply #7 on: February 05, 2007, 11:28:46 AM »

You should also add some logic to switch back-and-forth between singular and plural in the output.  8-)
Logged

BizB

  • Forum Moderator
  • Hacker
  • *
  • Coolio Points: +439/-15
  • Offline Offline
  • Gender: Male
  • Posts: 4324
  • Keep making circles
    • View Profile
Re: C++ For You++
« Reply #8 on: February 05, 2007, 12:05:35 PM »

TMTOWTDI
Logged
Without me, it's just 'aweso'.

milifist

  • Troll
  • *
  • Coolio Points: +36/-1
  • Offline Offline
  • Posts: 233
  • Beam me up!
    • View Profile
    • HaXor Central
Re: C++ For You++
« Reply #9 on: February 05, 2007, 12:41:35 PM »

Yeah, the right way and the wrong way!  :evil:


Or in this case with an input of 130 cm: the one program that outputs 1 yard, 1 foot, 3 inches and the other that outputs 1 yard, 4 feet, -33 inches.   :w:
Logged

BizB

  • Forum Moderator
  • Hacker
  • *
  • Coolio Points: +439/-15
  • Offline Offline
  • Gender: Male
  • Posts: 4324
  • Keep making circles
    • View Profile
Re: C++ For You++
« Reply #10 on: February 05, 2007, 01:39:47 PM »

 :slap

D'oh!

coffee++;
Logged
Without me, it's just 'aweso'.