Function Back to Main

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Function Back to Main

 
0
  #1
Nov 2nd, 2006
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
  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
  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
  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" :/
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,367
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1465
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Functiom Back to Main

 
0
  #2
Nov 2nd, 2006
The number of braces in printShipPos() looks ok to me.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Functiom Back to Main

 
0
  #3
Nov 2nd, 2006
Ancient Dragon:

Cool. I will move and edit the RNG like said.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Functiom Back to Main

 
0
  #4
Nov 2nd, 2006
  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,367
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1465
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Functiom Back to Main

 
0
  #5
Nov 2nd, 2006
>>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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Functiom Back to Main

 
0
  #6
Nov 2nd, 2006
Originally Posted by Ancient Dragon View Post
>>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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Function Back to Main

 
0
  #7
Nov 3rd, 2006
Originally Posted by sharky_machine View 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
  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
  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
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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