0

how to solve this error ? when i run the program after i enter the userlist capacity and enter the user informartion it gives me this error in the delete_scalar.cpp file. enter image description here I am beginner so Any help would be apprciated. This my code i will list the cpp files below : user .cpp:enter code here

#include "user.h"
#include <iostream>
using namespace std;

User::User()
{
    name = "";
    age = 0;
    email = "";
    password = "";
    ++Count;
    id = Count;

}
User::User(string name1, int age1, string email1, string password1)
{
    name = name1;
    age = age1;
    email = email1;
    password = password1;
    ++Count;
    id = Count;

}

User::User(const User& u)
{
    name = u.name;
    age = u.age;
    email = u.email;
    password = u.password;
    ++Count; 
    id = u.id;
}
void User::setName(string name1)
{
    name = name1;
}
string User::getName()const
{
    return name;
}
void User::setPassword(string password1)
{
    password = password1;
}
string User::getPassword()const
{
    return password;
}
void  User::setEmail(string Email1)
{
    email = Email1;
}
string User::getEmail()const
{
    return email;
}
void User::setAge(int age1)
{
    age = age1;
}
int User::getAge()const
{
    return age;
}

void User::setId()
{
    id = Count;
    Count++;
}
int User::getId()const
{
    return id;
}
bool User::operator==(const User& u)
{
    bool status;
    if (name == u.name && age == u.age && email == u.email && id == u.id)
    {
        status = true;

    }
    else
    {
        status = false;

    }
    return status;
}
ostream& operator<<(ostream& os, const User& u)
{
    os << endl << "User name : " << u.name << " User id " << u.id << " User age " << u.age << " User email " << u.email << endl;
    return os;
}
istream& operator>>(istream& is, User& u)
{
    is >> u.name >> u.age >> u.email >> u.password;
    return is;


}

book.cpp:

#include "book.h"


book::book()
{
    title = "";
    isbn = "";
    ++Count1;
    id = Count1;
    category = "";
    averageRating = 0;
}
book::book(string title1, string isbn1, string category1, User author1)
{
    title = title1;
    isbn = isbn1;
    category = category1;
    author = author1;
    ++Count1;
    id = Count1;


}
book::book(const book& b)
{
    title = b.title;
    isbn = b.isbn;
    category = b.category;
    author = b.author;
    ++Count1;
    id = Count1;


}
void book::setTitle(string title1)
{
    title = title1;

}
string book::getTitle()const
{
    return title;
}
void book::setIsbn(string isbn1)
{
    isbn = isbn1;

}
string book::getIsbn()const
{
    return isbn;
}
void book::setCategory(string category1)
{
    category = category1;

}
string book::getCategory()const
{
    return category;
}
void book::setAuthor(User author1)
{
    author = author1;

}
User book::getAuthor()const
{
    return author;
}
void book::rateBook(int rating)
{
    ++numRates;
    sumRates += rating;

    averageRating = sumRates / numRates;


}
bool book:: operator ==(const book& b)
{
    bool status;
    if (title == b.title && isbn == b.isbn && category == b.category && author == b.author && id == b.id)
    {
        status = true;

    }
    else
    {
        status = false;

    }
    return status;
}

 ostream& operator<<(ostream& os, const book& b)
{

    os << endl << "book title : " << b.title << " book id " << b.id << " book isbn " << b.isbn << " book category " << b.category << " book average rating " << b.averageRating << endl;
    return os;
}
 istream& operator>>(istream& is, book& b)
 {
     is >> b.title >> b.isbn >> b.category;
     return is;


 }

Userlist.cpp:

#include "UserList.h"
#include <iostream>
using namespace std;
UserList::UserList(int capacity1) 
{
    
    capacity = capacity1;
    users = new User[capacity];
    usersCount = 0;
    User userdef;
    for (int i = 0; i < capacity; i++) 
    {
        users[i] = userdef;
        


    }
    

}

void UserList::addUser(User u) 
{
    
    users[usersCount] = u;
    ++usersCount;
}
User& UserList::searchUser(string name)
{ 
    cin >> name;
    for (int i = 0; i < capacity; i++) 
    {
        
        if (name==users[i].getName() )
        {
            return users[i];
        
        }

    }
}



User& UserList::searchUser(int id)
{
    cin >> id;
    for (int i = 0; i < capacity; i++)
    {

        if (id = users[i].getId())
        {
            return users[i];

        }

    }
}

void UserList::deleteUser(int id)
{
    cin >> id;
    int i;
    for ( i =0; i < usersCount; i++)
    {
        break;
    }
    for (i;i<usersCount;i++) 
    {
        users[i + 1] = users[i];

    }
    usersCount--;
}

UserList::~UserList()
{
    delete users;
}


ostream& operator<<(ostream& output, UserList& userList)
{
    int capacity2=userList.capacity;
    for (int i = 0; i < capacity2; i++)
    {
        output << userList.users[i]<<endl;
    }
    return output;
}

source.cpp:

#include <iostream>
using namespace std;
#include "User.h"
#include "book.h"
#include "UserList.h"


int User::Count = 0;
int book::Count1 = 0;

int main()
{
    int capacity1;
    cout << "enter capacity" << endl;
    cin >> capacity1;
    UserList u(capacity1);
    User u1;
    cout << "Let's add a user" << endl;
    cout << "Enter the user info in this order" << endl;
    cout << "Name || Age || Email || Password" << endl;
     
   cin>> u1;
   u.addUser(u1);
   cout << u;
      return 0;
}

output screen:

enter image description here

  • You should learn how to debug your app. As you are using Visual Studio, press F5 then it will launch your app in a debugger – Asesh Dec 22 '20 at 15:47
  • Use the "Stack Frame" combo box on the debug toolbar to switch to your code. – drescherjm Dec 22 '20 at 15:47
  • In deleteUser you have: `users[i + 1] = users[i];` looks backwards to me. Shouldn't it be `users[i] = users[i+1];` – drescherjm Dec 22 '20 at 15:49
  • `for ( i =0; i < usersCount; i++) { break; }` what is the point of a loop where you exit on the first iteration. `void UserList::deleteUser(int id)` then `cin >> id;` Why do you pass an `id` to the function by value if you are going to prompt the user for input of that value. – drescherjm Dec 22 '20 at 15:52
  • @drescherjm thanks for help but the main problem is still there – Adham Ahmad Dec 22 '20 at 15:58
  • @drescherjm thanks for help but the program is not finished yet this function will take the id from the user of the user we want to delete and cascade to the left – Adham Ahmad Dec 22 '20 at 16:00
  • @Asesh this is the debugger output i don't understand it : HEAP["file name".exe]: Invalid address specified to RtlValidateHeap( 00670000, 0067FDCC ) "file name".exe has triggered a breakpoint. – Adham Ahmad Dec 22 '20 at 16:38
  • `users[i + 1] = users[i];` can go out of bounds. – drescherjm Dec 22 '20 at 17:18
  • Your UserList class also violates the rule of 3 / 5 /0 however if you don't use assignment or pass it to a function by value you can get away with this. [https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – drescherjm Dec 22 '20 at 17:24
  • 1
    There is a possibility that the array `users[i + 1] = users[i];` is out of bounds. I suggest you could modify to `for (i;i – Barrnet Chou Dec 23 '20 at 02:39

0 Answers0