Question

Hello i am having a bit of trouble with a verified exit for my program. For...

Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function?

C++ Visual Studio 2017

#include "cPlayer.h"
char chChoice1 = ' ';
char chChoice3 = ' ';
cPlayer::cPlayer()
{
}
cPlayer::~cPlayer()
{
}

void cPlayer::fMenu()
{
   char chChoice3 = ' ';
   do
   {
       cout << "\n\t--Menu--" << endl;
       cout << "1) Enter Player Name" << endl;
       cout << "2) Select Player's Team" << endl;
       cout << "3) Display Player Info" << endl;
       cout << "4) Exit" << endl;
       cin >> chChoice1;
       system("cls");

       switch (chChoice1)
       {
       case '1':
           fPlayerName();
           system("pause");
           system("cls");
           fMenu();
           break;

       case '2':
           fSelectTeam();
           system("pause");
           system("cls");
           fMenu();
           break;

       case '3':
           fPlayerInfo();
           system("pause");
           system("cls");
           fMenu();
           break;

       case '4':
           fVerifyExit();

           break;
          
       default:
           cout << "**********************" << endl;
           cout << "--Not a valid option--" << endl;
           cout << "**********************" << endl;
           system("pause");
           break;
          
       }
   } while (chChoice1 != 'e');
}


void cPlayer::fSelectTeam()
{
   //chChoice2 is a character variable
   char chChoice2 = ' ';
   while (true)
   {
       cout << "Please select your team color." << endl;
       cout << "1) Red" << endl;
       cout << "2) Blue" << endl;
       cin >> chChoice2;

       switch (chChoice2)
       {
       case '1':
           system("cls");
           sPlayer.strTeamName = "Red";
           fMenu();
           break;

       case '2':
           system("cls");
           sPlayer.strTeamName = "Blue";
           fMenu();
           break;

       default:
           cout << "Invalid Input." << endl;
           return;
       }
   }
}


void cPlayer::fPlayerInfo()
{
   cout << "\t--PLAYER INFO--" << endl;
   cout << "Player Name: " << sPlayer.strPlayerName << endl;
   cout << "Team: " << sPlayer.strTeamName << endl;
}


void cPlayer::fPlayerName()
{
   cout << "Enter the players name." << endl;
   cin >> sPlayer.strPlayerName;//MAKE A GET LINE
}


char cPlayer::fVerifyExit()
{
   char chChoice = ' ';
   system("cls");
   cout << "Do you want to exit (Y/y)." << endl;
   cin >> chChoice;
   switch (chChoice)
   {
   case 'Y':
   case 'y':
       chChoice1 = 'e';
       break;
   default:
       chChoice1 = ' ';

       break;
   }
   return chChoice1;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include "cPlayer.h"
char chChoice1 = ' ';
char chChoice3 = ' ';
cPlayer::cPlayer()
{
}
cPlayer::~cPlayer()
{
}

void cPlayer::fMenu()
{
   // char chChoice3 = ' '; : Error here, It should not be local variable.
   chChoice3 = ' ';
   do
   {
       cout << "\n\t--Menu--" << endl;
       cout << "1) Enter Player Name" << endl;
       cout << "2) Select Player's Team" << endl;
       cout << "3) Display Player Info" << endl;
       cout << "4) Exit" << endl;
       cin >> chChoice1;
       system("cls");

       switch (chChoice1)
       {
       case '1':
           fPlayerName();
           system("pause");
           system("cls");
           fMenu();
           break;

       case '2':
           fSelectTeam();
           system("pause");
           system("cls");
           fMenu();
           break;

       case '3':
           fPlayerInfo();
           system("pause");
           system("cls");
           fMenu();
           break;

       case '4':
           fVerifyExit();

           break;
          
       default:
           cout << "**********************" << endl;
           cout << "--Not a valid option--" << endl;
           cout << "**********************" << endl;
           system("pause");
           break;
          
       }
   } while (chChoice1 != 'e');
}


void cPlayer::fSelectTeam()
{
   //chChoice2 is a character variable
   char chChoice2 = ' ';
   while (true)
   {
       cout << "Please select your team color." << endl;
       cout << "1) Red" << endl;
       cout << "2) Blue" << endl;
       cin >> chChoice2;

       switch (chChoice2)
       {
       case '1':
           system("cls");
           sPlayer.strTeamName = "Red";
           fMenu();
           break;

       case '2':
           system("cls");
           sPlayer.strTeamName = "Blue";
           fMenu();
           break;

       default:
           cout << "Invalid Input." << endl;
           return;
       }
   }
}


void cPlayer::fPlayerInfo()
{
   cout << "\t--PLAYER INFO--" << endl;
   cout << "Player Name: " << sPlayer.strPlayerName << endl;
   cout << "Team: " << sPlayer.strTeamName << endl;
}


void cPlayer::fPlayerName()
{
   cout << "Enter the players name." << endl;
   cin >> sPlayer.strPlayerName;//MAKE A GET LINE
}


char cPlayer::fVerifyExit()
{
   char chChoice = ' ';
   system("cls");
   cout << "Do you want to exit (Y/y)." << endl;
   cin >> chChoice;
   switch (chChoice)
   {
   case 'Y':
   case 'y':
       chChoice1 = 'e';
       break;
   default:
       chChoice1 = ' ';

       break;
   }
   return chChoice1;
}


your verifyExit method changes the global variable, while in the menu function, you use the local variable.. And do not read the global variable.. hence in menu function the while loop keeps running always. let me know if any issues. 

Add a comment
Know the answer?
Add Answer to:
Hello i am having a bit of trouble with a verified exit for my program. For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • Hello, I am working on my final and I am stuck right near the end of...

    Hello, I am working on my final and I am stuck right near the end of the the program. I am making a Tic-Tac-Toe game and I can't seem to figure out how to make the program ask if you would like to play again, and keep the same names for the players that just played, keep track of the player that wins, and display the number of wins said player has, after accepting to keep playing. I am wanting...

  • EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using...

    EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using namespace std; typedef struct ComplexRectStruct {    double real;    double imaginary; } ComplexRect; typedef struct ComplexPolarStruct {    double magnitude;    double angle; } ComplexPolar; double radToDegree (double rad) {    return (180.00 * rad) / PI; } double degToRadian (double deg) {    return (deg * PI) / 180; } ComplexPolar toPolar (ComplexRect r) {    ComplexPolar p;    p.magnitude = round(sqrt(pow(r.real,...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Trying to figure out how to complete my fourth case 4 (option exit), write a function...

    Trying to figure out how to complete my fourth case 4 (option exit), write a function that display a “Good Bye” message and exits/log out from user’s account displaying a menu (sign in, balance, withdraw and exit.. #include<iostream> #include <limits> using namespace std; void printstar(char ch , int n); double balance1; int main() { system("color A0"); cout<<"\n\t\t ========================================="<< endl; cout<<"\t\t || VASQUEZ ATM SERVICES ||"<< endl; cout<<"\t\t ========================================\n\n"<< endl; int password; int pincode ; cout<<" USERS \n"; cout<<" [1] -...

  • Hey, i was just wondering how i would calculate over time pay in c++ visual studio...

    Hey, i was just wondering how i would calculate over time pay in c++ visual studio 2017? what i have right now is either returning 0 for some reason or being skipped over? im also wondering about the federal tax as well because that also doesn't seem to work, any help is greatly appreciated! these are my variables char chChoice = ' '; int intempID = 0; int intHours = 0; int intOThours = 0; float flOTrate = 0; float...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs ca...

    I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs can be found at compile time, while others are found at run time. Access the UserMenu.cpp code file (in the zip file in Start Here) and use debugging practices and the debugger in Visual Studio to find each bug. Fix the bug and write a comment in the code giving a description of how you found the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT