Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram breaks when trying to add string to structure
    primarykey
    data
    text
    <p>I just started to learn c++. I got problem with structure. When I am adding data to structure program beaks when it tries to add string to structure. I really don't know where is problem. here is my code:</p> <pre><code> #include "stdafx.h" #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; int main(void) { typedef struct hardware { int id; // will store information string name; int year; float price; hardware *next; // the reference to the next hardware }; hardware *head = NULL; //empty linked list int tempid = 0,tempyear=0, hardware_number = 0, counter = 0; string tempname=""; float tempprice=0; cout&lt;&lt;"Unesite sifru proizvoda:"; cin&gt;&gt;tempid; cout&lt;&lt;"Unesite naziv proizvoda:"; cin&gt;&gt;tempname; cout&lt;&lt;"Unesite godinu proizvoda:"; cin&gt;&gt;tempyear; cout&lt;&lt;"Unesite cijenu proizvoda:"; cin&gt;&gt;tempprice; cout&lt;&lt;"Unijeli ste : ID: "&lt;&lt;tempid&lt;&lt;", naziv: "&lt;&lt;tempname&lt;&lt;", godina: "&lt;&lt;tempyear&lt;&lt;", cijena: "&lt;&lt;tempprice&lt;&lt;", hardware No: " &lt;&lt;++counter; hardware *temp; temp = (hardware*)malloc(sizeof(hardware)); temp-&gt;id = tempid; temp-&gt;name = tempname; temp-&gt;year = tempyear; temp-&gt;price = tempprice; temp-&gt;next = head; head = temp; return 0; </code></pre> <p>EDIT 1: When I run program it compiles fine. After I enter data that will fill structure (id, name, price, year, ...) program breaks on this line</p> <pre><code>temp-&gt;name = tempname; </code></pre> <p>Here is error output: </p> <pre><code>An unhandled exception of type 'System.AccessViolationException' occurred in proba.exe Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload