简体   繁体   中英

Having trouble trying to compile this code

I am having an issue when I try to compile this solution.
I am fairly new to c++, but I have been teaching myself with practicing things. Please critique!

I am getting a huge list of errors, but I have spent hours searching the internet to find what I am missing, but I can't seem to cypher the error message.

The biggest error seems to say that it doesn't recognize my cin>> command

I wonder if it is possible to place an if statement inside each case, and may be easier to read, but I keep thinking that isn't legal.

/////////////////////edit/////////////////////////

#include <iostream>
#include <string>
using namespace std;

int main()
{
   int gameNumber;
   string response;

    cout << "\tPlease choose the number for the game you would like to play: \n" << endl;
    cout << "\t\t1. Shenmue 1 and 2.\n" << endl;
    cout << "\t\t2. Witcher 3.\n" << endl;
    cout << "\t\t3. Dragonball Xenoverse 2.\n" << endl;
    cout << "\t\t4. God of War.\n" << endl;
    cout << "\t\t5. Splinter Cell.\n" << endl;
    cout << "\t\t6. The Sims 3.\n" << endl;
    cout << "\t\t7. Life is Strange 3.\n" << endl;
    cout << "\t\t8. God of War 7.\n" << endl;
    cin >> gameNumber;

    switch (gameNumber)
    {
        case 1:   
                cout << "Good choice!" << endl;
                cout << "Which console did you play it one?" << endl;
        cin >> response;
            break;
        case 2:
                cout << "This one is really long!" << endl;
                cout << "Who was your favorite character?" << endl;
        cin >> response;
            break;
        case 3:
                cout << "I've beat this one a bunch!" << endl;
                cout << "Who do you battle with the most?" << endl;
        cin >> response;
            break;
        case 4:
                cout << "Best game ever!" << endl;
                cout << "Which God of War is your favorite?" << endl;
        cin >> response;
            break;
        case 5:
                cout << "When's the last time you heard this name?" << endl;
                cout << "Have you actually heard of this game?" << endl;
        cin >> response;
            break;
        case 6:
                cout << "Why can't you kill anyone anymore?" << endl;
                cout << "What was your favorite death?" << endl;
        cin >> response;
            break;
        case 7:
                cout << "You wish this was out." << endl;
                cout << "Do you wish this game was out?" << endl;
        cin >> response;
            break;
        case 8:
                cout << "You really wish this was out!" << endl;
                cout << "What could they possibly make this game with?" << endl;
        cin >> response;
            break;
        default:
                cout << "You're dumb, click the list!" << endl;
            break;
    }

    return 0;
}

This edited code compiles correctly and does what I want it to do at this stage. Thank you for all of your feedback. I took what you guys suggested and pointed out, and fixed it! Thanks again guys.

Most of your program is perfectly fine. The main problem is that you really seem to want only a single string, not an array of 50 strings, so you want to change this string response[50]; to: string response; .

The other problem is that in one place you accidentally used << where you undoubtedly intended to use >> , so: cin << response; needs to be cin >> response; .

With those changes, it compiles and seems to do about what I'd expect you probably wanted from glancing at the code. I suppose I should add, however, that this doesn't mean the code is really written the way I'd like to see it written. Personally, I'd probably write something more like this:

#include <iostream>
#include <string>
#include <vector>

struct Game {
    std::string name;
    std::string resp;
    std::string question;

    std::string ask() {
        std::cout << resp << "\n";
        std::cout << question;
        std::string ret;
        std::cin >> ret;
        return ret;
    }
};

int main()
{
    std::vector<Game> games
    {
        { "Shenmue 1 and 2.", "Good choice!", "Which console did you play it on?"},
        { "Witcher 3.", "This one is really long!", "Who was your favorite character?"},
        { "Dragonball Xenoverse 2.", "I've beat this one a bunch!",  "Who do you battle with the most?" },
        { "God of War.", "Best game ever!", "Which God of War is your favorite?" },
        { "Splinter Cell.", "When's the last time you heard this name?", "Have you actually heard of this game?"},
        { "The Sims 3.", "Why can't you kill anyone anymore?", "What was your favorite death?"},
        { "Life is Strange 3.", "You wish this was out.", "Do you wish this game was out?"},
        { "God of War 7.","You really wish this was out!", "What could they possibly make this game with?" }
    };


    std::cout << "\tPlease choose the number for the game you would like to play: \n";
    for (int i = 1; i < games.size(); i++) {
        std::cout << i << ") " << games[i].name << "\n";
    }

    int gameNumber;
    std::cin >> gameNumber;
    games[gameNumber].ask();
}

I'm still not entirely happy with that, but it's at least something of an improvement.

There's a lot going on here, but I'll start with the problem that's preventing it from compiling. As you've observed, the cin call isn't working -- more precisely, it's the operator>> that can't be resolved.

I'm guessing you've been learning C++ from a source that has a lot of old C stuff still in it, because your definition of response is a mixture of the two that ends up in a middle ground that doesn't compile. In C, you'd declare this string as an array of characters , possibly of size 50 like you've done, but the syntax for this is actually char response[50] (although this is problematic for a number of reasons, they're not really relevant what you're doing at the moment). Your string response[50] doesn't declare a string of length 50, it declares an array of 50 strings! Hopefully this makes it clear why operator>> doesn't work: it knows how to put something into a string, but it doesn't know how to put it into an array of strings.

The easy solution here is to just declare a single string , as the other answer suggests. Since C++ strings are automatically sized, it will take care of the length automatically and you don't have to worry about arbitrarily picking a maximum.

Separately, as you seem to suspect, the switch and the if statements are doing essentially the same thing, so you can combine them (and should do so for readability). In this case you shouldn't even need the ifs at all: just move the cout/cin calls into the switch cases before the break; s and it should do what you expect.

Finally, it's generally discouraged to write using namespace std; , it being clearer and less error-prone to just write the std:: before all the Standard Library stuff you use. This makes your intent clearer to both readers and compilers, and I promise you get used to it pretty quickly.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM