hello fellow programmers.

I am having an anoying problem with a Contact book i am making. As the title of this thread suggests i am getting that error but only when i call for a specific function in the program called void search(). All the other functions in the class Adressbok excecute. This all worked fine until today when i was working on it and made some changes this message popped up. Tried to change back to make it work but it just doesnt want to.

I am a bit of a beginner att c++ so dont be to harsh about my code and this is not all finished yet if some parts look strange. Also when I run the debugger a window pops up called Disassebly with the text

00404514 push %ebp
00404515 mov %esp,%ebp
00404517 push %ebx
00404518 sub $0x14,%esp
0040451B mov 0x8(%ebp),%ebx
0040451E lea -0x60(%ebx),%eax
00404521 movl $0x1,-0x60(%ebx)
00404528 mov 0xc(%ebp),%edx
0040452B mov %edx,0x10(%eax)
0040452E mov 0x10(%ebp),%edx
00404531 mov %edx,0x14(%eax)
00404534 mov 0x47b050,%edx
0040453A mov (%edx),%edx
0040453C mov %edx,0x18(%eax)
0040453F mov 0x47b04c,%edx
00404545 mov (%edx),%edx
00404547 mov %edx,0x1c(%eax)
0040454A movl $0x432b2b00,0x40(%eax)
00404551 movl $0x474e5543,0x44(%eax)
00404558 movl $0x404578,0x48(%eax)
0040455F sub $0x20,%ebx
00404562 mov %ebx,(%esp)
00404565 call 0x41402c <_Unwind_SjLj_RaiseException>
0040456A mov %ebx,(%esp)
0040456D call 0x404d30 <__cxa_begin_catch>
00404572 call 0x4041ec <std::terminate()>
00404577 nop

This doesnt mean anything to me but i hope this can help you help me solve the problem.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int x;
class Adressbok
{
    private:
    string Namn;
    string Ort;
    string telefon_number;
    string e_mail;

    public:
    void skriv_in_data()
    {
        cin.ignore();
        cout << "Name: ";
        getline(cin,Namn);
        cout << "Location: ";
        getline(cin,Ort);
        cout << "E-mail: ";
        getline(cin,e_mail);
        cout << "Phonenumber: ";
        getline(cin,telefon_number);
    }
    void skriv_till_fil()
    {
    ofstream fil("Adressbok.txt", ios_base::in | ios_base::app);
       if(fil.is_open())
       {

           fil << Namn << "\n";
           fil << Ort << "\n";
           fil << telefon_number << "\n";
           fil << e_mail << "\n\n";
           fil.close();
       }
    }
    void skriv_context_av_adressbok()
    {
        ifstream fil("Adressbok.txt", ios_base::out);
        if(fil.is_open())
        {
            while(!fil.eof())
            {
                string output;
                getline(fil,output);
                cout << output << endl;
            }
        fil.close();
        }
    }
    void search()
    {
        int x=0;
        cin.ignore();
        string inmatning;
        cout << "Type in your search: ";
        getline(cin,inmatning);
        ofstream fil("Adressbok.txt", ios_base::out);
        if(fil.is_open())
        {
            while(getline(readfile, line))
            {

                string tomrad;
                getline(fil,tomrad);
                getline(fil,Namn);
                //int pos = Namn.find (' ');
                //string efternamn = Namn.substr(pos, 10);
                getline(fil,Ort);
                getline(fil,telefon_number);
                getline(fil,e_mail);
                if(Namn == sokning || Ort == sokning || telefon_number == sokning || e_mail == sokning)
                {
                    cout << Namn << endl;
                    cout << Ort << endl;
                    cout << telefon_number << endl;
                    cout << e_mail << endl;
                    cout << endl;
                    x++;
                }
            }

       if(x==0)
            cout << "Could not find your search" << endl;

        fil.close();
        }
    }
};

int main()
{
    do
    {
    system("CLS");
    cout << setw(50) << "**********************" << endl;
    cout << setw(50) << "* MIRZAS CONTACTBOOK *" << endl;
    cout << setw(50) << "**********************" << endl << endl;
    cout << setw(52) << "What would you like to do?" << endl << endl;
    cout << setw(45) << "1. Add a new person" << endl;
    cout << setw(35) << "2. Search" << endl;
    cout << setw(35) << "3. Delete" << endl;
    cout << setw(59) << "4. Look at contest of addressbook" << endl << endl;
    cout << setw(34) << "Choice: ";
    cin >> x;
    if(x == 1)
    {
        Adressbok add_new_person;
        add_new_person.skriv_in_data();
        add_new_person.skriv_till_fil();
        cin.get();
    }
    else if(x == 2)
    {

        Adressbok search_funktion;
        search_funktion.search();
        cin.get();
    }
    /*else if(x == 3)
    {

    }*/
    else if (x == 4)
    {
        Adressbok las_fran_fil;
        las_fran_fil.skriv_context_av_adressbok();
        cin.get();

    }
    else
    {
        cout << "Det går inte" << endl;
    }
}while(x > 0);
    return 0;
}

I have many annoying problems with this program...or my compiler has

testit.cpp: In member function ‘void Adressbok::search()’:
testit.cpp:68: error: ‘readfile’ was not declared in this scope
testit.cpp:68: error: ‘line’ was not declared in this scope
testit.cpp:72: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:73: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:76: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:77: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:78: error: cannot convert ‘std::ofstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
testit.cpp:79: error: ‘sokning’ was not declared in this scope

What compiler do you use? the two first errors can be ignored i was just trying something out in the while loop it should say (!fil.eof()). I dont understand these errors, any idea what they mean?

The main problem is that you need to get rid of header stdlib.h it corrupts the call to getline. One simple error is that your file in the search function should be of type "ifstream" not "ofstream", calling a getline on an ofstream will likely lead to an abnormal termination (usually due to two exceptions being thrown simultaneously, or worse).

With the above changes, I ran your program and it mostly works. I would also take out the line 70 (för där finns ingen tom rad melan personer i filen).

Thanks mike for the tips but i did all that and for some reasong I still got the error, but I managed to solve it somehow. I copied the code and started a new projekt and pasted it. Now it works perfectly. Dont really understand. Hopefully it will work from now on thanks all

That error seems to be caused by this code

int pos = Namn.find (' ');
string efternamn = Namn.substr(pos, 10);

Anyone know why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.