Compiling Problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 175
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Compiling Problems

 
0
  #1
Nov 14th, 2005
Can't get this to compile for some reason. Anyone have an idea what's going on with it?

  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <iomanip.h>
  4. #include <time.h>
  5.  
  6. void MoveT(int *tortoisePtr, const int End_race);
  7. void MoveHare(int *harePtr, const int End_race);
  8. void PrintCurrentPosition(const int *snapperPtr, const int *bunnyPtr, const int End_race);
  9.  
  10. int main()
  11. {
  12.  
  13. const int End_race=70;
  14. int tortoise=1,hare=1,timer=0;
  15.  
  16.  
  17.  
  18.  
  19. cout<<"BANG!!!"<<endl;
  20. cout<<"AND THEY'RE OFF !!!!"<<endl;
  21.  
  22. srand(time(NULL));
  23.  
  24.  
  25. while(tortoise != End_race && hare != End_race)
  26. {
  27. MoveT(&tortoise,End_race);
  28. MoveHare(&hare,End_race);
  29. PrintCurrentPosition(&tortoise,&hare,End_race);
  30. timer++;
  31. }
  32.  
  33. if(&tortoise>=&hare)
  34.  
  35. cout<<"tortoise wins: ";
  36.  
  37. else
  38. cout<<"hare wins: ";
  39.  
  40. cout<<timer<<endl;
  41.  
  42. return 0;
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. void MoveT(int *tortoisePtr, const int End_race)
  50.  
  51. {
  52. int ranNum;
  53.  
  54. ranNum = (rand()%10)+1 ;
  55.  
  56. if(ranNum>=1 && ranNum<=5)
  57.  
  58. *tortoisePtr += 3;
  59.  
  60. else if(ranNum == 6 || ranNum == 7)
  61. *tortoisePtr -= 6;
  62.  
  63. else
  64. *tortoisePtr += 1;
  65.  
  66.  
  67. if (*tortoisePtr<1)
  68. *tortoisePtr=1;
  69.  
  70. else if(*tortoisePtr>End_race)
  71. *tortoisePtr=End_race;
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. void MoveHare(int *harePtr, const int End_race)
  80. {
  81. int hrandNum;
  82.  
  83. hrandNum= (rand()%10)+1;
  84.  
  85. if(hrandNum ==3 || hrandNum ==4)
  86. *harePtr +=9;
  87.  
  88. else if(*harePtr==5)
  89. *harePtr -=12;
  90.  
  91. else if(hrandNum>=6 && hrandNum<=8)
  92. *harePtr +=1;
  93.  
  94. else if(hrandNum>8)
  95. *harePtr -=2;
  96.  
  97.  
  98. if(*harePtr<1)
  99. *harePtr=1;
  100.  
  101. else if(*harePtr>End_race)
  102. *harePtr=End_race;
  103. cout<<endl;
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. void PrintCurrentPosition(const int *snapperPtr, const int *bunnyPtr, const int End_race)
  112. {
  113. if(*snapperPtr == *bunnyPtr)
  114. cout<<setw(*bunnyPtr)<<"OUCH!!";
  115.  
  116. else if(*snapperPtr<*bunnyPtr)
  117. {
  118. cout<<setw(*bunnyPtr)<<'H';
  119. cout<<setw(*snapperPtr-*bunnyPtr)<<'T';
  120.  
  121. }
  122. else
  123. {
  124. cout<<setw(*snapperPtr)<<'T';
  125. cout<<setw(*bunnyPtr-*snapperPtr)<<'H'<<endl;
  126. cout<<endl;
  127. }
  128.  
  129. }
  130.  
  131. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,336
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Compiling Problems

 
0
  #2
Nov 14th, 2005
Originally Posted by kahaj
Can't get this to compile for some reason. Anyone have an idea what's going on with it?
Another victim of the "poorly indented code" bug: you have an extra closing } at the end of the file.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,674
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is online now Online
Posting Virtuoso

Re: Compiling Problems

 
0
  #3
Nov 14th, 2005
comment out one function at a time until you get back to a spot where it does compile. The last item you commented out probably has the error. Once you've found the function that's giving you trouble comment it all out to an empty body and start adding back one line/activity at a time to see what line is the likely culprit. As it stands I see several possible problems, but they should result in run time errors, not prevent compiling. Learning how to debug is as important as learning how to write code in the first place. Good luck.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 175
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Compiling Problems

 
0
  #4
Nov 14th, 2005
Okay, I cleaned up the code a bit and am still getting a compile error. Here's the code.

  1. #include <iostream>
  2. #include <stdlib>
  3. #include <iomanip>
  4. #include <time>
  5.  
  6. void MoveT(int *tortoisePtr, const int End_race);
  7. void MoveHare(int *harePtr, const int End_race);
  8. void PrintCurrentPosition(const int *snapperPtr, const int *bunnyPtr, const int End_race);
  9.  
  10. int main()
  11. {
  12.  
  13. const int End_race=70;
  14. int tortoise=1,hare=1,timer=0;
  15.  
  16. cout << "BANG!!!" << endl;
  17. cout << "AND THEY'RE OFF !!!!" << endl;
  18.  
  19. srand(time(NULL));
  20.  
  21.  
  22. while(tortoise != End_race && hare != End_race)
  23. {
  24. MoveT(&tortoise,End_race);
  25. MoveHare(&hare,End_race);
  26. PrintCurrentPosition(&tortoise,&hare,End_race);
  27. timer++;
  28. }
  29.  
  30. if(&tortoise>=&hare)
  31. cout << "TORTOISE WINS!!! YAY!!!" << endl;
  32.  
  33. else
  34. cout << "Hare wins. Yuch." << endl;
  35. cout << timer << endl;
  36.  
  37. return 0;
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44. void MoveT(int *tortoisePtr, const int End_race)
  45. {
  46. int ranNum;
  47. ranNum = (rand()%10)+1 ;
  48.  
  49. if(ranNum >= 1 && ranNum <= 5)
  50. *tortoisePtr += 3;
  51.  
  52. else if(ranNum == 6 || ranNum == 7)
  53. *tortoisePtr -= 6;
  54.  
  55. else
  56. *tortoisePtr += 1;
  57.  
  58. if (*tortoisePtr<1)
  59. *tortoisePtr=1;
  60.  
  61. else if(*tortoisePtr>End_race)
  62. *tortoisePtr=End_race;
  63. }
  64.  
  65.  
  66. void MoveHare(int *harePtr, const int End_race)
  67. {
  68. int hrandNum;
  69. hrandNum= (rand()%10) + 1;
  70.  
  71. if(hrandNum ==3 || hrandNum == 4)
  72. *harePtr += 9;
  73.  
  74. else if(*harePtr == 5)
  75. *harePtr -= 12;
  76.  
  77. else if(hrandNum >= 6 && hrandNum <= 8)
  78. *harePtr += 1;
  79.  
  80. else if(hrandNum > 8)
  81. *harePtr -= 2;
  82.  
  83. if(*harePtr < 1)
  84. *harePtr = 1;
  85.  
  86. else if(*harePtr > End_race)
  87. *harePtr = End_race;
  88. cout << endl;
  89. }
  90.  
  91.  
  92. void PrintCurrentPosition(const int *snapperPtr, const int *bunnyPtr, const int End_race)
  93. {
  94. if(*snapperPtr == *bunnyPtr)
  95. cout << setw(*bunnyPtr) << "OUCH!!";
  96.  
  97. else if(*snapperPtr < *bunnyPtr)
  98. {
  99. cout << setw(*bunnyPtr) << 'H';
  100. cout << setw(*snapperPtr - *bunnyPtr) << 'T';
  101. }
  102. else
  103. {
  104. cout << setw(*snapperPtr) << 'T';
  105. cout << setw(*bunnyPtr-*snapperPtr) << 'H' << endl;
  106. cout << endl;
  107. }
  108. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,336
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Compiling Problems

 
0
  #5
Nov 14th, 2005
Add this (for now, until you learn why not to).
#include <time>
using namespace std;

void MoveT(int *tortoisePtr, const int End_race);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Compiling Problems

 
0
  #6
Nov 14th, 2005
change the headers as
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>

include the following statement after the include statements
  1. using namespace std;
OR
append
  1. std::
before
  1. cout ,endl, setw
.
For ex.
  1. std::cout <<

Put a
  1. std::cin.ignore( );
before the return statement.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 175
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Compiling Problems

 
0
  #7
Nov 16th, 2005
Originally Posted by Dave Sinkula
Add this (for now, until you learn why not to).
#include <time>
using namespace std;

void MoveT(int *tortoisePtr, const int End_race);
I already have all of that code in there.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,336
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Compiling Problems

 
0
  #8
Nov 16th, 2005
Originally Posted by kahaj
I already have all of that code in there.
Is that a question?

Taking your last posted code I get errors. Changing the first few lines to this.
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>
  4. #include <ctime>
  5. using namespace std;
And it compiles and runs. This is my output.
  1. BANG!!!
  2. AND THEY'RE OFF !!!!
  3.  
  4. TH
  5.  
  6.  
  7. TH
  8.  
  9.  
  10. TH
  11.  
  12.  
  13. HT
  14. HT
  15. HT
  16. TH
  17.  
  18.  
  19. HT
  20. TH
  21.  
  22.  
  23. TH
  24.  
  25.  
  26. TH
  27.  
  28.  
  29. TH
  30.  
  31.  
  32. OUCH!!
  33. OUCH!!
  34. HT
  35. HT
  36. HT
  37. HT
  38. HT
  39. HT
  40. HT
  41. HT
  42. HT
  43. HT
  44. HT
  45. HT
  46. HT
  47. HT
  48. HT
  49. HT
  50. HT
  51. HT
  52. HT
  53. HT
  54. HT
  55. HTTORTOISE WINS!!! YAY!!!
  56. 36
  57.  
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC