3

I've tried creating a code for an assignment that will have the user input two characters, and from a list the program will determine what the user meant.. In my example, my list is video game consoles, so xb is xbox, pl is playstation, and so on. I've gotten really far and used what was available in my book and online, however at this point whenever I run my code, it compiles, runs, and instantly closes. No errors, but also no asking 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");
    }
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Logan Smith
  • 139
  • 1
  • 8
  • your system("PAUSE"); is in the switch-directive, isn't it? – kylecorver Apr 27 '15 at 15:22
  • where is the main()? – Ayushi Jha Apr 27 '15 at 15:27
  • I tried moving it outside the set, no difference. Even so, it never stopped the process when it should be asking for the cin. It just brings up the command prompt window for a second, and then disappears. – Logan Smith Apr 27 '15 at 15:29
  • That's the problem, I have no idea where to put the main and my book is crap and doesn't help explain any further... This is essentially what it showed. I tried adding all of this inside a variety of mains (int, char, enum, void), practically every prefix I could think of, only to have it error out – Logan Smith Apr 27 '15 at 15:33
  • I wouldnt put the `system("pause")` inside your switch statement (even if it showed no difference). Also, what are you doing in your main? I would revise your question with taking out the switch statements since they are not your issue. Your issue is code execution so whatever the issue is is most likely related to your main. – Javia1492 Apr 27 '15 at 15:33
  • You should remove the first `gameconsoles listed;` line. It declares a global variable that is shadowed by local variables in both functions and therefore never referenced. – Roland W Apr 27 '15 at 15:44

3 Answers3

2

You can do this:

 #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.

Javia1492
  • 862
  • 11
  • 28
  • Awesome! Thank you so much. Except, I don't know if you are getting this, but the only console output, not matter what I input seems to be Xbox... But I can definitely work with this at least! – Logan Smith Apr 27 '15 at 15:51
  • You are writing the local variable in `readgameconsoles` but reading the global one in `main` (which is initialized to `0` and therefore incidentally equal to the `Xbox` enum value). – Roland W Apr 27 '15 at 15:54
  • @RolandW Ah you're right. I only checked it for `Xbox` case lol. Ill revise the code to remove the local declaration of `listed` – Javia1492 Apr 27 '15 at 16:01
  • Beautiful. This makes so much more sense. Thank you. Like I said, going into this for the first time for an assignment, and the book is crappy at showing examples of how to fully make the code work... Just examples of each individual set. – Logan Smith Apr 27 '15 at 16:10
  • Yea, there are alot of bad programming books out there. Its a difficult thing to try and teach someone via book how to program. I suggest taking a look [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for a list of stackoverflow "approved" books :). – Javia1492 Apr 27 '15 at 16:13
0

Just put main at the end of your code, and call the relevant function:

int main()
{
readgameconsoles();
return 0;
}
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
0

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

You should

1) make 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 not see row "Please input the first two characters of a game console:", it is very and very strange. Ask about it with describe of your environment. if you see one, type two char, press enter, and go to step (3):

3) if you see "you typed ... " row, that it is ok, and you can continue to study enum-s. otherwithe go to step (4)

4) If no,