Thread: Little error but i can't even think of it

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    Little error but i can't even think of it

    Hello guys as i see this is a little error and i don't know how i can't fix it so the following are the main and the header file from the book i learn from:

    main:
    Code:
    #include "../std_lib_facilities.h"
    
    int main()
    try
    {
    	vector<int>numbers;
    	int number;
    
    	cout << "Enter the numbers (\'|\' for terminating it) : ";
    	while(cin >> number)
    		numbers.push_back(number);
    
    	int sum = 0;
    	int num = 0;
    
    	cout << "Enter the number of sums you want: ";
    	cin >> num;
    
    	for(int i = 0; i < numbers.size(); ++i)
    		sum+=numbers[i];
    
    	if(num >= numbers.size())
    		error("Sorry the number of sums you want is out of range!!!\n");
    
    	keep_window_open("exit");
    	return 0;
    }catch(exception& e){
    	cerr << "Error: " << e.what() << endl;
    	keep_window_open("exit");
    	return 1;
    }catch(...){
    	cerr << "Error: Unknown exception"<< endl;
    	keep_window_open("exit");
    	return 1;
    }
    Header file:
    Code:
    /*
    	simple "Programming: Principles and Practice using C++" course header to
    	be used for the first few weeks.
    	It provides the most common standard headers (in the global namespace)
    	and minimal exception/error support.
    
    	Students: please don't try to understand the details of headers just yet.
    	All will be explained. This header is primarily used so that you don't have
    	to understand every concept all at once.
    
    	Revised April 25, 2010: simple_error() added
    */
    
    #ifndef H112
    #define H112 201004L
    
    #include<iostream>
    #include<fstream>
    #include<sstream>
    #include<cmath>
    #include<cstdlib>
    #include<string>
    #include<list>
    #include<vector>
    #include<algorithm>
    #include<stdexcept>
    
    //------------------------------------------------------------------------------
    
    #ifdef _MSC_VER
    #include <hash_map>
    using stdext::hash_map;
    #else
    #include <ext/hash_map>
    using __gnu_cxx::hash_map;
    
    namespace __gnu_cxx {
    
        template<> struct hash<std::string>
        {
            size_t operator()(const std::string& s) const
            {
                return hash<char*>()(s.c_str());
            }
        };
    
    } // of namespace __gnu_cxx
    #endif
    
    //------------------------------------------------------------------------------
    
    #define unordered_map hash_map
    
    //------------------------------------------------------------------------------
    
    typedef long Unicode;
    
    //------------------------------------------------------------------------------
    
    using namespace std;
    
    template<class T> string to_string(const T& t)
    {
    	ostringstream os;
    	os << t;
    	return os.str();
    }
    
    struct Range_error : out_of_range {	// enhanced vector range error reporting
    	int index;
    	Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
    };
    
    
    // trivially range-checked vector (no iterator checking):
    template< class T> struct Vector : public std::vector<T> {
    	typedef typename std::vector<T>::size_type size_type;
    
    	Vector() { }
    	explicit Vector(size_type n) :std::vector<T>(n) {}
    	Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
    	template <class I>
    	Vector(I first, I last) :std::vector<T>(first,last) {}
    
    	T& operator[](unsigned int i) // rather than return at(i);
    	{
    		if (i<0||this->size()<=i) throw Range_error(i);
    		return std::vector<T>::operator[](i);
    	}
    	const T& operator[](unsigned int i) const
    	{
    		if (i<0||this->size()<=i) throw Range_error(i);
    		return std::vector<T>::operator[](i);
    	}
    };
    
    // disgusting macro hack to get a range checked vector:
    #define vector Vector
    
    // trivially range-checked string (no iterator checking):
    struct String : std::string {
    	
    	String() { }
    	String(const char* p) :std::string(p) {}
    	String(const string& s) :std::string(s) {}
    	template<class S> String(S s) :std::string(s) {}
    	String(int sz, char val) :std::string(sz,val) {}
    	template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }
    
    	char& operator[](unsigned int i) // rather than return at(i);
    	{
    		if (i<0||size()<=i) throw Range_error(i);
    		return std::string::operator[](i);
    	}
    
    	const char& operator[](unsigned int i) const
    	{
    		if (i<0||size()<=i) throw Range_error(i);
    		return std::string::operator[](i);
    	}
    };
    
    #ifndef _MSC_VER
    namespace __gnu_cxx {
    
        template<> struct hash<String>
        {
            size_t operator()(const String& s) const
            {
                return hash<std::string>()(s);
            }
        };
    
    } // of namespace __gnu_cxx
    #endif
    
    
    struct Exit : runtime_error {
    	Exit(): runtime_error("Exit") {}
    };
    
    // error() simply disguises throws:
    inline void error(const string& s)
    {
    	throw runtime_error(s);
    }
    
    inline void error(const string& s, const string& s2)
    {
    	error(s+s2);
    }
    
    inline void error(const string& s, int i)
    {
    	ostringstream os;
    	os << s <<": " << i;
    	error(os.str());
    }
    
    #if _MSC_VER<1500
    	// disgusting macro hack to get a range checked string:
    	#define string String
    	// MS C++ 9.0 have a built-in assert for string range check
    	// and uses "std::string" in several places so that macro substitution fails
    #endif
    
    template<class T> char* as_bytes(T& i)	// needed for binary I/O
    {
    	void* addr = &i;	// get the address of the first byte
    						// of memory used to store the object
    	return static_cast<char*>(addr); // treat that memory as bytes
    }
    
    
    inline void keep_window_open()
    {
    	cin.clear();
    	cout << "Please enter a character to exit\n";
    	char ch;
    	cin >> ch;
    	return;
    }
    
    inline void keep_window_open(string s)
    {
    	if (s=="") return;
    	cin.clear();
    	cin.ignore(120,'\n');
    	for (;;) {
    		cout << "Please enter " << s << " to exit\n";
    		string ss;
    		while (cin >> ss && ss!=s)
    			cout << "Please enter " << s << " to exit\n";
    		return;
    	}
    }
    
    
    
    // error function to be used (only) until error() is introduced in Chapter 5:
    inline void simple_error(string s)	// write ``error: s’’ and exit program
    {
    	cerr << "error: " << s << '\n';
    	keep_window_open();		// for some Windows environments
    	exit(1);
    }
    
    // make std::min() and std::max() accessible:
    #undef min
    #undef max
    
    #include<iomanip>
    inline ios_base& general(ios_base& b)	// to augment fixed and scientific
    {
    	b.setf(ios_base::fmtflags(0),ios_base::floatfield);
    	return b;
    }
    
    // run-time checked narrowing cast (type conversion):
    template<class R, class A> R narrow_cast(const A& a)
    {
    	R r = R(a);
    	if (A(r)!=a) error(string("info loss"));
    	return r;
    }
    
    
    inline int randint(int max) { return rand()%max; }
    
    inline int randint(int min, int max) { return randint(max-min)+min; }
    
    inline double sqrt(int x) { return sqrt(double(x)); }	// to match C++0x
    
    #endif
    compile the main file using this libraries(header file) and you'll see some thing strange really strange!!!

    Thank's

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    No. Don't make us compile your code to see the errors. Copy and paste the error you are getting!

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    it's not an syntax error it's runtime one and a hard thing to understand : the following program has to do the following:

    get some numbers into a vector by input
    terminate the input
    get the number of sums you want by input
    check (the number of sums) if it's less than the vector size
    printing the sum
    or if the number of sums is equal or bigger than vector size it'll print error message

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by XMasterrrr
    it's not an syntax error it's runtime one and a hard thing to understand
    However, hard to understand you may think it is, you know the expected behaviour and you experienced the actual behaviour, so tell us what they are (you already loosely described the expected behaviour, so that's a start). If undefined behaviour is involved, you may find that your claim of "you'll see some thing strange really strange" could turn out to be false for some people because everything works as expected, by chance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    for(int i = 0; i < numbers.size(); ++i)
    		sum+=numbers[i];
    
    	if(num >= numbers.size())
    		error("Sorry the number of sums you want is out of range!!!\n");
    Aren't you a little out of order here?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  2. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  3. Replies: 1
    Last Post: 01-11-2007, 05:22 PM
  4. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM
  5. fatal error C1001: INTERNAL COMPILER ERROR
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 12:07 PM