-2

Please help me understand what I'm doing wrong here.

Struct is

struct node
{
int value;
stack <int>losersStack;
};

Main is

int main()
{
    int size;
    printf("Enter size of arrary\n");
    scanf("%d", &size);
    node *arr = new node[size];
    for(int i=0; i<size; i++)
    {
        printf("Enter a value\n");
        scanf("%d", arr[i].value );
    }
}

I get an error during the value entering. "Unhandled exception at 0x55e5effe (msvcr100d.dll) in blabla: 0xC0000005: Access violation writing location 0xcdcdcdcd."

Thanks in advance!

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Ran Lavi
  • 25
  • 6
  • 2
    Why are you using `scanf` in C++? – Barmar Mar 27 '18 at 18:36
  • 3
    `scanf("%d", &arr[i].value);` – Barmar Mar 27 '18 at 18:36
  • @Barmar I'm more used to it than to cout.. Is that part of the issue? – Ran Lavi Mar 27 '18 at 18:37
  • 2
    You seem to know how to use `scanf` when you get the size (but why use `scanf` in the first place in C++?), but then you *forget* how to use it in the loop? – Some programmer dude Mar 27 '18 at 18:37
  • 1
    If you're more used to it, then you should know that you need to give the address of the variable. – Barmar Mar 27 '18 at 18:37
  • There are no pointers in the struct. – Barmar Mar 27 '18 at 18:38
  • @Rosh You mean `cin`, don't you? – Barmar Mar 27 '18 at 18:38
  • Firstly, this is more C than C++, and I would recommend choosing one over the other since modern C++ code style diverges significantly from C. Also, if it helps, `0xCDCDCDCD` is a sign that your compiler has inserted code to mark memory as being unitialized. [see more on magic numbers here](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – alter_igel Mar 27 '18 at 18:38
  • @Barmar OMG!! I was wondering why it worked a few hours ago and now it started with this error! Probably in one of my edits I've erased that by mistake! Thank you so much! – Ran Lavi Mar 27 '18 at 18:39
  • @Some programmer dude That's what happens when you work on the same code for too many hours.. You start changing and skipping stuff.. Lost my focus – Ran Lavi Mar 27 '18 at 18:43
  • @alter igel Thanks! I already saw it in under a question somebody else posted. But I was sure that the mistake is before the loop for some reason, with the "new node". Got totally off guard with the "&" – Ran Lavi Mar 27 '18 at 18:45
  • @Rosh Then it's time for either coffee, coke, or bed. Perhaps a coke while finishing up and then bed? Or at least go outside and get some fresh air, walk of your tensions, clear your mind. Always good to do a couple of times a day. :) – Some programmer dude Mar 27 '18 at 18:47
  • @Some programmer dude You're definitely right. I haven't even been outside today, and it's 10PM. I just really wanted to finish writing several codes by midnight and submit on time. But I guess I'll take the penalty of submitting late, cause in the current concentration state, I'll do more harm than good. Thanks dude! – Ran Lavi Mar 27 '18 at 19:03

2 Answers2

3

You need to give the address of the variable:

scanf("%d", &arr[i].value);

But since you're writing C++, it's easier to use cin:

cin >> arr[i].value;
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

In the code you posted, you need an ampersand & before the arr expression.

scanf("%d", &arr[i].value );

But don't use all that low level stuff. In particular, avoid using new. Here (minus error-checking on the input) is the C++ way to do it.

#include <iostream>
#include <stack>
#include <vector>
struct node
{
int value;
std::stack <int>losersStack;
};

int main()
{
    std::cout <<  "Enter size of arrary\n";
    int size;
    std::cin >> size;
    std::vector<node> arr(size);
    for(int i=0; i<size; i++)
    {
        std::cout << "Enter a value\n";
        std::cin >> arr[i].value;
    }
}
Jive Dadson
  • 16,680
  • 9
  • 52
  • 65