943,602 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 740
  • C++ RSS
Nov 6th, 2008
0

Function call problem

Expand Post »
Hi guys, im trying to call a function with in another and for some reason i am getting


In function `int bDisplay()':
`move' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
In function `int move()':
`int move()' used prior to declaration

but the problem is that i have that function declared =(. What going on? Im making a memory tic tac toe game with AI and im fairly new into coding. So please don't flame my syntax =)

Also if it would be more clear to you guys if i posted everything i have right now please ask. Its not that long right now so it wouldnt take up that much space.

C++ Syntax (Toggle Plain Text)
  1. int bDisplay()
  2. {
  3. cout<<"\n\n\n\n\n";
  4. cout<<"\t\t\t\t"<<sOne<<" | "<<sTwo<<" | "<<sThree<<endl;
  5. cout<<"\t\t\t\t----------"<<endl;
  6. cout<<"\t\t\t\t"<<sFour<<" | "<<sFive<<" | "<<sSix<<endl;
  7. cout<<"\t\t\t\t----------"<<endl;
  8. cout<<"\t\t\t\t"<<sSeven<<" | "<<sEight<<" | "<<sNine<<endl;
  9. Sleep(2000);
  10. system("cls");
  11. move();
  12. }
  13.  
  14. int winCond()
  15. {
  16. if (sOne == 'x' && sTwo == 'x' && sThree == 'x'){win();}
  17. if (sOne == 'x' && sFour == 'x' && sSeven == 'x'){win();}
  18. if (sOne == 'x' && sFive == 'x' && sNine == 'x'){win();}
  19. if (sTwo == 'x' && sFive == 'x' && sEight == 'x'){win();}
  20. if (sThree == 'x' && sFive == 'x' && sSeven == 'x'){win();}
  21. if (sThree == 'x' && sSix == 'x' && sNine == 'x'){win();}
  22. if (sFour == 'x' && sFive == 'x' && sSix == 'x'){win();}
  23. if (sSeven == 'x' && sEight == 'x' && sNine == 'x'){win();}
  24. }
  25.  
  26.  
  27. int move()
  28. {
  29. bool repeat;
  30. char choice;
  31. winCond();
  32.  
  33. do {
  34.  
  35. repeat = false;
  36. cout<<"Please enter a selection 1-9:";
  37. cin>>choice;
  38.  
  39. switch (choice)
  40. {
  41. case 1:
  42. etc....
Similar Threads
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008
Nov 6th, 2008
0

Re: Function call problem

without the function call aswell it is hard to see what the problem is.

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Nov 6th, 2008
0

Re: Function call problem

Use a 2d array to represent the tic tac toe grid, not a seperate variable for each place >.< . And also...post the whole code. I don't understand why your calling wincond() at the start of main. It does literally nothing there. Also, never forget to initialize variables! Also, you are inputting a choice of type char, and are checking it against integers. While char and ints are very similar, the compiler does an ascii conversion for you in cases like this. If you want to check a char against an int, use single quotes '1' around it. If you don't, it will check against the ascii values of the char, whcih for 1 is 49 decimal, or 31 in hex. Ascii Table
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Nov 6th, 2008
0

Re: Function call problem

You do have function prototypes, and the prototype for move( ) agrees with your implementation?

Seeing your full program would help.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 6th, 2008
0

Re: Function call problem

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. int sOne = 1;
  7. int sTwo = 2;
  8. int sThree = 3;
  9. int sFour = 4;
  10. int sFive = 5;
  11. int sSix = 6;
  12. int sSeven = 7;
  13. int sEight = 8;
  14. int sNine = 9;
  15.  
  16. int win()
  17. {
  18. cout<<"gratz, you win the game!";
  19. return (0);
  20. }
  21.  
  22. int bDisplay()
  23. {
  24. cout<<"\n\n\n\n\n";
  25. cout<<"\t\t\t\t"<<sOne<<" | "<<sTwo<<" | "<<sThree<<endl;
  26. cout<<"\t\t\t\t----------"<<endl;
  27. cout<<"\t\t\t\t"<<sFour<<" | "<<sFive<<" | "<<sSix<<endl;
  28. cout<<"\t\t\t\t----------"<<endl;
  29. cout<<"\t\t\t\t"<<sSeven<<" | "<<sEight<<" | "<<sNine<<endl;
  30. Sleep(2000);
  31. system("cls");
  32. move();
  33. }
  34.  
  35. int winCond()
  36. {
  37. if (sOne == 'x' && sTwo == 'x' && sThree == 'x'){win();}
  38. if (sOne == 'x' && sFour == 'x' && sSeven == 'x'){win();}
  39. if (sOne == 'x' && sFive == 'x' && sNine == 'x'){win();}
  40. if (sTwo == 'x' && sFive == 'x' && sEight == 'x'){win();}
  41. if (sThree == 'x' && sFive == 'x' && sSeven == 'x'){win();}
  42. if (sThree == 'x' && sSix == 'x' && sNine == 'x'){win();}
  43. if (sFour == 'x' && sFive == 'x' && sSix == 'x'){win();}
  44. if (sSeven == 'x' && sEight == 'x' && sNine == 'x'){win();}
  45. }
  46.  
  47.  
  48. int move()
  49. {
  50. bool repeat;
  51. char choice;
  52. winCond();
  53.  
  54. do {
  55.  
  56. repeat = false;
  57. cout<<"Please enter a selection 1-9:";
  58. cin>>choice;
  59.  
  60. switch (choice)
  61. {
  62. case 1:
  63.  
  64. sOne = 'x';
  65. break;
  66.  
  67. case 2:
  68.  
  69. sTwo = 'x';
  70. break;
  71.  
  72. case 3:
  73.  
  74. sThree = 'x';
  75. break;
  76.  
  77. case 4:
  78.  
  79. sFour = 'x';
  80. break;
  81.  
  82. case 5:
  83.  
  84. sFive = 'x';
  85. break;
  86.  
  87. case 6:
  88.  
  89. sSix = 'x';
  90. break;
  91.  
  92. case 7:
  93.  
  94. sSeven = 'x';
  95. break;
  96.  
  97. case 8:
  98.  
  99. sEight = 'x';
  100. break;
  101.  
  102. case 9:
  103.  
  104. sNine = 'x';
  105. break;
  106.  
  107. default:
  108.  
  109. cout<<"Sorry, bad input, please choose again.";
  110. repeat = true;
  111. }
  112. }
  113. while (repeat);
  114. ai();
  115. }

Theres the full code right now. I didn't use arrays because of the simplicity of assigning new values to the int variables. But ya, their is no main() right now because im building the components needed first, before I attach them all to eachother. Again, i have not been programming for long, only a couple weeks, so take it easy on me =( Thanks!
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008
Nov 6th, 2008
0

Re: Function call problem

You will need a main before you can properly compile your program.

Because as of right now I have no idea of what your program is supposed to do. Also you need to have return statements for your functions since they are all declared int functionName
If you do not want to return anything remove the int and make it void functionName .
Reputation Points: 11
Solved Threads: 11
Junior Poster
kenji is offline Offline
145 posts
since May 2008
Nov 6th, 2008
0

Re: Function call problem

As I guessed, no function prototypes. The compiler sees your call of the move( ) function, but has no idea what that is, not having gottne that far in the code yet. Got back to your textbook or here for an explanation of what you need to do.

As kenji points out, you have at present only a collection of functions and some global variables (ooohh, you should be slapped on the wrist for that, too), but no actual program. Until you write function main( ), nothing will actually run.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 7th, 2008
0

Re: Function call problem

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. int sOne = 1;
  7. int sTwo = 2;
  8. int sThree = 3;
  9. int sFour = 4;
  10. int sFive = 5;
  11. int sSix = 6;
  12. int sSeven = 7;
  13. int sEight = 8;
  14. int sNine = 9;
  15.  
  16. int win()
  17. {
  18. cout<<"gratz, you win the game!";
  19. return (0);
  20. }
  21.  
  22. int bDisplay()
  23. {
  24. cout<<"\n\n\n\n\n";
  25. cout<<"\t\t\t\t"<<sOne<<" | "<<sTwo<<" | "<<sThree<<endl;
  26. cout<<"\t\t\t\t----------"<<endl;
  27. cout<<"\t\t\t\t"<<sFour<<" | "<<sFive<<" | "<<sSix<<endl;
  28. cout<<"\t\t\t\t----------"<<endl;
  29. cout<<"\t\t\t\t"<<sSeven<<" | "<<sEight<<" | "<<sNine<<endl;
  30. Sleep(2000);
  31. system("cls");
  32. move();
  33. }
  34.  
  35. int winCond()
  36. {
  37. if (sOne == 'x' && sTwo == 'x' && sThree == 'x'){win();}
  38. if (sOne == 'x' && sFour == 'x' && sSeven == 'x'){win();}
  39. if (sOne == 'x' && sFive == 'x' && sNine == 'x'){win();}
  40. if (sTwo == 'x' && sFive == 'x' && sEight == 'x'){win();}
  41. if (sThree == 'x' && sFive == 'x' && sSeven == 'x'){win();}
  42. if (sThree == 'x' && sSix == 'x' && sNine == 'x'){win();}
  43. if (sFour == 'x' && sFive == 'x' && sSix == 'x'){win();}
  44. if (sSeven == 'x' && sEight == 'x' && sNine == 'x'){win();}
  45. }
  46.  
  47.  
  48. int move()
  49. {
  50. bool repeat;
  51. char choice;
  52. winCond();
  53.  
  54. do {
  55.  
  56. repeat = false;
  57. cout<<"Please enter a selection 1-9:";
  58. cin>>choice;
  59.  
  60. switch (choice)
  61. {
  62. case 1:
  63.  
  64. sOne = 'x';
  65. break;
  66.  
  67. case 2:
  68.  
  69. sTwo = 'x';
  70. break;
  71.  
  72. case 3:
  73.  
  74. sThree = 'x';
  75. break;
  76.  
  77. case 4:
  78.  
  79. sFour = 'x';
  80. break;
  81.  
  82. case 5:
  83.  
  84. sFive = 'x';
  85. break;
  86.  
  87. case 6:
  88.  
  89. sSix = 'x';
  90. break;
  91.  
  92. case 7:
  93.  
  94. sSeven = 'x';
  95. break;
  96.  
  97. case 8:
  98.  
  99. sEight = 'x';
  100. break;
  101.  
  102. case 9:
  103.  
  104. sNine = 'x';
  105. break;
  106.  
  107. default:
  108.  
  109. cout<<"Sorry, bad input, please choose again.";
  110. repeat = true;
  111. }
  112. }
  113. while (repeat);
  114. ai();
  115. }

Theres the full code right now. I didn't use arrays because of the simplicity of assigning new values to the int variables. But ya, their is no main() right now because im building the components needed first, before I attach them all to eachother. Again, i have not been programming for long, only a couple weeks, so take it easy on me =( Thanks!


Hi,

Please write the body of move function before bDisplay() function. Your mistake is that
You are calling the move() function before, but you are defining its body after your calling function. Write your code in parent child top to bottom hierarchy. ok It will now compile and run successfully.okay

--
Regards,
Asif
Reputation Points: 10
Solved Threads: 0
Light Poster
asifjavaid is offline Offline
40 posts
since Oct 2006
Nov 7th, 2008
0

Re: Function call problem

ok, that makes some sense because i know ive dinked around calling functions before. thanks. Im also going to do some research on the prototype stuff.
Reputation Points: 23
Solved Threads: 3
Junior Poster
clutchkiller is offline Offline
181 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Program have one error :)
Next Thread in C++ Forum Timeline: input map coordinates into map





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC