Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7434289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:48:42+00:00 2026-05-29T09:48:42+00:00

Ok so I’m doing a program for class when I run across a bug

  • 0

Ok so I’m doing a program for class when I run across a bug I’ve never seen before and no idea what to do about it with only minimal experience with using the debugger, so I’ve come here hoping someone here can set me down the path to fixing this bug. My bug is the access violation reading location. Here is the portion of code that seems to be giving me the error:

#include "Book.h"

using namespace std;

void add (char*, char*, int);
void remove (int&);
void list ();

int Count;



Book Bookshelf [4];

int main ()
{   
    char* In = "";
    char* N = "";
    char* A = "";
    int Y;
    int Num;

    do
    {
        cout << "Bookshelf> ";
        cin >> In;

        if (In == "add")
        {
            cout << "Bookshelf> Enter book: ";
            cin >> N >> A >> Y;
            add (N,A,Y);
        }

        else if (In == "remove")
        {
            cout << "Bookshelf> Select number: ";
            cin >> Num;
            remove (Num);
        }

        else if (In == "list")
        {
        }

    } while (cin != "quit");

    return 0;
}

void add (char* N, char* A, int Y)
{
    if (Bookshelf[4].IsEmpty() == false)
        cout << "Error!" << endl;
    else
    {
        Bookshelf[Count] = Book (N,A,Y);
        Count++;
    }
    cout << "Bookshelf> ";
}

I get the error when I type add into the command line to try to call the add function but it happens immediately so the debugger is no help to me. I know the problem is probably really simple but I can’t seem to find it. Any help you can offer would be greatly appreciated. Let me know if any more code samples are needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:48:43+00:00Added an answer on May 29, 2026 at 9:48 am

    You shouldn’t use char* unless you really know what you are doing. For example, I rarely use char* at all and I’m programming with C++ since about 20 years. You want to use std::string, e.g. like this:

    std::string In;
    if (std::cin >> In) { ... }
    

    The reason your code doesn’t work is that the input operator wants to store data at the location pointed to by your pointer. However, this pointer is pointing at immutable memory for a c-string literal. When the operator tries to store something at this location it immediately gets an access violation.

    The easiest fix is to use std::string. If you can’t use std::string for whatever reason, use a preallocated array of char. If you do this, make sure you tell the stream how much characters are available by setting up the width:

    char In[16];
    if (std::cin >> std::setw(sizeof(In)) >> In) { ... }
    

    (the reason I’m always using a check in these example is that it is very important that you always check whether your input was successful before you do anything with the result).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.