Using Enum in C ++, no errors, but won't be injected

I tried to create a code for the assignment where the user would enter two characters and from the list the program would figure out what the user meant. In my example my list is game consoles, so xb xbox, pl is playstation, etc. I got a very long time and used what was available in my book and online, however at this point whenever I run my code it compiles, runs, and closes instantly. There are no errors, but there are also no prompts for user input. Any advice?

#include <iostream>
using namespace std;


enum gameconsoles { Xbox, Playstation, PSP, Super_Nintendo, NES, Sega, Gamecube, Nintendo64, Wii, Comodore64, Atari }; // Yes, I know some of this isn't proper.. Should be Atari 2600 and so on, I know my consoles. Just limited to 2 characters made my selection a little more narrow so I had to generalize.
gameconsoles listed;
gameconsoles readgameconsoles()
{
    gameconsoles listed;
    char char1, char2;

    cout << "This program will determine a game console based off of" << endl;
    cout << "the first two characters you input. The list is somewhat small, but" << endl;
    cout << "demonstrates the operation of enumeration programming." << endl << endl;
    cout << "Please input the first two characters of a game console: ";
    cin >> char1 >> char2;
    switch (char1)
    {
    case 'A':
    case 'a':
        listed = Atari;
        break;
    case 'C':
    case 'c':
        listed = Comodore64;
        break;
    case 'G':
    case 'g':
        listed = Gamecube;
        break;
    case'N':
    case'n':
        if (char2 == 'E' || char2 == 'e')
            listed = NES;
        else
            listed = Nintendo64;
        break;
    case 'P':
    case 'p':
        if (char2 == 'L' || char2 == 'l')
            listed = Playstation;
        else
            listed = PSP;
        break;
    case 'S':
    case 's':
        if (char2 == 'E' || char2 == 'e')
            listed = Sega;
        else
            listed = Super_Nintendo;
        break;
    case 'W':
    case 'w':
        listed = Wii;
        break;
    case 'X':
    case 'x':
        listed = Xbox;
        break;
    default:
        cout << "Illegal input. Try again" << endl;
    }
    return listed;
}

void printEnum(gameconsoles listed)
{
    switch (listed)
    {
    case Atari:
        cout << "The console you have specified is Atari";
        break;
    case Comodore64:
        cout << "The console you have specified is the Comodore 64";
        break;
    case Gamecube:
        cout << "The console you have specified is Gamecube";
        break;
    case NES:
        cout << "The console you have specified is the NES" << endl;
        cout << "or also known as the Nintendo Entertainment System";
        break;
    case Nintendo64:
        cout << "The console you have specified is Nintendo 64";
        break;
    case Playstation:
        cout << "The console you have specified is Playstation";
        break;
    case PSP:
        cout << "The console you have specified is PSP" << endl;
        cout << "or better known as the Playstation Portable";
        break;
    case Sega:
        cout << "The console you have specified is Sega";
        break;
    case Super_Nintendo:
        cout << "The console you have specified is Super Nintendo";
        break;
    case Wii:
        cout << "The console you have specified is Wii";
        break;
    case Xbox:
        cout << "The console you have specified is Xbox";
        system("PAUSE");
    }
}

      

+3


source to share


3 answers


You can do it:

 #include <iostream>
    using namespace std;

enum gameconsoles
{ 
    Xbox, 
    Playstation, 
    PSP, 
    Super_Nintendo,
    NES, 
    Sega, 
    Gamecube, 
    Nintendo64, 
    Wii, 
    Comodore64,
    Atari 
};

gameconsoles listed;
gameconsoles readgameconsoles(char char1, char char2)
{

    switch (char1)
    {
    case 'A':
    case 'a':
        listed = Atari;
        break;
    case 'C':
    case 'c':
        listed = Comodore64;
        break;
    case 'G':
    case 'g':
        listed = Gamecube;
        break;
    case'N':
    case'n':
        if (char2 == 'E' || char2 == 'e')
            listed = NES;
        else
            listed = Nintendo64;
        break;
    case 'P':
    case 'p':
        if (char2 == 'L' || char2 == 'l')
            listed = Playstation;
        else
            listed = PSP;
        break;
    case 'S':
    case 's':
        if (char2 == 'E' || char2 == 'e')
            listed = Sega;
        else
            listed = Super_Nintendo;
        break;
    case 'W':
    case 'w':
        listed = Wii;
        break;
    case 'X':
    case 'x':
        listed = Xbox;
        break;
    default:
        cout << "Illegal input. Try again" << endl;
    }
    return listed;
}

void printEnum(gameconsoles listed)
{
    switch (listed)
    {
    case Atari:
        cout << "The console you have specified is Atari";
        break;
    case Comodore64:
        cout << "The console you have specified is the Comodore 64";
        break;
    case Gamecube:
        cout << "The console you have specified is Gamecube";
        break;
    case NES:
        cout << "The console you have specified is the NES" << endl;
        cout << "or also known as the Nintendo Entertainment System";
        break;
    case Nintendo64:
        cout << "The console you have specified is Nintendo 64";
        break;
    case Playstation:
        cout << "The console you have specified is Playstation";
        break;
    case PSP:
        cout << "The console you have specified is PSP" << endl;
        cout << "or better known as the Playstation Portable";
        break;
    case Sega:
        cout << "The console you have specified is Sega";
        break;
    case Super_Nintendo:
        cout << "The console you have specified is Super Nintendo";
        break;
    case Wii:
        cout << "The console you have specified is Wii";
        break;
    case Xbox:
        cout << "The console you have specified is Xbox";
        system("PAUSE");
    }
}
void main()
{
    char char1, char2;

    cout << "This program will determine a game console based off of" << endl;
    cout << "the first two characters you input. The list is somewhat small, but" << endl;
    cout << "demonstrates the operation of enumeration programming." << endl << endl;
    cout << "Please input the first two characters of a game console: ";
    cin >> char1 >> char2;
    readgameconsoles(char1, char2);
    printEnum(listed);
    system("pause");
    return;
}

      



I tried it and it works for me.

+2


source


Just put main

at the end of your code and call the appropriate function:



int main()
{
readgameconsoles();
return 0;
}

      

0


source


This is not a question about enum, but about environment design or main function.

You must

1) run and run this file:

#include <iostream>
using namespace std;

int main(int argc, char** argv){
    cout << "Please input the first two characters of a game console: ";
    cin >> char1 >> char2;
    cout << "you typed " << char1 << " and " << char2 << endl;

}

      

2) if you don't see the line . Please enter the first two characters of the game console: "This is very, very strange. Ask about this by describing your environment. If you see one, type two char, press enter and go to step (3):

3) if you see the string "typed ..." that's ok and you can continue exploring enum-s. otherwithe go to step (4)

4) If not,

0


source







All Articles