Click here to Skip to main content
15,913,836 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I cant identify what is going wrong in my c++ coding

there is no compiler error but as soon as I run my program it says MY PROGRAM HAS STOPPED WORKING.



i am just learning c++ and its my 14th day with c++

What I have tried:

C++
#include <iostream>

using namespace std;

struct node
{
    int number;
    node* next;
};
node*head=NULL;
bool emptylist(node* head)
{
    if(head==NULL)
    {
        return true;
    }
    else{
        return false;
    }
}

void insertfirstelement(node*temp,int numb)
{
    node*temp1= new node;
    temp->number=numb;
    temp->next=NULL;

    temp=head;
}
void insert_(node*temp,int numb)
{
    node*temp1=new node;
    temp1->number=numb;
    temp1->next=NULL;
    head->next=temp1;

}
int main()
{
if (emptylist)
{
    node*temp1;
    insertfirstelement(temp1,5);
    insert_(temp1,3);
    insert_(temp1,4);

}

cout << head->next->number;
}
Posted
Updated 10-Jun-16 3:17am
v5
Comments
Patrice T 9-Jun-16 14:34pm    
Give us real message "MY PROGRAM HAS STOPPED WORKING"
Sergey Alexandrovich Kryukov 9-Jun-16 17:27pm    
The "error message" (most likely, exception) is only informative when you see it under the debugger or catch exception and provide comprehensive exception information. To do so, you will have to program in C++, not C.

By the way, if you only have 2 weeks of C++ experience, it's only natural that you have some problems and bugs. Just don't give up. Use the debugger in case of any concern of your runtime.

—SA

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Your code have numerous problems, this line is a memory leak.
C++
node*temp1= new node;

Run your code line by line with the debugger, you will learn a lot.

Advice: Take a sheet of paper and note how is the list on start and how it evolve as you add new nodes, then check if your code match the changes.
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 9-Jun-16 17:28pm    
5ed.
—SA
Obviously, in your main you declare an uninitialized temp1 node and then use it in insertfirstelement function.

In C++, it is up to you to ensure that variable are initialized before they are used. Compiler might warns about obvious cases but won't do global analysis...

In fact, it look like the whole insertfirstelement function is improperly coded. Since the list is empty at that point, you have to create the head and you don't need to pass any node to that function.

I could give code but then you won't learn anything.

Also, as others have told you, it would be a good idea to use a debugger. Using such tools, you can find the root of such problem in less time than it took to write that question...
 
Share this answer
 
Comments
Member 12566145 11-Jun-16 0:00am    
why is it necessary to initialize temp1
Philippe Mori 12-Jun-16 19:04pm    
Because if you don't initialize a local variable in C++, it will have garbage value and then if you read that garbage value, then you have a pointer that point at some random location in memory and it might cause you program to crash.

In that case, it is very easy that problem in the code without even compiling it. Again, if you don't yet understand how code works, I would suggest you to use a debugger and follow the execution of code line by line.

By the way, if you are learning, you can also use a pencil and paper and try to "execute" the program on paper and then compare the actual working of the program with what you have written on paper. By doing such effort, you should be able to learn a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900