28

I was just programming in c++, when all of a sudden all the "cout"s and "cin"s were errors and "Ambiguous". Including System.

I don't know why this happened. Everything was fine, I was coding the same program for about 2 hours, when it just... happened.

EDIT

I can still run the program without errors, but they show as errors on the text, the red scribbly line. What happened?

I'm using the Visual Studio 2013 IDE, whatever it comes with.

#include <iostream>
#include <ctime>
#include <string>
#include <Windows.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

int main()
{

struct Gun
{
    string name;
    int damage;
    int cost;
    bool purchased;
    bool equipped;

} M4A1, FAMAS;

//WEAPONS INFO
M4A1.cost = 50;
M4A1.damage = 5;
M4A1.purchased = false;
M4A1.equipped = false;

FAMAS.cost = 300;
FAMAS.damage = 10;
FAMAS.purchased = false;
FAMAS.equipped = false;
//WEAPONS INFO

//-----PLAYER(BEGIN)-----

struct Player
{
    int health;
    string name;
    int money;
    int energy;

    string l_a;
    string r_a;
    string l_l;
    string r_l;

    string rank;

} Player;

//GAME PLAYER BEGIN

Player.l_a = "Normal";
Player.r_a = "Normal";
Player.l_l = "Normal";
Player.r_l = "Normal";

Player.health = 100;
Player.money = 100;
Player.energy = 100;

string plyrname;
string rank = "Private";

Player.name = plyrname;

//GAME PLAYER END

//-----PLAYER(END)-----

cout << "What is your name? ";
cin >> plyrname;

goto mmenu;


mmenu:

//-----MAIN MENU(BEGIN)-----
system("CLS");
if (Player.l_a == "Damaged")
{
    cout << "Your Left Arm is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}
if (Player.r_a == "Damaged")
{
    cout << "Your Right Arm is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}
if (Player.l_l == "Damaged")
{
    cout << "Your Left Leg is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}
if (Player.r_l == "Damaged")
{
    cout << "Your Right Leg is damaged! Sleep for a while to fix it!";
    Sleep(1600);
}


if (Player.money >= 500 && Player.rank == "Private")
{
    system("CLS");
    cout << "You have been promoted to Private 2!";
    Player.rank = "Private 2";
    Sleep(1600);

}

if (Player.money >= 1000 && Player.rank == "Private 2")
{
    system("CLS");
    cout << "You have been promoted to Private First Class!";
    Player.rank = "Private First Class";
    Sleep(1600);
}


system("CLS");
cout << "Health: " << Player.health << ". Money: " << Player.money << " dollars." << "   Energy: " << Player.energy;
if (M4A1.equipped)
    cout << "\nGun: M4A1";
if (FAMAS.equipped)
    cout << "\nGun: FAMAS";

cout << "\n\nRank: " << Player.rank;
cout << "\n\n1) Go to Gunstore\n2) Sleep\n3) Fight\n\nAction: ";
int mmenuch1;
cin >> mmenuch1;

if (mmenuch1 == 1)
{
    goto gunstore;
}

if (mmenuch1 == 2)
{
    system("CLS");
    cout << "You sleep, restoring your energy.";
    Player.energy = 100;

    if (Player.l_a == "Damaged")
    {
        cout << "\n\nYour Left Arm was healed.";
        Player.l_a = "Normal";
    }
    if (Player.r_a == "Damaged")
    {
        cout << "\n\nYour Right Arm was healed.";
        Player.r_a = "Normal";
    }
    if (Player.l_l == "Damaged")
    {
        cout << "\n\nYour Left Leg was healed.";
        Player.l_l = "Normal";
    }
    if (Player.r_l == "Damaged")
    {
        cout << "Your Right Leg was healed.";
    }


    Sleep(1400);
    goto mmenu;
}

if (mmenuch1 == 3)
{
    system("CLS");
    goto fight;
}

//-----MAIN MENU(END)-----


fight:
srand(time(0));

system("CLS");

if (Player.r_a == "Damaged" || Player.r_l == "Damaged" || Player.l_a == "Damaged" || Player.l_l == "Damaged")
{
    cout << "You're injured, sleep to heal.";
    Sleep(1400);
    goto mmenu;
}

if (Player.energy < 40)
{
    cout << "You don't have enough energy to fight.";
    Sleep(1400);
    goto mmenu;
}

if (M4A1.equipped == false && FAMAS.equipped == false)
{
    cout << "You don't have a gun equipped.";
    Sleep(1400);
    goto gunstore;
}

if (M4A1.equipped == true && Player.energy > 40)
{

    int randnum1 = rand() % (M4A1.damage - 2 + 1) + 2;
    Player.money = Player.money + (randnum1 * 15);
    Player.energy = Player.energy - 40;

    int randnum3 = rand() % (10 - 1 + 1) + 1;
    if (randnum3 < 4)
    {

        int randnum4 = rand() % (13 - 1 + 1) + 1;

        if (randnum4 < 3)
        {
            Player.l_a = "Damaged";
        }
        if (randnum4 <= 6 && randnum4 >= 4)
        {
            Player.r_a = "Damaged";
        }
        if (randnum4 <= 9 && randnum4 >= 7)
        {
            Player.l_l = "Damaged";
        }
        if (randnum4 <= 13 && randnum4 >= 10)
        {
            Player.r_l = "Damaged";
        }



    }


    cout << "You fight, killing " << randnum1 << " enemies, making " << randnum1 * 15 << " dollars!";
    Sleep(1600);
    goto mmenu;





}




if (FAMAS.equipped == true && Player.energy > 40)
{
    int randnum2 = rand() % (FAMAS.damage - 4 + 1) + 4;
    Player.money = Player.money + (randnum2 * 15);
    Player.energy = Player.energy - 40;




    int randnum5 = rand() % (10 - 1 + 1) + 1;
    if (randnum5 < 4)
    {

        int randnum6 = rand() % (13 - 1 + 1) + 1;

        if (randnum6 < 3)
        {
            Player.l_a = "Damaged";
        }
        if (randnum6 <= 6 && randnum6 >= 4)
        {
            Player.r_a = "Damaged";
        }
        if (randnum6 <= 9 && randnum6 >= 7)
        {
            Player.l_l = "Damaged";
        }
        if (randnum6 <= 13 && randnum6 >= 10)
        {
            Player.r_l = "Damaged";
        }



    }



    cout << "You fight, killing " << randnum2 << " enemies, making " << randnum2 * 15 << " dollars!";
    Sleep(1600);
    goto mmenu;
}



//-----GUNSTORE(BEGIN)-----
gunstore:
system("CLS");
cout << "Welcome to the gunstore! You have " << Player.money << " dollars.";
cout << "\n\n1)M4A1 | Assault Rifle | $50\n2)FAMAS | Assault Rifle | $300\n\n3)Back\n\nAction: ";

int gschoice1;

cin >> gschoice1;

if (gschoice1 == 1)
{
    goto prchs_M4A1;
}
else if (gschoice1 == 2)
{
    goto prchs_FAMAS;
}
else if (gschoice1 == 3)
{
    goto mmenu;
}


prchs_M4A1:

system("CLS");

if (M4A1.purchased == true)
{
    cout << "You already purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
    int gschoice6;
    cin >> gschoice6;

    if (gschoice6 == 1)
    {
        system("CLS");
        M4A1.equipped = true;
        FAMAS.equipped = false;
        goto mmenu;
    }
    else if (gschoice6 == 2)
    {
        goto gunstore;
    }


}


if (Player.money >= 0)
{
    system("CLS");
    cout << "Would you like to buy the M4A1?";
    cout << "\n\n1)Yes\n2)No\n\nAction: ";

    int gschoice2;
    cin >> gschoice2;

    if (gschoice2 == 1)
    {
        system("CLS");
        Player.money = Player.money - M4A1.cost;
        M4A1.purchased = true;
        cout << "You've purchased the M4A1. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
        int gschoice3;
        cin >> gschoice3;



        if (gschoice3 == 1)
        {
            system("CLS");
            M4A1.equipped = true;
            FAMAS.equipped = false;

            cout << "You've equipped the M4A1";
            Sleep(1400);
            goto gunstore;
        }

        if (gschoice3 == 2)
        {
            system("CLS");
            M4A1.equipped = false;
            goto gunstore;
        }


    }

    if (gschoice2 == 2)
    {
        system("CLS");
        goto gunstore;
    }

}
else if (Player.money < 0)
{
    system("CLS");
    cout << "You don't have enough money.";
    Sleep(1400);
    goto gunstore;
}

prchs_FAMAS:

if (FAMAS.purchased == true)
{
    cout << "You already purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
    int gschoice7;
    cin >> gschoice7;

    if (gschoice7 == 1)
    {
        system("CLS");
        FAMAS.equipped = true;
        M4A1.equipped = false;
        goto mmenu;
    }
    else if (gschoice7 == 2)
    {
        goto gunstore;
    }
}



    if (Player.money >= 100)
    {
        system("CLS");
        cout << "Would you like to buy the FAMAS?";
        cout << "\n\n1)Yes\n2)No\n\nAction: ";

        int gschoice4;
        cin >> gschoice4;

        if (gschoice4 == 1)
        {
            system("CLS");
            Player.money = Player.money - FAMAS.cost;
            FAMAS.purchased = true;
            cout << "You've purchased the FAMAS. Would you like to equip it?\n\n1)Yes\n2)No\n\nAction: ";
            int gschoice5;
            cin >> gschoice5;

            if (gschoice5 == 1)
            {
                system("CLS");
                FAMAS.equipped = true;
                M4A1.equipped = false;
                cout << "You've equipped the FAMAS";
                Sleep(1400);
                goto gunstore;
            }

            if (gschoice5 == 2)
            {
                system("CLS");
                FAMAS.equipped = false;
                goto gunstore;
            }


        }

        if (gschoice4 == 2)
        {
            system("CLS");
            goto gunstore;
        }

    }
    else if (Player.money < 100)
    {
        system("CLS");
        cout << "You don't have enough money.";
        Sleep(1400);
        goto gunstore;
    }
    //-----GUNSTORE-----




}
Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
Colby Ryan Freeman
  • 325
  • 1
  • 3
  • 8
  • 2
    No, it didn't "just happen". You changed something! – Lightness Races in Orbit Aug 27 '14 at 23:31
  • 1
    I know what `cout` and `cin` are likely to be. What's `System`? – T.C. Aug 27 '14 at 23:32
  • "Suddenly... ambiguity". Were gonna need some details. – Borgleader Aug 27 '14 at 23:32
  • 4
    "The red scribbly line" implies intellisense in an IDE of some sort. Please be aware that there are many of these, and many programmers don't even use an IDE. Therefore, if your question is about the behaviour of some IDE, you need to include prominently in the question _which one_. – Lightness Races in Orbit Aug 27 '14 at 23:33
  • 'intellisense' - doomed already.. – Martin James Aug 27 '14 at 23:34
  • @MartinJames: You are just obsessed with Doom – Lightness Races in Orbit Aug 27 '14 at 23:34
  • 8
    And now a huge code dump. :( I don't want to discourage you, @Colby, because I know you are a new, young coder. I want you to continue trying and experiementing because that's _good_! You should probably be made aware, though, that your questions aren't likely to find much footing on SO because it's not really a site for teaching from scratch, and your questions are showing a pattern of not following best practices. I suggest having a read through the Help material, and some of the higher scoring questions, to see what we do here on SO. Good luck! – Lightness Races in Orbit Aug 27 '14 at 23:36
  • Okay, you're right :) – Colby Ryan Freeman Aug 27 '14 at 23:37
  • 4
    Why do you include both `` and ``? – T.C. Aug 27 '14 at 23:37
  • I was following a tutorial and someone did it... I don't really know what it does, TBH – Colby Ryan Freeman Aug 27 '14 at 23:40
  • @ColbyRyanFreeman: You really shouldn't add lines of code for no apparent reason. Understand the lines of code before you add them into your program. – Lightness Races in Orbit Aug 27 '14 at 23:40
  • Where you have labels like `gunstore:`, you should probably be calling functions instead of jumping to labels. There are occasions to use `goto` and a label; your code is not one of them. – Jonathan Leffler Aug 27 '14 at 23:43
  • Here is a hint on why ambiguity might happened: https://stackoverflow.com/questions/10150468/how-to-redirect-cin-and-cout-to-files/38504880#38504880 – Vadzim Dec 20 '17 at 11:38
  • This just happened to me in Visual Studio 2019. Exact same error. AND I HADN'T CHANGED A THING. Closed the project. Closed VS. Reopened everything, and the issue was gone. – unqualified Nov 21 '20 at 22:38
  • @peterh Why did you approve [this edit](https://stackoverflow.com/review/suggested-edits/28127232)? – Asteroids With Wings Jan 21 '21 at 17:08
  • @unqualified I just had the same issue in VS Code. In my case, I didn't close the project. After awhile, that error just disappeared. – hyena Jan 03 '22 at 21:55

1 Answers1

32

This kind of thing doesn't just magically happen on its own; you changed something! In industry we use version control to make regular savepoints, so when something goes wrong we can trace back the specific changes we made that resulted in that problem.

Since you haven't done that here, we can only really guess. In Visual Studio, Intellisense (the technology that gives you auto-complete dropdowns and those squiggly red lines) works separately from the actual C++ compiler under the bonnet, and sometimes gets things a bit wrong.

In this case I'd ask why you're including both cstdlib and stdlib.h; you should only use one of them, and I recommend the former. They are basically the same header, a C header, but cstdlib puts them in the namespace std in order to "C++-ise" them. In theory, including both wouldn't conflict but, well, this is Microsoft we're talking about. Their C++ toolchain sometimes leaves something to be desired. Any time the Intellisense disagrees with the compiler has to be considered a bug, whichever way you look at it!

Anyway, your use of using namespace std (which I would recommend against, in future) means that std::system from cstdlib now conflicts with system from stdlib.h. I can't explain what's going on with std::cout and std::cin.

Try removing #include <stdlib.h> and see what happens.

If your program is building successfully then you don't need to worry too much about this, but I can imagine the false positives being annoying when you're working in your IDE.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 3
    I wouldn't usually answer such a low-quality question but, as I indicated in my comment above, I want to _encourage_ new, young programmers to keep experimenting and learning. However, since Stack Overflow is not a forum or discussion area, I would ask you to check out the Help area and any existing _upvoted_ C++ questions, to see what it is that we get up to here on Stack Overflow! – Lightness Races in Orbit Aug 27 '14 at 23:42
  • So when you were saying this code isn't right, how? It works the way I want it to work. Is there a better way? – Colby Ryan Freeman Aug 27 '14 at 23:44
  • @ColbyRyanFreeman: I didn't, did I? There are a number of style-related changes I would make in order to have this program look like a more conventional C++ program, but for a beginner (particularly if it's working) then I'd say it's fine for now. In time you'll stop using gotos and you'll stop encapsulating _everything_ inside `main`, and you'll stop duplicating code and your code will include documenting comments, and you'll start using functions. For now, at your level, I'm satisfied with it. I don't recall saying that it is "not right"! – Lightness Races in Orbit Aug 27 '14 at 23:45
  • 1
    My theory is that Intellisense might be reporting an ambiguity between, e.g., `::system` and `std::system` thanks to the `using namespace std;`. – T.C. Aug 27 '14 at 23:48
  • @T.C.: Yeah it sounds like they frakked up wrapping `stdlib.h` to produce `cstdlib`. Any place that Intellisense disagrees with the compiler is patently a bug whichever way you look at it. _Go figure_! – Lightness Races in Orbit Aug 27 '14 at 23:48
  • @Lightness Races in Orbit: Oh, sorry. That must have been someone else. But yes, I will start using Functions after this program. I just don't understand how to not "duplicate" code. Would I do that with classes? – Colby Ryan Freeman Aug 27 '14 at 23:49
  • I think we should encourage beginner not use "using namespace std" because it brings more trouble than convenience. – xis Aug 27 '14 at 23:49
  • @Colby: Sometimes! Classes, functions, arrays, and just writing terser code. In time you'll develop a knack for spotting the opportunities to make your code shorter. It's not something I can teach in a comment thread, though. – Lightness Races in Orbit Aug 27 '14 at 23:50
  • @ColbyRyanFreeman: You're welcome and good luck. Don't forget to take on board what I said about Stack Overflow — there are some good chatrooms and forums out there which may be able to help mentor you through the first few years. Stack Overflow is a Q&A repository rather than a helpdesk, though. – Lightness Races in Orbit Aug 27 '14 at 23:52
  • Note that the VC++ compiler pulls things from the `std::` namespace into the global namespace at the slightest provocation - or sometimes no provocation at all. Its STL implementation is also pretty good at including headers you didn't think you'd included - so it's very possible to end up with the same STL header included twice. Perhaps stdlib.h includes iostream.h? – Tom Aug 28 '14 at 00:19
  • @Tom: It's not the compiler causing a problem here; it's Intellisense, which uses a separate system. And there is no `iostream.h`. – Lightness Races in Orbit Aug 28 '14 at 02:53
  • Hmmm, when did MS get rid of that? Probably a long long time ago, but I hadn't noticed. – Tom Aug 28 '14 at 03:48
  • Yes, but I'd guess that intellisense is just as keen on muddling namespaces as the compiler. – Tom Aug 28 '14 at 03:49
  • @Tom: [2003](http://msdn.microsoft.com/en-us/library/8h8eh904(v=vs.90).aspx). – Lightness Races in Orbit Aug 28 '14 at 13:56
  • Just run it once. The error will be gone..!! It was so, for me...!!! – Samitha Chathuranga Nov 16 '15 at 19:58
  • 6
    The same thing happened to me in VS 2017. I confirmed no changes via a diff to my source control. I restarted VS and then reopened my project and the problem had gone away. – Chris Arbogast Sep 12 '19 at 22:42
  • 2
    Same thing happened in VS 2019. Commented out `#include `, built (lots of errors), removed the comment, rebuilt and it all cleared up. Restarting VS would probably have fixed it too. I'm guess some sort of Intellisense bug. – AlainD Jul 14 '20 at 10:39
  • 1
    I've faced the same situation in VS 2020. I restarted the IDE and it was gone. – Naveen Nov 17 '20 at 18:58