943,605 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1879
  • C++ RSS
Nov 2nd, 2006
0

Function Back to Main

Expand Post »
Greetings:

A simple question that has me stuck: Program runs, calls function, function runs... Return to Main? This last area is where it fails. No errors, just no control returned to Main.cpp at end of function run. I've been studying the code much and researching the solution on the Net all day.

I swear there is one (1) too many "}" in Look.cpp, but it is not detected by the compiler. (?)

Any point in the right direction would be vastly appreciated-- I do not want the answer given to me directly at this point, I want to figure it out myself. Perhaps just a hint, a link to a relevant Web resource, etc?

Thank-you in advance, everyone.

Look.h
C++ Syntax (Toggle Plain Text)
  1. class Look {
  2. public:
  3. float shipPos ;
  4. float satPos;
  5. float interior;
  6.  
  7. void printShipPos(); // new function declaration
  8.  
  9. Look();
  10. ~Look();
  11. };
Main.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "Look.h"
  3. using namespace std;
  4.  
  5. int choice;
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11.  
  12.  
  13. std::cout << "________________________________"<< endl;
  14. std::cout << ""<< endl;
  15. std::cout << "Welcome to Platform XYZ-- Please Enter a Number:"<< endl;
  16. std::cout << ""<< endl;
  17. std::cout << "[1]Check Location for Transmission to Base"<< endl;
  18. std::cout << "[2]Stub"<< endl;
  19. std::cout << ""<< endl;
  20. std::cout << "________________________________"<< endl;
  21.  
  22.  
  23.  
  24. cin >> choice;
  25.  
  26. Look look1; //** ERROR
  27.  
  28.  
  29. if(choice == 1){
  30. look1.printShipPos(); //** ERROR
  31. }
  32. else {
  33. std::cout << "OTHER! chosen (stub)"<< endl;
  34. }
  35.  
  36.  
  37. system("PAUSE");
  38.  
  39. return 0;
  40. }

Look.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <ctime>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include "Look.h"
  5. using namespace std;
  6.  
  7.  
  8. int countFlag = 0;
  9.  
  10. Look::Look() {
  11. }
  12.  
  13. Look::~Look() {
  14. }
  15.  
  16. void Look::printShipPos()
  17. {
  18. srand((unsigned)time(0)); //RNG
  19. float shipPos;
  20. for(int index=0; index<1; index++){
  21. shipPos = (rand()%24)+7;
  22. cout << shipPos << endl;
  23. countFlag = 1; //test
  24. cout << countFlag << endl; //test
  25.  
  26.  
  27. if (shipPos <= 3.80) {
  28. std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
  29. }
  30. else if (shipPos <= 7.60) {
  31. std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
  32. }
  33. else if (shipPos <= 11.40) {
  34. std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl;
  35. }
  36. else if ((shipPos >= 15.20) && (shipPos < 19.00)) {
  37. std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl;
  38. }
  39. else if ((shipPos >= 19.00) && (shipPos < 22.80)){
  40. std::cout << "La Paz, Baja, Mexico [In Transmission Range]"<< endl;
  41. }
  42. else if ((shipPos >= 22.80) && (shipPos < 26.60)){
  43. std::cout << "Gulf of Mexico [In Transmission Range]"<< endl;
  44. }
  45. else if ((shipPos >= 26.60) && (shipPos < 30.40)){
  46. std::cout << "Tampa, Florida [In Transmission Range]"<< endl;
  47. }
  48. else if ((shipPos >= 30.40) && (shipPos < 34.20)){
  49. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 4320 miles from Lisbon, Portugal"<< endl;
  50. }
  51. else if ((shipPos >= 34.20) && (shipPos < 38.00)){
  52. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 3240 miles from Lisbon, Portugal"<< endl;
  53. }
  54. else if (shipPos <= 38.00) {
  55. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 2160 miles from Lisbon, Portugal"<< endl;
  56. }
  57. else if (shipPos <= 41.80) {
  58. std::cout << "Canary Islands [Out of Transmission Range]"<< endl;
  59. }
  60. else if (shipPos <= 45.60) {
  61. std::cout << "Lisbon, Portugal [Out of Transmission Range]"<< endl;
  62. }
  63. else if (shipPos <= 49.40) {
  64. std::cout << "Tripoli, Libya [Out of Transmission Range]"<< endl;
  65. }
  66. else if (shipPos <= 53.20) {
  67. std::cout << "Baghdad, Iraq [Out of Transmission Range]"<< endl;
  68. }
  69. else if (shipPos <= 57.00) {
  70. std::cout << "Eastern Iran [Out of Transmission Range]"<< endl;
  71. }
  72. else if (shipPos <= 60.80) {
  73. std::cout << "Central Tajikistan [Out of Transmission Range]"<< endl;
  74. }
  75. else if (shipPos <= 64.60) {
  76. std::cout << "Kathmandu, Nepal [Out of Transmission Range]"<< endl;
  77. }
  78. else if (shipPos <= 68.40) {
  79. std::cout << "Hanoi, Vietnam [Out of Transmission Range]"<< endl;
  80. }
  81. else if (shipPos <= 72.20) {
  82. std::cout << "Hong Kong, China [Out of Transmission Range]"<< endl;
  83. }
  84. else if (shipPos <= 76.00) {
  85. std::cout << "Pacific (open waters) [Out of Transmission Range] 5400 miles from French Polynesia"<< endl;
  86. }
  87. else if (shipPos <= 79.80) {
  88. std::cout << "Pacific (open waters) [Out of Transmission Range] 4320 miles from French Polynesia"<< endl;
  89. }
  90. else if (shipPos <= 83.60) {
  91. std::cout << "Pacific (open waters) [Out of Transmission Range] 3240 miles from French Polynesia"<< endl;
  92. }
  93. else if (shipPos <= 87.40) {
  94. std::cout << "Pacific (open waters) [Out of Transmission Range] 2160 miles from French Polynesia"<< endl;
  95. }
  96. else{
  97. std::cout << "Pacific (open waters) [Out of Transmission Range] 1080 miles from French Polynesia"<< endl;
  98. }
  99.  
  100.  
  101. return ;
  102. }
  103.  
  104. }
Last edited by mattyd; Nov 2nd, 2006 at 6:14 pm. Reason: Title:"Functiom" should be "Function" :/
Similar Threads
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Functiom Back to Main

The number of braces in printShipPos() looks ok to me.

C++ Syntax (Toggle Plain Text)
  1. srand((unsigned)time(0)); //RNG
  2. float shipPos;
  3. for(int index=0; index<1; index++){
two comments: (1) srand() should only be called once throught the lifetime of the program. Best place to put it is near the beginning of main() function.

(2) I hope you realize that loop will be executed only once. If you do then there really is no point to the loop.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Nov 2nd, 2006
0

Re: Functiom Back to Main

Ancient Dragon:

Cool. I will move and edit the RNG like said.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Functiom Back to Main

C++ Syntax (Toggle Plain Text)
  1. two comments: (1) srand() should only be called once throught the lifetime of the program. Best place to put it is near the beginning of main() function.
And, I can simply call this from within the program when needed, say from the area it originally was located?
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 2nd, 2006
0

Re: Functiom Back to Main

>>And, I can simply call this from within the program when needed, say from the area it originally was located?

As long as it only gets called ONCE. Every time it is called it will reseed the random number generator
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Nov 2nd, 2006
0

Re: Functiom Back to Main

>>And, I can simply call this from within the program when needed, say from the area it originally was located?

As long as it only gets called ONCE. Every time it is called it will reseed the random number generator
OK-- Thank-you -- will do.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 3rd, 2006
0

Re: Function Back to Main

Greetings:

A simple question that has me stuck: Program runs, calls function, function runs... Return to Main? This last area is where it fails. No errors, just no control returned to Main.cpp at end of function run. I've been studying the code much and researching the solution on the Net all day.

I swear there is one (1) too many "}" in Look.cpp, but it is not detected by the compiler. (?)

Any point in the right direction would be vastly appreciated-- I do not want the answer given to me directly at this point, I want to figure it out myself. Perhaps just a hint, a link to a relevant Web resource, etc?

Thank-you in advance, everyone.

Look.h
C++ Syntax (Toggle Plain Text)
  1. class Look {
  2. public:
  3. float shipPos ;
  4. float satPos;
  5. float interior;
  6.  
  7. void printShipPos(); // new function declaration
  8.  
  9. Look();
  10. ~Look();
  11. };
Main.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "Look.h"
  3. using namespace std;
  4.  
  5. int choice;
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11.  
  12.  
  13. std::cout << "________________________________"<< endl;
  14. std::cout << ""<< endl;
  15. std::cout << "Welcome to Platform XYZ-- Please Enter a Number:"<< endl;
  16. std::cout << ""<< endl;
  17. std::cout << "[1]Check Location for Transmission to Base"<< endl;
  18. std::cout << "[2]Stub"<< endl;
  19. std::cout << ""<< endl;
  20. std::cout << "________________________________"<< endl;
  21.  
  22.  
  23.  
  24. cin >> choice;
  25.  
  26. Look look1; //** ERROR
  27.  
  28.  
  29. if(choice == 1){
  30. look1.printShipPos(); //** ERROR
  31. }
  32. else {
  33. std::cout << "OTHER! chosen (stub)"<< endl;
  34. }
  35.  
  36.  
  37. system("PAUSE");
  38.  
  39. return 0;
  40. }

Look.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <ctime>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include "Look.h"
  5. using namespace std;
  6.  
  7.  
  8. int countFlag = 0;
  9.  
  10. Look::Look() {
  11. }
  12.  
  13. Look::~Look() {
  14. }
  15.  
  16. void Look::printShipPos()
  17. {
  18. srand((unsigned)time(0)); //RNG
  19. float shipPos;
  20. for(int index=0; index<1; index++){
  21. shipPos = (rand()%24)+7;
  22. cout << shipPos << endl;
  23. countFlag = 1; //test
  24. cout << countFlag << endl; //test
  25.  
  26.  
  27. if (shipPos <= 3.80) {
  28. std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
  29. }
  30. else if (shipPos <= 7.60) {
  31. std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
  32. }
  33. else if (shipPos <= 11.40) {
  34. std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl;
  35. }
  36. else if ((shipPos >= 15.20) && (shipPos < 19.00)) {
  37. std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl;
  38. }
  39. else if ((shipPos >= 19.00) && (shipPos < 22.80)){
  40. std::cout << "La Paz, Baja, Mexico [In Transmission Range]"<< endl;
  41. }
  42. else if ((shipPos >= 22.80) && (shipPos < 26.60)){
  43. std::cout << "Gulf of Mexico [In Transmission Range]"<< endl;
  44. }
  45. else if ((shipPos >= 26.60) && (shipPos < 30.40)){
  46. std::cout << "Tampa, Florida [In Transmission Range]"<< endl;
  47. }
  48. else if ((shipPos >= 30.40) && (shipPos < 34.20)){
  49. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 4320 miles from Lisbon, Portugal"<< endl;
  50. }
  51. else if ((shipPos >= 34.20) && (shipPos < 38.00)){
  52. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 3240 miles from Lisbon, Portugal"<< endl;
  53. }
  54. else if (shipPos <= 38.00) {
  55. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 2160 miles from Lisbon, Portugal"<< endl;
  56. }
  57. else if (shipPos <= 41.80) {
  58. std::cout << "Canary Islands [Out of Transmission Range]"<< endl;
  59. }
  60. else if (shipPos <= 45.60) {
  61. std::cout << "Lisbon, Portugal [Out of Transmission Range]"<< endl;
  62. }
  63. else if (shipPos <= 49.40) {
  64. std::cout << "Tripoli, Libya [Out of Transmission Range]"<< endl;
  65. }
  66. else if (shipPos <= 53.20) {
  67. std::cout << "Baghdad, Iraq [Out of Transmission Range]"<< endl;
  68. }
  69. else if (shipPos <= 57.00) {
  70. std::cout << "Eastern Iran [Out of Transmission Range]"<< endl;
  71. }
  72. else if (shipPos <= 60.80) {
  73. std::cout << "Central Tajikistan [Out of Transmission Range]"<< endl;
  74. }
  75. else if (shipPos <= 64.60) {
  76. std::cout << "Kathmandu, Nepal [Out of Transmission Range]"<< endl;
  77. }
  78. else if (shipPos <= 68.40) {
  79. std::cout << "Hanoi, Vietnam [Out of Transmission Range]"<< endl;
  80. }
  81. else if (shipPos <= 72.20) {
  82. std::cout << "Hong Kong, China [Out of Transmission Range]"<< endl;
  83. }
  84. else if (shipPos <= 76.00) {
  85. std::cout << "Pacific (open waters) [Out of Transmission Range] 5400 miles from French Polynesia"<< endl;
  86. }
  87. else if (shipPos <= 79.80) {
  88. std::cout << "Pacific (open waters) [Out of Transmission Range] 4320 miles from French Polynesia"<< endl;
  89. }
  90. else if (shipPos <= 83.60) {
  91. std::cout << "Pacific (open waters) [Out of Transmission Range] 3240 miles from French Polynesia"<< endl;
  92. }
  93. else if (shipPos <= 87.40) {
  94. std::cout << "Pacific (open waters) [Out of Transmission Range] 2160 miles from French Polynesia"<< endl;
  95. }
  96. else{
  97. std::cout << "Pacific (open waters) [Out of Transmission Range] 1080 miles from French Polynesia"<< endl;
  98. }
  99.  
  100.  
  101. return ;
  102. }
  103.  
  104. }

Finding the solution for this is not going so well. I always miss the little things and I think it is the same in this case; I made a copy of the project last night so I could hack at it freely and see what was going on-- this has yielded nothing as of yet.

I have been debugging. This is always neat. It is one of my favorite aspects of programming, digging around step-by-step in hopes of unearthing a bug buried deep like an unwanted treasure.

I guess I always took for granted the concept of returning control to Main-- this has never been an issue before. What am I missing? There must be a lesson here. One thing about programming that I love: there is always so much to learn.

I am going to keep pushing forward with this problem. I could use some help and direction from any kind-hearted coder who has the time.

Thank-you.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Calendar
Next Thread in C++ Forum Timeline: help for building a USB driver for windows?





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


Follow us on Twitter


© 2011 DaniWeb® LLC