Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I want to run this C++ code in visual studio 2010

#include<stdio.h>
#include<conio.h>
struct node
{
    int data;
    int count;
    struct node *right, *left;
}*root,*p,*q;

struct node *make(int y,int x)
{
    struct node *newnode;
    newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=y;
    newnode->count=x;
    newnode->right=newnode->left=NULL;
    return(newnode);
}

void left(struct node *r,int x)
{
    if(r->left!=NULL)
        printf("\n Invalid !");
    else
        r->left=make(x,y);
}

void right(struct node *r,int x)
{
    if(r->right!=NULL)
        printf("\n Invalid !");
    else
        r->right=make(x,y);
}


int postorder(struct node *r)
{
    int c=0;
    if(r!=NULL)
    {
        i=preorder(r->left);
        if(i!=0)
            ++c;
        i=preorder(r->right);
        if(i!=0)
            ++c;
        r->count=c1+c2;
        printf("\t %d \t %d",r->data,r->count);
    }
}


int i=0;
void main()
{
    int no1,no2;
    int choice;
    clrscr();
    printf("\n Enter the root:");
    scanf("%d",& no1);
    scanf("%d",& no2)
    root=make(no1,no2);
    p=root;
    while(1)
    {
        printf("\n Enter id and 0 number:");
        scanf("%d",& no1);
        scanf("%d",& no2);
        
        if(no1==-1)
            break;
        p=root;
        q=root;
        while(no1!=p->data && q!=NULL)
        {
            p=q;
            if(no1<p->data)
                q=p->left;
            else
                q=p->right;
        }
        if(no1<p->data)
        {
            printf("\n Left branch of %d is %d",p->data,no1);
            left(p,no1);
        }
        else
        {
            right(p,no1);
            printf("\n Right Branch of %d is %d",p->data,no1);
        }
    }
    while(1)
    {
        printf(" 3.Postorder Traversal \n 4.Exit");
        printf("\n Enter choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 3 :postorder(root);
                break;
            case 4 :exit(0);
            default:printf("Error ! Invalid Choice ");
                break;
        }
        getch();
    }

}
Posted
Updated 10-Dec-11 2:54am
v4
Comments
Albert Holguin 10-Dec-11 8:53am    
Added some formatting to the code.

Start your VS2010
Click on File - New Project - Other Languages - Visual C++ - Win 32 - Win32 Console Application

Enter a name for it and press Ok and Finish in next window.

Then replace _tmain content with your main and also add you other variables and functions.

Add necessary headers and if there is no error it will run.

Good Luck.
 
Share this answer
 
First, before you post any further code fragments here, please have the politeness to format them properly. Unindented code may be easy for you top read, but it wastes far too much of our time for most people to be bothered, myself included.

To run a basic C-type program in VS, create a new project, select C++, and then Win32 Console application.
You may need to call your Main function from the _tmain routine, or copy the code into it.
 
Share this answer
 
First of this code fragment will not even run with a regular C++ compiler.
Example:
C#
int postorder(struct node *r)
{
    int c=0;
    if(r!=NULL)
    {
        i=preorder(r->left);
        if(i!=0)
            ++c;
        i=preorder(r->right);
        if(i!=0)
            ++c;
        r->count=c1+c2; //Where is this c1 and c2 defined at least put the code fragment in context
        printf("\t %d \t %d",r->data,r->count);
    }
}


Anyways I can give you some hints:

Follow these steps:
1.Open Visual Studio
2.Create new project.
3.Select the Visual c++ win32 console template. (From the create new project dialog in the left side choose visual c++ and then from the right side choose win32 console)
4.Type the name of your project/program.
5.You'll get a file opened with contents like this:
C#
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

6.Remove everything from this except #include "stdafx.h"
7.Copy your whole code.
8.Next follow the next sequence of instructions given below.


After correcting the general programming errors you have to:
1.Use #include<malloc.h> for using the malloc function
2.For using clrscr(); use system("cls"); instead
 
Share this answer
 
Comments
Albert Holguin 10-Dec-11 17:40pm    
Good answer... +5

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