4 Replies - 442 Views - Last Post: 05 February 2010 - 03:46 PM Rate Topic: -----

#1 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

help with build code error please

Posted 04 February 2010 - 08:53 PM

Hello,I am very new to using C and could use some help understanding where I went wrong. I am trying to build the following code.
#include <conio.h>
#include <stdio.h>

 main(void)
 
{
    void hanoi(char,char,char,int);
    char t1='A',t2='B',t3='C';
    int n;
    clrscr();
    printf("\n Enter the no. of disks on Tower A:");
    scanf("%d",&n);
    if(n<1)
    {
        printf("\n Nothing to move");
    }
    else
    hanoi(t1,t2,t3,n);
    getch();
}

void hanoi(char t1,char t2,char t3,int n)
{
    static int step=0;
    step++;
    printf("\n %c %c %c %d",t1,t2,t3,n);
    if(n==1)
    {
        printf("\n Move top disk from Tower %c ----> %c",t1,t2);
        return;
    }
    hanoi(t1,t3,t2,n-1);
    printf("\n %c %c %c %d",t1,t2,t3,n);
    printf("\n Move top disk from Tower %c ----> %c",t1,t2);
    printf("\n %c %c %c %d",t1,t2,t3,n);
    hanoi(t3,t2,t1,n-1);
    printf("\n %c %c %c %d steps=%d",t1,t2,t3,n,step);
}



I am getting an error that says failed to load and parse the manifest in Visual C++ 2008. I did read some similar posts but was not sure how they applied to my situation. By the way are there any easier to use compilers that work with simple C code for beginners like myself?
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: help with build code error please

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: help with build code error please

Posted 04 February 2010 - 09:59 PM

First of all, visual C++ can be very easy for compiling code, if you know how to create the project at the start. Create a new project and select "General" from the tree on the left. On the right pane select "Empty project" and click ok. Then after the project opens, on the left you will see three folders (Header files, Resource files, Source files). Right-click Source files and go to the Add >> New Item option. Add a C++ file and name it something descriptive and click ok. This will create a cpp file, but it is ok to put your C code into it. From there you can run it no problem and it will compile fine for you.

I think your error just stems from the wrong project type or included some kind of extension you shouldn't have.

Also make sure your main returns an int and looks something like this...

#include <conio.h>
#include <stdio.h>


int main() //<--- Notice that it returns an int
 
{
    void hanoi(char,char,char,int);
    char t1='A',t2='B',t3='C';
    int n;

    printf("\n Enter the no. of disks on Tower A:");
    scanf("%d",&n);
    if(n<1)
    {
        printf("\n Nothing to move");
    }
    else
    hanoi(t1,t2,t3,n);
    getch();

	return 0; //<---- Notice return statement
}

void hanoi(char t1,char t2,char t3,int n)
{
    static int step=0;
    step++;
    printf("\n %c %c %c %d",t1,t2,t3,n);
    if(n==1)
    {
        printf("\n Move top disk from Tower %c ----> %c",t1,t2);
        return;
    }
    hanoi(t1,t3,t2,n-1);
    printf("\n %c %c %c %d",t1,t2,t3,n);
    printf("\n Move top disk from Tower %c ----> %c",t1,t2);
    printf("\n %c %c %c %d",t1,t2,t3,n);
    hanoi(t3,t2,t1,n-1);
    printf("\n %c %c %c %d steps=%d",t1,t2,t3,n,step);
}



Look at the source code above and my two comments to show some changes. VC++ also doesn't support clrscr() so just do away with that command or it will error out. You are now ready to go! :)
Was This Post Helpful? 1
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: help with build code error please

Posted 04 February 2010 - 10:32 PM

I don't use it for my own projects but for help on DreamInCode I use Dev C++, and I never had a problem with it compiling C++ or C code.
Was This Post Helpful? 0
  • +
  • -

#4 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: help with build code error please

Posted 05 February 2010 - 01:01 PM

Thank you very much for the input. My code now runs. My next effort is to modify the code I want to output the following based on moving form peg one to three in Towers of Hanoi.
1 -> 3
1 -> 2
3 -> 2
1 -> 3
2 -> 1
2 -> 3
1 -> 3
My current output is:

Enter the number of disks on Peg 1:3

Move top disk from Tower 1 ----> 3
Move top disk from Tower 1 ----> 2
Move top disk from Tower 3 ----> 2
Move top disk from Tower 1 ----> 2
Move top disk from Tower 3 ----> 2
Move top disk from Tower 3 ----> 1
Move top disk from Tower 2 ----> 1

Here is the code I have so far.
#include <conio.h>
#include <stdio.h>

 int main(void)
 
{
    void hanoi(char,char,char,int);
    char p1='1',aux='2',p3='3';
    int n;

	printf("\n Enter the number of disks on Peg 1:");
    scanf("%d",&n);
    if(n<1)
    {
        printf("\n no disks to move");
    }
    else
    hanoi(p1,aux,p3,n);
    getch();

	return 0;
}

void hanoi(char p1,char aux,char p3,int n)
{
    static int step=0;
    step++;
       if(n==1)
    {
        printf("\n Move top disk from Tower %c ----> %c",p1,p3);
        return;
    }
    hanoi(p1,aux,p3,n-1);
        printf("\n Move top disk from Tower %c ----> %c",p1,aux);
       hanoi(p3,p1,aux,n-1);
    
}


I have looked over and even run some of the snippets from the snippet library but since there only about two pages in my book on recursion I am having a little trouble understanding where I have gone wrong. If you can help I would appreciate it.
Was This Post Helpful? 0
  • +
  • -

#5 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: help with build code error please

Posted 05 February 2010 - 03:46 PM

View Postnewcuser, on 05 February 2010 - 12:01 PM, said:

Thank you very much for the input. My code now runs. My next effort is to modify the code I want to output the following based on moving form peg one to three in Towers of Hanoi.
1 -> 3
1 -> 2
3 -> 2
1 -> 3
2 -> 1
2 -> 3
1 -> 3
My current output is:

Enter the number of disks on Peg 1:3

Move top disk from Tower 1 ----> 3
Move top disk from Tower 1 ----> 2
Move top disk from Tower 3 ----> 2
Move top disk from Tower 1 ----> 2
Move top disk from Tower 3 ----> 2
Move top disk from Tower 3 ----> 1
Move top disk from Tower 2 ----> 1

Here is the code I have so far.
#include <conio.h>
#include <stdio.h>

 int main(void)
 
{
    void hanoi(char,char,char,int);
    char p1='1',aux='2',p3='3';
    int n;

	printf("\n Enter the number of disks on Peg 1:");
    scanf("%d",&n);
    if(n<1)
    {
        printf("\n no disks to move");
    }
    else
    hanoi(p1,aux,p3,n);
    getch();

	return 0;
}

void hanoi(char p1,char aux,char p3,int n)
{
    static int step=0;
    step++;
       if(n==1)
    {
        printf("\n Move top disk from Tower %c ----> %c",p1,p3);
        return;
    }
    hanoi(p1,aux,p3,n-1);
        printf("\n Move top disk from Tower %c ----> %c",p1,aux);
       hanoi(p3,p1,aux,n-1);
    
}


I have looked over and even run some of the snippets from the snippet library but since there only about two pages in my book on recursion I am having a little trouble understanding where I have gone wrong. If you can help I would appreciate it.


I did it!!!! Oh yeah, feels good to solve this. Any suggestions from programmers for improvements?
   
      #include<stdio.h>
   
      #include<conio.h>

   
      void hanoi(int x, char from,char to,char aux)
   
      {
   
      if(x<1)
	  {
		  printf("No disks to move");
	  }
	  else

		  if(x==1)
   
      {
   
      printf("Move Disk From %c to %c\n",from,to);
  
      }
  
      else
  
      {
  
      hanoi(x-1,from,aux,to);
  
      printf("Move Disk From %c to %c\n",from,to);
  
      hanoi(x-1,aux,to,from);
 
      }
  
       
  
      }
  
      void main()
  
      {
  
      int disk;
  
  
      printf("Enter the number of disks you want to play with:");
  
      scanf("%d",&disk);
 
  
      hanoi(disk,'1','3','2');
  
      getch();
  
      }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1