6 Replies - 580 Views - Last Post: 15 August 2011 - 10:29 PM Rate Topic: -----

#1 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

holiday destinations, sentinel controlled exit problem

Posted 15 August 2011 - 08:24 PM

hey guys, this is a small problem.
I've already written the code, just can't seem to get my loop to work, here it is:


#include <stdlib.h>
#include <conio.h>
#include <iostream>
using namespace std;

void holidayDestinations( char );
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{  
   char destination;
   cout << "Welcome to the holiday destination package..." << endl << endl;
   cout << "Please choose one of the following destinations: " << endl << endl;
   
   while ( destination != "*" )
   {
      holidayDestinations( destination );
   }
    
   getch();
   return 0;
}
//------------------------------------------------------------------------------
void holidayDestinations( char choice )
{
   cout << " France  (a) " << endl << " Italy   (B)/> " << endl << " England (c) " 
   << endl << " Spain   (d) " << endl << " Germany (e) " << endl << endl;
   cin >> choice;
   
   system("cls");
   
   switch ( choice )
   {
      case 'a' :
         cout << "You chose France as your destination!" << endl << endl;
         break;
         
      case 'b' :
         cout << "You chose Italy as your destination!" << endl << endl;
         break;  
         
      case 'c' :
         cout << "You chose England as your destination!" << endl << endl;
         break;
            
      case 'd' :
         cout << "You chose Spain as your destination!" << endl << endl;
         break;
         
      case 'e' :
         cout << "You chose Germany as your destination!" << endl << endl;
         break;
            
      default: 
         cout << "That's not a VALID choice" << endl << endl;
         break;
         
      cout << "Would you like to choose another destination or ""*"" to exit?: "
           << endl << endl;
   }   
}
//------------------------------------------------------------------------------     



basically I just want "*" to exit the function... any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: holiday destinations, sentinel controlled exit problem

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: holiday destinations, sentinel controlled exit problem

Posted 15 August 2011 - 09:04 PM

"*" == the char array { '*', 0x00 }

'*' == the char '*'

so char destination can never equal "*". Just can't happen.
Was This Post Helpful? 0
  • +
  • -

#3 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: holiday destinations, sentinel controlled exit problem

Posted 15 August 2011 - 09:06 PM

View PostNickDMax, on 15 August 2011 - 09:04 PM, said:

"*" == the char array { '*', 0x00 }

'*' == the char '*'

so char destination can never equal "*". Just can't happen.


ok thanks... so how do I fix this?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: holiday destinations, sentinel controlled exit problem

Posted 15 August 2011 - 09:12 PM

Well I have not examined your program in depth so I don't know if your logic is correct. But you can test:


while ( destination != '*' )


however I can see that the variable destination is never initialized and is never changed... so unless it randomly happens to land on a '*' when the program starts up you have an infinite loop there.

So you need to ensure that the variable destination changes.

you could do this by changing your function to "pass by reference" but it would probably be better to just have the function return a value:
#include <cstdlib>
#include <iostream>
#include <conio.h> // non-standard header.
using namespace std;

char holidayDestinations();
//------------------------------------------------------------------------------
int main(int argc, char *argv[]) {
    char destination = ' ';
    cout << "Welcome to the holiday destination package..." << endl << endl;
    cout << "Please choose one of the following destinations: " << endl << endl;

    while ( destination != '*' ) {
        destination = holidayDestinations();
    }

    getch();
    return 0;
}
//------------------------------------------------------------------------------
char holidayDestinations() {
    char choice;
    cout << " France  (a) " << endl << " Italy   (B)/> " << endl << " England (c) "
         << endl << " Spain   (d) " << endl << " Germany (e) " << endl << endl;
    cin >> choice;
    system("cls");

    switch ( choice ) {
        case 'a' :
            cout << "You chose France as your destination!" << endl << endl;
            break;
        case 'b' :
            cout << "You chose Italy as your destination!" << endl << endl;
            break;
        case 'c' :
            cout << "You chose England as your destination!" << endl << endl;
            break;
        case 'd' :
            cout << "You chose Spain as your destination!" << endl << endl;
            break;
        case 'e' :
            cout << "You chose Germany as your destination!" << endl << endl;
            break;
        default:
            cout << "That's not a VALID choice" << endl << endl;
            break;
            cout << "Would you like to choose another destination or ""*"" to exit?: "
                 << endl << endl;
    }
    return choice;
}
//------------------------------------------------------------------------------


Was This Post Helpful? 0
  • +
  • -

#5 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: holiday destinations, sentinel controlled exit problem

Posted 15 August 2011 - 09:22 PM

View PostNickDMax, on 15 August 2011 - 09:12 PM, said:

Well I have not examined your program in depth so I don't know if your logic is correct. But you can test:


while ( destination != '*' )


however I can see that the variable destination is never initialized and is never changed... so unless it randomly happens to land on a '*' when the program starts up you have an infinite loop there.

So you need to ensure that the variable destination changes.

you could do this by changing your function to "pass by reference" but it would probably be better to just have the function return a value:
#include <cstdlib>
#include <iostream>
#include <conio.h> // non-standard header.
using namespace std;

char holidayDestinations();
//------------------------------------------------------------------------------
int main(int argc, char *argv[]) {
    char destination = ' ';
    cout << "Welcome to the holiday destination package..." << endl << endl;
    cout << "Please choose one of the following destinations: " << endl << endl;

    while ( destination != '*' ) {
        destination = holidayDestinations();
    }

    getch();
    return 0;
}
//------------------------------------------------------------------------------
char holidayDestinations() {
    char choice;
    cout << " France  (a) " << endl << " Italy   (B)/> " << endl << " England (c) "
         << endl << " Spain   (d) " << endl << " Germany (e) " << endl << endl;
    cin >> choice;
    system("cls");

    switch ( choice ) {
        case 'a' :
            cout << "You chose France as your destination!" << endl << endl;
            break;
        case 'b' :
            cout << "You chose Italy as your destination!" << endl << endl;
            break;
        case 'c' :
            cout << "You chose England as your destination!" << endl << endl;
            break;
        case 'd' :
            cout << "You chose Spain as your destination!" << endl << endl;
            break;
        case 'e' :
            cout << "You chose Germany as your destination!" << endl << endl;
            break;
        default:
            cout << "That's not a VALID choice" << endl << endl;
            break;
            cout << "Would you like to choose another destination or ""*"" to exit?: "
                 << endl << endl;
    }
    return choice;
}
//------------------------------------------------------------------------------



Ok I see, so it's not possible to use void in this case? because our question states:


Write a menu system that allows the user to select alternatives for types of holiday destination.
Clear the screen and print an output that indicates which alternative was selected.
On this same output screen, prompt the user for an alternative holiday destination, printing the menu again. Put the code for the menu into a function and call the function from main()whenever you wish to print the menu. In main() use a loop with sentinel controlled exit.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: holiday destinations, sentinel controlled exit problem

Posted 15 August 2011 - 09:33 PM

Well -- as I said you can pass-by-reference. However it really is not necessary or probably a very good idea to do so.

I don't see anything in the question that precludes using a plain-old regular return a value function.
Was This Post Helpful? 1
  • +
  • -

#7 rdhc1330  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 01-August 11

Re: holiday destinations, sentinel controlled exit problem

Posted 15 August 2011 - 10:29 PM

View PostNickDMax, on 15 August 2011 - 09:33 PM, said:

Well -- as I said you can pass-by-reference. However it really is not necessary or probably a very good idea to do so.

I don't see anything in the question that precludes using a plain-old regular return a value function.


Thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1