6 Replies - 1597 Views - Last Post: 18 December 2010 - 12:56 PM Rate Topic: -----

#1 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

How do I error proof my code?

Posted 17 December 2010 - 11:39 PM

Hello again..

This should be a simple question.. I'll do my best to explain it.

Ok let say I have code that looks like this..

int loop2; 
do{
 cout<<"Here are your options:"<<endl;        
 cout<<"1: Deposit    \t"<<" 2: WithDraw"<<endl; 
 cout<<"3: Balance    \t"<<" 4: Loan"<<endl; 
 cout<<"5: Help       \t"<<" 6: Exit"<<endl; 
 cout<<"Please choise a number 1-5 or 6 to exit"<<endl;
 cout<<": ";
 cin>>loop2; 
 //Lets start the MASTER IF STATEMENTS    
     
     
   if(loop2==1){
   Deposit(line); 

  }  
  else if(loop2==2){
   Withdraw(line);

  }   
  else if(loop2==3){
   //balance 
  
  ifstream fin(line);
  if(!fin)
     {
     cout<<"Could not open account file!";
     cout<<endl; 
     cout<<"Press any button to continue...";
     cin.ignore(2); 
     }
     getline(fin, balance); 
     cout<<"Your balane: "<<balance<<endl; 
     fin.close(); 
  }   
  else if(loop2==4){
    Loan();
  }   
  else if(loop2==5){
    Help(); 

  }  
  else if (loop2==6)
  {
  cout<<"Hit any key to continue...";
  
  }
 else if(!loop2==1||2||3||4||5||6){
       cout<<"ERROR, Invaid entry! Press any button to Try again!!";
 cin.ignore(2); 
       
       }   
     
}while(loop2!=6);     



For the most part if you type any number outside of the range 6. You will get the error massage.. However, if you type in a charter of any kind you get an infinite loop...

What I want to know is how can I produce a question lock, that will only take in the input of the statements, that it supports?

One last bonus question. How can I set up a question line that can read the enter button.. Lets say the user doesn't type anything but presses enter anyway. I would like to make a program that can detect these kinds of problems, and display the proper error message..

Any solution code or web links would do a world of good for me, and my code.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I error proof my code?

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,362
  • Joined: 28-March 09

Re: How do I error proof my code?

Posted 17 December 2010 - 11:58 PM

how about if you just have the integers input as a character, and error check the range of the range of the ascii code of the choices since you don't do any mathematical ops with them.

else if(!loop2==1||2||3||4||5||6) //INCORRECT

else if(loop2 != 1 || loop2 != 2 || loop2 != 3 ||
        loop2 != 4 || loop2 != 5 || loop2 != 6)



and instead of a bunch of if statements, you can use switch-case

This post has been edited by IngeniousHax: 18 December 2010 - 12:02 AM

Was This Post Helpful? 2
  • +
  • -

#3 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: How do I error proof my code?

Posted 18 December 2010 - 12:14 AM

Thanks man! Let me test it out.. Btw If I hit any key outside of the switch statement range, it will produce an error??

I mean any key such as a number or string?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1770
  • View blog
  • Posts: 3,429
  • Joined: 30-May 10

Re: How do I error proof my code?

Posted 18 December 2010 - 01:56 AM

The main problem with using cin directly is all the cleanup you have to do when the user is being dumb (or worse, is hostile).

In a robust program, it is easier to separate input errors from conversion errors.
With say cin >> myInt;, you have both to contend with at the same time, and it gets really messy in a hurry to do it properly.

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
int main()
{
  string rawInput;
  while ( getline(cin,rawInput) ) {
    istringstream s(rawInput);
    int myVal;
    if ( s >> myVal ) {
      if ( myVal >= 1 && myVal <= 6 ) {
        cout << "A good integer " << myVal << endl;
      }
      else {
        cout << "Out of range " << myVal << endl;
      }
    }
    else {
      cout << "Not an integer" << endl;
    }
  }
  return 0;
}



A dumb user might try
$ ./a.out 
hello
Not an integer
124
Out of range 124
2
A good integer 2




A hostile user might try
$ ./a.out < a.out 
Not an integer
Not an integer
Not an integer
Not an integer
Not an integer


Feeding your program it's own binary image is a good way to throw random data at your program. It might complain a lot (as above), but it should NOT crash in any way.
Was This Post Helpful? 3
  • +
  • -

#5 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: How do I error proof my code?

Posted 18 December 2010 - 03:24 AM

YOU THE MAN Salem_c!!!T
Thanks for the information!!!
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6108
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: How do I error proof my code?

Posted 18 December 2010 - 05:33 AM

A nice set of tips on using C++ I/O.
Was This Post Helpful? 1
  • +
  • -

#7 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Re: How do I error proof my code?

Posted 18 December 2010 - 12:56 PM

not bad at all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1