4 Replies - 1037 Views - Last Post: 08 April 2011 - 07:46 AM Rate Topic: -----

#1 mist4lyf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-April 11

how to terminate program input when certain invalid input is put in

Posted 07 April 2011 - 06:56 PM

My question is how do you do terminate a program input when a negative deposit or a letter is entered for the firstDeposit, secDeposit etc.

cout<< "Enter a select code: ";
cout<< "\n1 to enter deposits up to 5 years";
cout<< "\n2 to enter deposits up to 4 years";
cout<< "\n3 to enter deposits up to 3 years";
cout<< "\n4 to enter deposits up to 2 years";
cout<< "\n5 to enter deposits up to last year : ";
cin>>deposityear;

switch (deposityear)
{
       case 1:
            cout<< "\nEnter your deposits for five years ago: ";
            cin>> firstDeposit;
            cout<< "Enter your deposits for four years ago: ";
            cin>> secDeposit;
            cout<< "Enter your deposits for three years ago: ";
            cin>> thirdDeposit;
            cout<< "Enter your deposits for two years ago: ";
            cin>> fourthDeposit;
            cout<< "Enter your deposits for last year: ";
            cin>> fifthDeposit;
            cout<< "Enter your deposits for this year: ";
            cin>> sixthDeposit;
            total=((firstDeposit*(annualInterest*annualInterest*annualInterest*annualInterest*annualInterest))+(secDeposit*(annualInterest*annualInterest*annualInterest*annualInterest))+(thirdDeposit*(annualInterest*annualInterest*annualInterest))+(fourthDeposit*(annualInterest*annualInterest))+(fifthDeposit*annualInterest)+sixthDeposit);
            //the accumalated amount of these six deposits is assigned to the variable total
            cout<< "\nthe total of your deposits is " <<total<<endl;
            break;


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 08 April 2011 - 04:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how to terminate program input when certain invalid input is put in

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: how to terminate program input when certain invalid input is put in

Posted 07 April 2011 - 08:17 PM

First use code tags!!!!!!!!

If deposityear is a numeric type then to check for non-numeric entry you will need to check the status of your input stream. Since if the variable is a character that you try to insert into a integer or floating point value with an input stream (cin) will cause the input stream to set the failure flag.
cin >> intValue;
if(!cin) // Test the stream state. If failure do this:
{ // A failure means some type of invalid entry, usually a character instead of numeric value.
   // First clear the stream error
   cin.clear();
   // Now discard any garbage in the buffer
   cin.ignore(200,'\n');
   // Now you can handle this problem
}


If you want to check for a negative value, check for a negative value!

Jim

This post has been edited by jimblumberg: 07 April 2011 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 mist4lyf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-April 11

Re: how to terminate program input when certain invalid input is put in

Posted 08 April 2011 - 12:37 AM

Oh wow I have no idea what you meant ><. I think the code at the bottom has to do something with my terminating program input, but it still is confusing

#include<iostream>
using namespace std;


int main()
{
  int value;
  cout << "enter an integer: ";
  cin >> value;

  if(cin.eof()) cout << "EOF .. ";
  if(cin.fail()) cout << "FAIL .. ";
  cout << "value= " << value << endl;

  system("pause");
  return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 08 April 2011 - 04:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: how to terminate program input when certain invalid input is put in

Posted 08 April 2011 - 04:34 AM

When you post code, you need to follow the rules (IT'S EVERYWHERE ON THE PAGE!!!) and use CODE TAGS!

:code:

If that's too complicated for you, then read the first link in my signature.
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: how to terminate program input when certain invalid input is put in

Posted 08 April 2011 - 07:46 AM

View Postmist4lyf, on 08 April 2011 - 02:37 AM, said:

Oh wow I have no idea what you meant ><. I think the code at the bottom has to do something with my terminating program input, but it still is confusing

#include<iostream>
using namespace std;


int main()
{
  int value;
  cout << "enter an integer: ";
  cin >> value;

  if(cin.eof()) cout << "EOF .. ";
  if(cin.fail()) cout << "FAIL .. ";
  cout << "value= " << value << endl;

  system("pause");
  return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!


I like to use a function to get and validate numeric input ...

You might like to see the examples here ...

http://developers-he...index.php/topic,2019.0.html

or ... copy/paste b elow link into your web browser to go there ...
http://developers-heaven.net/forum/index.php/topic,2019.0.html

This post has been edited by David W: 08 April 2011 - 07:21 PM

Was This Post Helpful? -1
  • +
  • -

Page 1 of 1