DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Function Back to Main (http://www.daniweb.com/forums/thread60335.html)

mattyd Nov 2nd, 2006 6:11 pm
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
class Look {
public:
float shipPos ; 
float satPos;
float interior;

void printShipPos(); // new function declaration

Look();
~Look();
};
Main.cpp
#include <iostream>
#include "Look.h"
using namespace std;

 int choice;



int main()
{


    std::cout << "________________________________"<< endl;
    std::cout << ""<< endl;
    std::cout << "Welcome to Platform XYZ-- Please Enter a Number:"<< endl;
    std::cout << ""<< endl;
    std::cout << "[1]Check Location for Transmission to Base"<< endl;
    std::cout << "[2]Stub"<< endl;
    std::cout << ""<< endl;
    std::cout << "________________________________"<< endl;

   

    cin >> choice;

    Look look1; //** ERROR


    if(choice == 1){
        look1.printShipPos(); //** ERROR
    }
    else {
        std::cout << "OTHER! chosen (stub)"<< endl;
    }
   
   
    system("PAUSE");

return 0;
}

Look.cpp
#include <ctime> 
#include <cstdlib>
#include <iostream>
#include "Look.h"
using namespace std;


int countFlag = 0;

Look::Look() {
}
 
Look::~Look() {
}

void Look::printShipPos()
{
    srand((unsigned)time(0));  //RNG
    float shipPos;
    for(int index=0; index<1; index++){
        shipPos = (rand()%24)+7;
        cout << shipPos << endl;   
        countFlag = 1;                //test
        cout << countFlag << endl;    //test
   
   
if (shipPos <= 3.80) {
  std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 7.60) {
  std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 11.40) {
  std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl;
}
else if ((shipPos >= 15.20) && (shipPos < 19.00)) {
  std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl;
}
else if ((shipPos >= 19.00) && (shipPos < 22.80)){
  std::cout << "La Paz, Baja, Mexico [In Transmission Range]"<< endl;   
}
else if ((shipPos >= 22.80) && (shipPos < 26.60)){
  std::cout << "Gulf of Mexico [In Transmission Range]"<< endl;
}
else if ((shipPos >= 26.60) && (shipPos < 30.40)){
  std::cout << "Tampa, Florida [In Transmission Range]"<< endl;
}
else if ((shipPos >= 30.40) && (shipPos < 34.20)){
  std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 4320 miles from Lisbon, Portugal"<< endl;
}
else if ((shipPos >= 34.20) && (shipPos < 38.00)){
  std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 3240 miles from Lisbon, Portugal"<< endl;
}
else if (shipPos <= 38.00) {
  std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 2160 miles from Lisbon, Portugal"<< endl;
}
else if (shipPos <= 41.80) {
  std::cout << "Canary Islands [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 45.60) {
  std::cout << "Lisbon, Portugal [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 49.40) {
  std::cout << "Tripoli, Libya [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 53.20) {
  std::cout << "Baghdad, Iraq [Out of Transmission Range]"<< endl;       
}
else if (shipPos <= 57.00) {
  std::cout << "Eastern Iran [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 60.80) {
  std::cout << "Central Tajikistan [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 64.60) {
  std::cout << "Kathmandu, Nepal [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 68.40) {
  std::cout << "Hanoi, Vietnam [Out of Transmission Range]"<< endl;       
}
else if (shipPos <= 72.20) {
  std::cout << "Hong Kong, China [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 76.00) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 5400 miles from French Polynesia"<< endl;
}
else if (shipPos <= 79.80) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 4320 miles from French Polynesia"<< endl; 
}
else if (shipPos <= 83.60) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 3240 miles from French Polynesia"<< endl; 
}
else if (shipPos <= 87.40) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 2160 miles from French Polynesia"<< endl;
}
else{
  std::cout << "Pacific (open waters) [Out of Transmission Range] 1080 miles from French Polynesia"<< endl; 
}


return ;
    }

}

Ancient Dragon Nov 2nd, 2006 6:34 pm
Re: Functiom Back to Main
 
The number of braces in printShipPos() looks ok to me.

   srand((unsigned)time(0));   //RNG
    float shipPos;
    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.

mattyd Nov 2nd, 2006 6:43 pm
Re: Functiom Back to Main
 
Ancient Dragon:

Cool. I will move and edit the RNG like said.

mattyd Nov 2nd, 2006 6:53 pm
Re: Functiom Back to Main
 
 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?

Ancient Dragon Nov 2nd, 2006 6:56 pm
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

mattyd Nov 2nd, 2006 7:01 pm
Re: Functiom Back to Main
 
Quote:

Originally Posted by Ancient Dragon (Post 271360)
>>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.

mattyd Nov 3rd, 2006 4:26 pm
Re: Function Back to Main
 
Quote:

Originally Posted by sharky_machine (Post 271338)
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
class Look {
public:
float shipPos ; 
float satPos;
float interior;

void printShipPos(); // new function declaration

Look();
~Look();
};
Main.cpp
#include <iostream>
#include "Look.h"
using namespace std;

 int choice;



int main()
{


    std::cout << "________________________________"<< endl;
    std::cout << ""<< endl;
    std::cout << "Welcome to Platform XYZ-- Please Enter a Number:"<< endl;
    std::cout << ""<< endl;
    std::cout << "[1]Check Location for Transmission to Base"<< endl;
    std::cout << "[2]Stub"<< endl;
    std::cout << ""<< endl;
    std::cout << "________________________________"<< endl;

   

    cin >> choice;

    Look look1; //** ERROR


    if(choice == 1){
        look1.printShipPos(); //** ERROR
    }
    else {
        std::cout << "OTHER! chosen (stub)"<< endl;
    }
   
   
    system("PAUSE");

return 0;
}

Look.cpp
#include <ctime> 
#include <cstdlib>
#include <iostream>
#include "Look.h"
using namespace std;


int countFlag = 0;

Look::Look() {
}
 
Look::~Look() {
}

void Look::printShipPos()
{
    srand((unsigned)time(0));  //RNG
    float shipPos;
    for(int index=0; index<1; index++){
        shipPos = (rand()%24)+7;
        cout << shipPos << endl;   
        countFlag = 1;                //test
        cout << countFlag << endl;    //test
   
   
if (shipPos <= 3.80) {
  std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 7.60) {
  std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 11.40) {
  std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl;
}
else if ((shipPos >= 15.20) && (shipPos < 19.00)) {
  std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl;
}
else if ((shipPos >= 19.00) && (shipPos < 22.80)){
  std::cout << "La Paz, Baja, Mexico [In Transmission Range]"<< endl;   
}
else if ((shipPos >= 22.80) && (shipPos < 26.60)){
  std::cout << "Gulf of Mexico [In Transmission Range]"<< endl;
}
else if ((shipPos >= 26.60) && (shipPos < 30.40)){
  std::cout << "Tampa, Florida [In Transmission Range]"<< endl;
}
else if ((shipPos >= 30.40) && (shipPos < 34.20)){
  std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 4320 miles from Lisbon, Portugal"<< endl;
}
else if ((shipPos >= 34.20) && (shipPos < 38.00)){
  std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 3240 miles from Lisbon, Portugal"<< endl;
}
else if (shipPos <= 38.00) {
  std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 2160 miles from Lisbon, Portugal"<< endl;
}
else if (shipPos <= 41.80) {
  std::cout << "Canary Islands [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 45.60) {
  std::cout << "Lisbon, Portugal [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 49.40) {
  std::cout << "Tripoli, Libya [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 53.20) {
  std::cout << "Baghdad, Iraq [Out of Transmission Range]"<< endl;       
}
else if (shipPos <= 57.00) {
  std::cout << "Eastern Iran [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 60.80) {
  std::cout << "Central Tajikistan [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 64.60) {
  std::cout << "Kathmandu, Nepal [Out of Transmission Range]"<< endl;
}
else if (shipPos <= 68.40) {
  std::cout << "Hanoi, Vietnam [Out of Transmission Range]"<< endl;       
}
else if (shipPos <= 72.20) {
  std::cout << "Hong Kong, China [Out of Transmission Range]"<< endl;   
}
else if (shipPos <= 76.00) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 5400 miles from French Polynesia"<< endl;
}
else if (shipPos <= 79.80) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 4320 miles from French Polynesia"<< endl; 
}
else if (shipPos <= 83.60) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 3240 miles from French Polynesia"<< endl; 
}
else if (shipPos <= 87.40) {
  std::cout << "Pacific (open waters) [Out of Transmission Range] 2160 miles from French Polynesia"<< endl;
}
else{
  std::cout << "Pacific (open waters) [Out of Transmission Range] 1080 miles from French Polynesia"<< endl; 
}


return ;
    }

}


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.


All times are GMT -4. The time now is 9:06 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC