Commit 0a09a212 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Exception handling example

parent 2865b909
#include "ArrayWrapper.hpp"
#include <iostream>
#include <exception>
using namespace std;
ArrayWrapper::ArrayWrapper()
: ARRAY_SIZE( 20 )
{
Clear();
}
void ArrayWrapper::Clear()
{
m_totalElements = 0;
for ( int i = 0; i < ARRAY_SIZE; i++ )
{
m_arr[i] = "";
}
}
void ArrayWrapper::Add( string item, int index )
{
// Error checking
if ( index < 0 || index >= 20 )
{
// Invalid index
throw out_of_range( "Index is out of range!" );
}
else if ( m_arr[index] != "" )
{
// Design error - There is already data here!
throw invalid_argument( "Item is already at that index!" );
}
m_arr[index] = item;
m_totalElements++;
}
void ArrayWrapper::Remove( int index )
{
// Error checking
if ( index < 0 || index >= 20 )
{
// Invalid index
throw out_of_range( "Index is out of range!" );
}
else if ( m_arr[index] == "" )
{
// Design error - There is already data here!
throw invalid_argument( "That index is already empty!" );
}
m_arr[index] = "";
m_totalElements--;
}
void ArrayWrapper::Display() const
{
for ( int i = 0; i < ARRAY_SIZE; i++ )
{
cout << i << ". " << m_arr[i] << endl;
}
}
#ifndef _ARRAY_WRAPPER_HPP
#define _ARRAY_WRAPPER_HPP
#include <string>
using namespace std;
class ArrayWrapper
{
public:
ArrayWrapper();
void Clear();
void Add( string item, int index );
void Remove( int index );
void Display() const;
private:
const int ARRAY_SIZE;
int m_totalElements;
string m_arr[20];
};
#endif
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Exception Handling" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Exception Handling" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Exception Handling" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="ArrayWrapper.cpp" />
<Unit filename="ArrayWrapper.hpp" />
<Unit filename="Menu.hpp" />
<Unit filename="main.cpp" />
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1598902381 source:/home/wilsha/TEACHING/EXAMPLE-CODE/Exception Handling/ArrayWrapper.cpp
"ArrayWrapper.hpp"
<exception>
<iostream>
1598902237 /home/wilsha/TEACHING/EXAMPLE-CODE/Exception Handling/ArrayWrapper.hpp
<string>
1598916420 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Exception Handling/ArrayWrapper.cpp
"ArrayWrapper.hpp"
<iostream>
<exception>
1598915690 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Exception Handling/ArrayWrapper.hpp
<string>
1598916548 source:/home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Exception Handling/main.cpp
"Menu.hpp"
<iostream>
"ArrayWrapper.hpp"
1586920927 /home/wilsha/TEACHING/PUBLIC/cs250-basic-data-structures-with-cpp/example-code/Exception Handling/Menu.hpp
<iostream>
<string>
<vector>
<cstdlib>
<limits>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="ArrayWrapper.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="-4" zoom_2="0">
<Cursor>
<Cursor1 position="849" topLine="8" />
</Cursor>
</File>
<File name="ArrayWrapper.hpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="368" topLine="0" />
</Cursor>
</File>
<File name="Menu.hpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="154" topLine="0" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="1" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="214" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
#ifndef _MENU
#define _MENU
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <limits>
using namespace std;
class Menu
{
public:
// OUTPUT
static void Header( const string& header )
{
DrawHorizontalBar( 80 );
string head = "| " + header + " |";
cout << " " << head << endl << " ";
DrawHorizontalBar( head.size() );
cout << endl;
}
static void DrawHorizontalBar( int width, char symbol = '-' )
{
for ( int i = 0; i < width; i++ )
{
cout << symbol;
}
cout << endl;
}
// MENUS and INPUT/OUTPUT
static void ShowMenu( const vector<string> options, bool vertical = true )
{
if ( vertical )
{
for ( unsigned int i = 0; i < options.size(); i++ )
{
cout << " " << (i+1) << ".\t" << options[i] << endl;
}
}
else
{
for ( unsigned int i = 0; i < options.size(); i++ )
{
cout << " " << (i+1) << ". " << options[i] << "\t";
}
cout << endl;
}
}
static int ShowIntMenuWithPrompt( const vector<string> options, bool vertical = true )
{
ShowMenu( options, vertical );
int choice = GetValidChoice( 1, options.size() );
return choice;
}
static string ShowStringMenuWithPrompt( const vector<string> options, bool vertical = true )
{
ShowMenu( options, vertical );
int choice = GetValidChoice( 1, options.size() );
string value = options[ choice-1 ];
return value;
}
static int GetValidChoice( int min, int max, const string& message = "" )
{
if ( message != "" )
{
cout << endl;
DrawHorizontalBar( message.size() + 2 );
cout << " " << message << endl;
}
int choice = GetIntChoice();
while ( choice < min || choice > max )
{
cout << "Invalid selection. Try again." << endl;
choice = GetIntChoice();
}
return choice;
}
static string GetStringChoice( const string& message = "" )
{
if ( message != "" )
{
cout << " " << message << endl;
}
cout << endl << " >> ";
string choice;
cin >> choice;
cin.ignore();
cout << endl;
return choice;
}
static string GetStringLine( const string& message = "" )
{
if ( message != "" )
{
cout << " " << message << endl;
}
string choice;
cout << endl << " >> ";
getline( cin, choice );
cout << endl;
return choice;
}
static int GetIntChoice( const string& message = "" )
{
if ( message != "" )
{
cout << " " << message << endl;
}
cout << endl << " >> ";
int choice;
cin >> choice;
cin.ignore();
cout << endl;
return choice;
}
// HANDY TRICKS
static void ClearScreen()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
system( "cls" );
#else
system( "clear" );
#endif
}
static void Pause()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
system( "pause" );
#else
cout << endl << " Press ENTER to continue..." << endl;
cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
#endif
}
static void PrintPwd()
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
system( "echo %cd%" );
#else
system( "pwd" );
#endif
}
};
#endif
#include "Menu.hpp"
#include <iostream>
using namespace std;
#include "ArrayWrapper.hpp"
int main()
{
bool done = false;
ArrayWrapper arr;
while ( !done )
{
Menu::Header( "MAIN MENU" );
int choice = Menu::ShowIntMenuWithPrompt( {
"Add item",
"Remove item",
"View all items",
"Quit"
} );
switch( choice )
{
case 1: // add item
{
Menu::Header( "Add item" );
int index;
string data;
cout << "Insert to what index? ";
cin >> index;
cout << "Enter word: ";
cin >> data;
try
{
arr.Add( data, index );
}
catch( out_of_range ex )
{
cout << "Out of range error: " << ex.what() << endl;
}
catch( invalid_argument ex )
{
cout << "Invalid argument error: " << ex.what() << endl;
}
}
break;
case 2: // remove item
{
Menu::Header( "Remove item" );
int index;
cout << "Insert to what index? ";
cin >> index;
try
{
arr.Remove( index );
}
catch( out_of_range ex )
{
cout << "Out of range error: " << ex.what() << endl;
}
catch( invalid_argument ex )
{
cout << "Invalid argument error: " << ex.what() << endl;
}
}
break;
case 3: // view all items
Menu::Header( "View all items" );
arr.Display();
break;
case 4: // quit
done = true;
break;
}
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment