Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unable to run an example program.



  • I am a beginner. I tried little numbers (between 1 and 6. I tried names. I tried telephone numbers. No success. Maybe a second file is missing. Can somebody help me. Thanks a lot. Here is tje file :
    // example illustrating the use of std::list.

    // This is not an example of good style, just an illustration of facilities

    // pp 54-55, sec 3.7.3

    // No guarantees offered. Constructive comments to bs@research.att.com

    #include<list>
    #include<iostream>
    #include<string>

    using namespace std;

    struct Entry {
    string name;
    int number;
    Entry(const string& n, int i) :name(n), number(i) { }
    };

    list<Entry> phone_book;

    void print_entries()
    /*
    this kind of function should use a parameter,
    rather then a global name
    */
    {
    typedef list<Entry>::const_iterator LI;

    for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
    	const Entry& e = *i;	// reference used as shorthand
    		cout << '{' << e.name << ' ' << e.number << "}\n";
    }
    

    }

    void print_entry(const string& s)
    /*
    Is this the right treatment of a string not found?
    */
    {
    typedef list<Entry>::const_iterator LI;

    for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
    	const Entry& e = *i;	// reference used as shorthand
    	if (s == e.name) {
    		cout << e.name << ' ' << e.number << '\n';
    		return;
    	}
    }
    

    }

    void f(const Entry& e, list<Entry>::iterator i, list<Entry>::iterator p)
    /*
    just some nonsense code
    */
    {
    phone_book.push_front(e); // add at beginning
    phone_book.push_back(e); // add at end
    phone_book.insert(i,e); // add before the element referred to by `i'

    phone_book.erase(p);		// remove the element referred to by `p'
    

    }

    int main()
    {

    phone_book.push_back(Entry("one",1));
    phone_book.push_back(Entry("two",2));
    phone_book.push_back(Entry("three",3));
    phone_book.push_back(Entry("four",4));
    phone_book.push_back(Entry("five",5));
    Entry six("six",6);
    print_entries();
    f(six,phone_book.begin(),phone_book.begin());
    print_entries();
    print_entry("four");
    print_entry("seven");
    print_entry("three");
    

    }



  • @Sylas Hi! What is t you're trying to accomplish with your code? What do you want to do?



  • @Wieland Hi !
    I am a beginner. I tried little numbers (between 1 and 6. I tried names. I tried telephone numbers. No success. Maybe a second file is missing. Can somebody help me. Thanks a lot. Here is tje file :
    // example illustrating the use of std::list.

    // This is not an example of good style, just an illustration of facilities

    // pp 54-55, sec 3.7.3

    // No guarantees offered. Constructive comments to bs@research.att.com

    #include<list>
    #include<iostream>
    #include<string>

    using namespace std;

    struct Entry {
    string name;
    int number;
    Entry(const string& n, int i) :name(n), number(i) { }
    };

    list<Entry> phone_book;

    void print_entries()
    /*
    this kind of function should use a parameter,
    rather then a global name
    */
    {
    typedef list<Entry>::const_iterator LI;

    for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
    	const Entry& e = *i;	// reference used as shorthand
    		cout << '{' << e.name << ' ' << e.number << "}\n";
    }
    

    }

    void print_entry(const string& s)
    /*
    Is this the right treatment of a string not found?
    */
    {
    typedef list<Entry>::const_iterator LI;

    for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
    	const Entry& e = *i;	// reference used as shorthand
    	if (s == e.name) {
    		cout << e.name << ' ' << e.number << '\n';
    		return;
    	}
    }
    

    }

    void f(const Entry& e, list<Entry>::iterator i, list<Entry>::iterator p)
    /*
    just some nonsense code
    */
    {
    phone_book.push_front(e); // add at beginning
    phone_book.push_back(e); // add at end
    phone_book.insert(i,e); // add before the element referred to by `i'

    phone_book.erase(p);		// remove the element referred to by `p'
    

    }

    int main()
    {

    phone_book.push_back(Entry("one",1));
    phone_book.push_back(Entry("two",2));
    phone_book.push_back(Entry("three",3));
    phone_book.push_back(Entry("four",4));
    phone_book.push_back(Entry("five",5));
    Entry six("six",6);
    print_entries();
    f(six,phone_book.begin(),phone_book.begin());
    print_entries();
    print_entry("four");
    print_entry("seven");
    print_entry("three");
    

    }
    That is what the console shows. What shal I do for running that ?? Many thanks.



  • @Sylas said:

    That is what the console shows. What shal I do for running that ??

    What does the console show? What errors are you seeing?

    btw, I can't see what this has to do with Qt?



  • @KeithS Sorry . I made a mistake.
    {one 1}
    {two 2}
    {three 3}
    and so on (about 6 more lines) . I tried a copy-paste of my console without success.



  • @Sylas

    So what's the error? Is there any error?



  • @KeithS Thanks. No there is no error at all; I suppose it's a Stroustrup product. But i dont know what to do for making it run.


  • Lifetime Qt Champion

    You could use debugger


Log in to reply