online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#ifndef MSG_ENTER_NAME #define MSG_ENTER_NAME "please_enter_item_name \n" #endif #ifndef MSG_ENTER_PRICE #define MSG_ENTER_PRICE "please_enter_price\n" #endif // preprocessor directives macros #ifndef MSG #define MSG(X) #X; #endif // global identifiers // global constant identifiers // constants #include <iostream> // using namespaces // classes #include "file_missing.h" #include "empty.h" #include "list.h" using friend_global_funcs::operator<<; using friend_global_funcs::operator>>; int main() { // obj creation list :: list obj; ///////////////////////////////////////// // writing obj to file std::cout << MSG_ENTER_NAME; std::string temp_name; std::cin >> temp_name; std::cout << MSG_ENTER_PRICE; int temp_price; std::cin >> temp_price; std::cout << MSG(please_enter_quantity); std::cout << std::endl; short temp_quantity; std::cin >> temp_quantity; try { obj.set_name ( temp_name ); obj.set_price ( temp_price ); obj.set_quantity ( temp_quantity ); obj.set_created_file(); obj.ofs << obj; // protected code std::cout << "successfully written to file \n"; } catch ( class empty :: empty *e) { std::cout << (*e).what(); } catch ( class file_missing :: file_missing *e) { std::cout << (*e).what(); } catch ( std::exception e ) { std::cout << e.what(); } catch ( ... ) { std::cout << "couldn't write contact administrator \n"; } // finally so will execute otherwise std::cout << "end of try catch write \n"; /////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // reading into an obj try { obj.ifs >> obj; // protected code std::cout << "successfully read into obj\n"; std::cout << obj.get_name() << "\t"; std::cout << obj.get_quantity() << "\t"; std::cout << obj.get_price() << std::endl; } catch ( file_missing :: file_missing *e ) { std::cout << (*e).what(); } catch ( std::exception e ) { std::cout << e.what(); } catch (...) { std::cout << "couldn't read contact admin \n"; } // finally std::cout << "end of try catch read \n"; ////////////////////////////////////////////////// return 0; }
#ifndef STUDENT_PROJECTV1_LIST #define STUDENT_PROJECTV1_LIST #include <fstream> #include <string> namespace list { class list; } namespace friend_global_funcs { std::ofstream & operator << ( std::ofstream & , const list::list &) ; //const added here and keyword class removed from second parameter std::ifstream & operator >> ( std::ifstream & , list::list &) ;//keyword class removed from second parameter } namespace list { class list { private: std::string name; int price; short quantity; public: std::ofstream ofs; std::ifstream ifs; // file_mutators void set_created_file () noexcept(false) ; void set_readable_file (); // constructors list() noexcept ( noexcept ( set_created_file() ) ) ; list ( std::string , int , short ) noexcept(false) ; list ( class list &) noexcept ( noexcept ( set_created_file() ) ) ; // initialization to cover after construction of an obj void initialize ( std::string , int , short ) noexcept(false) ; // mutators void set_name ( std::string ) noexcept(false); void set_price ( int ) noexcept(false) ; void set_quantity ( short ) noexcept(false) ; // accessors std::string get_name ( ) const noexcept; int get_price () const noexcept; int get_quantity () const noexcept; // Enqueries bool check_created_file () const noexcept;//const added here bool check_opened_file(); // destructor ~list(); // friend global functions // overloaded operators friend std::ofstream & friend_global_funcs :: operator << ( std::ofstream & , const list &) ;//const added here friend std::ifstream & friend_global_funcs :: operator >> ( std::ifstream & , list &) ; }; } #endif
#include "list.h" #include"empty.h" #include "file_missing.h" namespace list{ // constructors list :: list () noexcept ( noexcept ( set_created_file() ) ) { // give 'em a valid obj // ,and a most common one name = "not_set" , price = 1 , quantity = 1; // not sure weather to call the set_created_file(); // or simply call this // ofs.open("list.txt", ios::app); } list :: list ( std::string name , int price , short quantity ) noexcept(false) { set_name ( name ); set_price ( price ); set_quantity ( quantity ); set_created_file(); } list :: list ( class list &r ) noexcept ( noexcept ( set_created_file() ) ) { name = r.name; price = r.price; quantity = r.quantity; // how to copy file location then? // ofs = r.ofs; set_created_file(); } //// // initialization to cover after construction of an obj void list :: initialize ( std::string name , int price , short quantity ) { set_name ( name ); set_price ( price ); set_quantity ( quantity ); set_created_file(); } //// // mutators void list :: set_name ( std::string name ) noexcept(false) { if ( name.empty() ) throw new empty::empty ( "name can not be left out enter something \n"); (*this).name = name; } void list :: set_price ( int price ) noexcept(false) { if ( !price ) throw new empty :: empty ( "price can not be zero \n" ); (*this).price = price; } void list :: set_quantity ( short quantity ) noexcept(false) { if ( !quantity ) throw new empty :: empty ( "quantity can not be zero \n" ); (*this).quantity = quantity; } ///// // file mutators void list :: set_created_file () noexcept(false) { if ( !ofs.is_open() ) ofs.open("student_list_file.txt", std::ios::app); if ( !ofs.is_open() ) throw new file_missing :: file_missing ( "file couldn't be created or opened \n" ); } void list :: set_readable_file () { if ( !ifs.is_open() ) ifs.open ( "student_list_file.txt" ); if ( !ifs.is_open() ) throw new file_missing :: file_missing ( "file couldn't be opened by set_readable_file function \n" ); } //// // accessors std::string list :: get_name () const noexcept { return name; } int list :: get_price () const noexcept { return price; } int list :: get_quantity () const noexcept { return quantity; } /// // enqueries bool list :: check_created_file () const noexcept{ return ofs.is_open(); } bool list :: check_opened_file (){ return ifs.is_open(); } // destructive list :: ~list() { // release resources // close file // close connection // release heap memory ofs.close(); ifs.close(); } }
#include "list.h" #include "file_missing.h" #include "empty.h" namespace friend_global_funcs { std::ofstream & operator<< (std::ofstream &ofs, const list::list &l) { //const added here if (!l.check_created_file()) throw new file_missing::file_missing( "can not write info to file something wrong with acquiring file in constructor of obj \n"); ofs << l.name << "\t" << l.price << "\t" << l.quantity << "\n"; //changed l.name() to l.name return ofs; } std::ifstream & operator>>(std::ifstream &ifs, list :: list &l) { l.set_readable_file(); if (!l.check_opened_file()) throw new file_missing::file_missing( "can't retrieve data cuz file is not associated with obj currently I'm in operated >> overloaded fuc \n"); ifs >> l.name >> l.price >> l.quantity; return ifs; } }
#ifndef STUDENT_PROJECTV1_EMPTY_H #define STUDENT_PROJECTV1_EMPTY_H #include <exception> namespace empty { class empty : public std::exception { private: const char * errors ; public: empty(); empty( const char * ); const char * what() const noexcept override; }; } #endif //STUDENT_PROJECTV1_EMPTY_H
#include "empty.h" namespace empty{ empty :: empty () { errors = "name can't be empty !\n"; } empty :: empty ( const char * errors ) { (*this).errors = errors; } const char * empty :: what () const noexcept { return errors; } }
#ifndef STUDENT_PROJECTV1_FILE_MISSING_H #define STUDENT_PROJECTV1_FILE_MISSING_H #include <exception> namespace file_missing { class file_missing : public std::exception { private: const char * errors; public: file_missing (); file_missing ( const char * ); const char * what() const noexcept override; }; } #endif
#include "file_missing.h" file_missing :: file_missing :: file_missing () { errors = "file is missing \n"; } file_missing :: file_missing :: file_missing ( const char * errors ) { (*this).errors = errors; } const char * file_missing :: file_missing :: what () const noexcept { return errors; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue