Jump to content

Teaching myself C++ need help with simple exercise

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Taoist

Taoist

    Newbie

  • Members
  • Pip
  • 7 posts
Hi.

I am reading Programming Principles and Practice using C++.

And one of the exercises is to create a simple mile to kilometer converter, which I did but...

I want to write the program to compensate for "bad input", so in short if someone types a letter value I want something like "please enter a number" to display. I was hoping someone could give me an example of how to do this with the code I have so far or radically correct me if need be with a whole new batch of code.

Below is the (working) code that I have written so far:


[B]#include "std_lib_facilities.h"

int main () {
double mile=1;
double kilo=1.609;
cout<<"Enter miles to convert to kilometers\n";
cin >>mile;
double answer= mile * kilo;
cout<< "That is equal to " << answer << " kilometers"<<"\n";


    return 0;
}[/B]

Edited by ZekeDragon, 15 March 2010 - 07:51 AM.
Please use [code] tags.


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Please use the
 tags when posting code. Paste your code in, highlight it, and click the # button. Makes it easier to read for us old guys. :)

As far as your invalid input problem:
[CODE]
cout << "Enter miles to convert to kilometers: ";
cin >> mile;
if( cin.fail() )
    cout << "You failed to follow my instructions." << endl;
Anyway, the rest of your code looks right. I would suggest a few things, though:
1) Instead of using a variable to store a constant value, use a #define statement, like so:
#define     kilo    1.609
...blah...
double answer = mile * kilo;
...blah...
2) Try not to use '\n' with the cout stuff. Better to use endl. At this point I don't expect you to understand nor care why (stream flushing), but if you want I'll explain.
3) Purely aesthetic note: If you're prompting the user for something, don't stick a newline after your prompt. Again, that's me just being anal-retentive. :)
sudo rm -rf /

#3
Taoist

Taoist

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks - I appreciate that.

I did what you said including the second part, but now when a non double is entered the output is

That is equal to 1.609 kilometers
You failed to follow my instructions.
Ideally it should only say

You failed to follow my instructions.

I tried experimenting with
else
but I couldn't figure it out.

here is the code in it's entirety

#include "std_lib_facilities.h"

int main () {
double mile=1;
#define     kilo    1.609
cout<<"Enter miles to convert to kilometers"<<endl;
cin >>mile;
double answer= mile * kilo;
cout<< "That is equal to " << answer << " kilometers"<<endl;
if( cin.fail() )
cout << "You failed to follow my instructions." << endl;


    return 0;
}
 


#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
if( cin.fail() )
cout << "You failed to follow my instructions." << endl;
You need to stick an exit line in there so it doesn't continue:
[FONT=Tahoma]
[/FONT]if( cin.fail() )
{
    cout << "You failed to follow my instructions." << endl;
    return -1;
}
Plus you stuck it in the wrong place. It should come directly after cin >> mile with no intervening lines.
sudo rm -rf /

#5
Taoist

Taoist

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks again.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
No problem! I take it that it works now?
sudo rm -rf /

#7
Taoist

Taoist

    Newbie

  • Members
  • Pip
  • 7 posts
Yes, I feel like I'm learning a lot really simply too. I'll be posting a lot on this forum.

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Oh, good. Gives me something to do when it's raining. :)
sudo rm -rf /