0

So in the program I'm currently writing, the user is supposed to add people to a vector by inputting their names and partisan identities. However, I have been unable to make the code that actually stores the input work. The program first prompts the user for a name; then, once the user gives a name, it prompts the user again for a partisan identity. Whenever I enter more than one word for the name (e.g. "John Smith"), instead of accepting the input, the program throws this exception.

enter image description here

It also gives this error when I enter "D" or "R" in response to the second prompt, no matter how I respond to the first prompt. Does anyone have an idea what I'm doing wrong here? Here's the code I've written so far:

#include "DelibDem.h"
#include <stdio.h>
#include <vector>

//initializing variables
using namespace std;
bool continue_ = true;
string name = "";
string partyID = "";
int numD = 0;
int numR = 0;
int difference = 0;
int vectorSize = 0;
int newVectorSize = 0;
struct person{
    string Name;
    string PartyID;
    string equivalentName;
    string equivalenceClass;
};
vector<person> Sample;

int main()
{
    //user adds people to the vector. I have not yet implemented the code that actually adds the people specified by the user yet, because I am still trying
    //to figure out why my cin code is not working.
    while (continue_ == true) {
        string personName;
        string personPartyID;
        cout << "Enter a person's name: ";
        cin >> personName;
        cout << "Enter the person's party ID (D or R): ";
        cin >> personPartyID;
        if (personPartyID == "D") struct person inputtedPerson = { personName, personPartyID, NULL, "Republicans" };
        else struct person inputtedPerson = { personName, personPartyID, NULL, "Democrats" };
        cout << "Do you wish to add more people? (Y/N) ";
        string answer;
        cin >> answer;
        if (answer == "N") continue_ = false;
    }
    //The number of Democrats in the sample is stored in numD. The number of Republicans is stored in numR.
    for (auto& element : Sample)
    {
        if (element.PartyID == "D") numD++;
        else numR++;
    }
    //print the number of Democrats and Republicans
    cout << numD;
    cout << numR;
    //print the properties of each element in the sample
    for (auto& element : Sample)
    {
        cout << element.Name << endl;
        cout << element.PartyID << endl;
        cout << element.equivalentName << endl;
        cout << element.equivalenceClass << endl;
        cout << "\n";
    }
    return 0;
}

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 3
    That is not a strange exception. It looks like an access violation caused by a null pointer. – drescherjm Dec 01 '22 at 21:16
  • 6
    The `NULL` in `{ personName, personPartyID, NULL, "Republicans" }` and `{ personName, personPartyID, NULL, "Democrats" }` are a problem. If you want an empty string use `""`. – Retired Ninja Dec 01 '22 at 21:17
  • 1
    `if (personPartyID == "D") struct person inputtedPerson = { personName, personPartyID, NULL, "Republicans" }; else struct person inputtedPerson = { personName, personPartyID, NULL, "Democrats" };` can't work. The scope of these variables end on the same line. – drescherjm Dec 01 '22 at 21:18
  • `struct person inputtedPerson =` in c++ you don't need the `struct` after the declaration of the type `person` – drescherjm Dec 01 '22 at 21:20
  • 2
    The other issue you've discovered, although not related to the exception, is that `>>` stops at the first whitespace it encounters. If you want people to be able to enter a full name like `John Smith` then you should use `std::getline` to read that input. If you end up mixing `getline` and `>>` which I don't recommend you'll need to read this [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Retired Ninja Dec 01 '22 at 21:23
  • Thanks drescherjm and Retired Ninja! By replacing NULL with "" and swapping >> with getline, I was able to fix my problem. The code compiles correctly in G++ now (although not in Visual Studio because Visual Studio doesn't recognize the getline command for some reason. That is an issue with Visual Studio and not my code, since I have encountered that problem before and it hasn't impaired my programs' functionality when run using software other than Visual Studio.) – StanAtkinson Dec 01 '22 at 22:21
  • I use Visual Studio (Not VS Code) all day every day and have never had an issue with `getline`. If you can narrow it down to something specific you should ask another question about that with your updated code. – Retired Ninja Dec 01 '22 at 22:29
  • Please [edit] to convert your images of text into actual text or at least add a transcription. [See here](https://meta.stackoverflow.com/a/285557/11107541) for why. I'd also suggest reading [ask], which gives tips on how to write a descriptive title. Ex. a title that indicates what the error actually is instead of just "strange exception", and what circumstance led to it. It's not a trivial task, but it's an important one in writing a good question. – starball Dec 02 '22 at 04:40

0 Answers0