Can someone help a complete noob to C++?

Discussion in 'Software' started by MicronBOMB, Oct 25, 2009.

  1. MicronBOMB

    MicronBOMB Private E-2

    I'm trying to learn C++ and so to practice my newly learned skills (as minimal as they are) I am making a simple text-based game. The game requires a loop to function properly ( thats the conclusion I came to anyway) but for some reason I keep getting errors when compiling. I don't think the game is 100% appropriate for the forum otherwise I would just post it but if someone is willing to help please contact me on skype under Zachery <snip>. Thanks everyone!
     
    Last edited by a moderator: Oct 26, 2009
  2. MicronBOMB

    MicronBOMB Private E-2

    This is the code for some reason whenever I introduce the do{ }while statement to loop the program until you dont have any men left I keep getting syntax errors when compiling. I did finally get it to run (not in its current state however) but the program didn't loop like I wanted. Any ideas?

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


    int main()
    {
    signed int cash;
    short spice;
    short sugar;
    short tea;
    short men;
    signed short spicecost;
    signed short sugarcost;
    signed short teacost;
    signed short mencost;
    cash = 10000;
    men = 1;
    sugar = 0;
    spice = 0;
    tea = 0;
    spicecost = -1500;
    sugarcost = -1000;
    teacost = -2000;
    mencost = -2000;
    short x;
    short y;
    short z;

    do{

    cout << "So you wanna make some money?"<< endl;
    system ("PAUSE");
    cout << "The object of this game is to make as much money as you can before you die!"<< endl;
    cout << "You must balence risk vs. payoff and you only get 30 turns."<< endl;
    system ("PAUSE");
    cout << "Each time you get caught you will lose one man if your already down to one man"<<endl<< " That means..."<< endl << "You lose!"<< endl;
    system("PAUSE");
    cout << "Sugar is $1000 per pound with a pay off of $200 dollars profit and a risk factor of 1 in 20. That means 1/20 of the time you go to trade pirates will raid you ship and you will lose a man."<< endl;
    cout << "Spice is $1500 per pound with a pay off of $500 dollars profit and a risk factor of 1 in 15."<< endl;
    cout << "Tea is $2000 per pound with a pay off of $1000 dollars profit and a risk factor of 1 in 10."<< endl;
    cout << "Men cost $2000 a peice.";
    system ("PAUSE");


    cout << "So whats your next move?"<< endl<< "Type 1 to buy sugar, 2 to buy spice, 3 to buy tea, or 4 to buy men."<< endl;
    cin >> x;
    if (x == 4){
    men++;
    cash += mencost;
    cout << "You have recrutied one man. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;}
    else if (x == 3) {
    tea++;
    cash += teacost;
    cout << "You have bought 1 lb. of tea. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;}
    else if (x == 2) {
    spice;
    cash += spicecost;
    cout << "You have bought 1 lb. of spice. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;}
    else if (x == 1) {
    sugar++;
    cash += sugarcost;
    cout << "You have bought 1 lb. of sugar. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;}
    cout<<"Time to make that gold."<<endl;
    system ("PAUSE");
    cout<<"Type 1 to sell sugar, 2 for spice, 3 for tea."<<endl;
    cin>> y;

    if (y == 1){

    int random_integer;

    for(int index=0; index<1; index++){

    z = (rand()%20)+1;

    cout << "You rolled a "<< z << endl;

    system ("PAUSE");

    if (z == 10){
    cout<< "You got raided by pirates... You lose one man."<<endl;
    men -= 1;
    cout<<"You've got "<<men<<" men."<<endl; }

    else if (z != 10){
    sugar -= 1;
    cash += 1200;
    cout << "You have sold 1 lb. of sugar. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;}

    }while (men > 0);
    }}

    cout<<"you lose"<<endl;
    system ("PAUSE"); }}
     
  3. JJJIrish05

    JJJIrish05 Sergeant

    What errors are you getting? What exactly isn't working?
     
  4. MicronBOMB

    MicronBOMB Private E-2

    I kept getting an error saying there is a missing } before the while statement. I have eventually figured that out and the program compiled correctly however the program is still not behaving like I want it to. Maybe I just don't fully understand the do{ } while statement or am not using it correctly. I just want the program to repeat the buying and selling stages of the game until the player doesn't have any lives left.
     
  5. JJJIrish05

    JJJIrish05 Sergeant

    Looks fine to me, but I'm still not sure what you're expecting to happen. Use a cout right before the end of the do while to print out how many men you have left for debugging sake and remove it later. If explain what's happening and what you want to happen we can try to figure out what's going wrong for you.
     
  6. MicronBOMB

    MicronBOMB Private E-2

    I want the program to continue looping the buying and selling portion of the game while men > 0. Basically when you lose all your men I want the program to proceed to a game over statement or whatever...
     
  7. Wyatt_Earp

    Wyatt_Earp MajorGeek

    Your curly brackets don't look correct to me. For instance, where does the for loop end? Not to mention, what is it's purpose?
     
  8. utgeek

    utgeek Private E-2

    if i remember ok look hear soz if site doesnt help sure it will www.w3schools.com
     
  9. JJJIrish05

    JJJIrish05 Sergeant

    Yeah your brackets were screwed up. You had 1 extra end bracket you didn't need and your while statement was 2 brackets too early. Here is the correct version. Notice the indenting and lining up the brackets so you can easily visually check the brackets match up correctly.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <ctime>
    using namespace std;
    
    
    int main()
    {
        signed int cash;
        short spice;
        short sugar;
        short tea;
        short men;
        signed short spicecost;
        signed short sugarcost;
        signed short teacost;
        signed short mencost;
        cash = 10000;
        men = 1;
        sugar = 0;
        spice = 0;
        tea = 0;
        spicecost = -1500;
        sugarcost = -1000;
        teacost = -2000;
        mencost = -2000;
        short x;
        short y;
        short z;
        do{
            cout << "So you wanna make some money?"<< endl;
            system ("PAUSE");
            cout << "The object of this game is to make as much money as you can before you die!"<< endl;
            cout << "You must balence risk vs. payoff and you only get 30 turns."<< endl;
            system ("PAUSE");
            cout << "Each time you get caught you will lose one man if your already down to one man"<<endl<< " That means..."<< endl << "You lose!"<< endl;
            system("PAUSE");
            cout << "Sugar is $1000 per pound with a pay off of $200 dollars profit and a risk factor of 1 in 20. That means 1/20 of the time you go to trade pirates will raid you ship and you will lose a man."<< endl;
            cout << "Spice is $1500 per pound with a pay off of $500 dollars profit and a risk factor of 1 in 15."<< endl;
            cout << "Tea is $2000 per pound with a pay off of $1000 dollars profit and a risk factor of 1 in 10."<< endl;
            cout << "Men cost $2000 a peice.";
            system ("PAUSE");
            cout << "So whats your next move?"<< endl<< "Type 1 to buy sugar, 2 to buy spice, 3 to buy tea, or 4 to buy men."<< endl;
            cin >> x;
            if (x == 4){
                men++;
                cash += mencost;
                cout << "You have recrutied one man. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;
            }else if (x == 3) {
                tea++;
                cash += teacost;
                cout << "You have bought 1 lb. of tea. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;
            }else if (x == 2) {
                spice;
                cash += spicecost;
                cout << "You have bought 1 lb. of spice. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;
            }else if (x == 1) {
                sugar++;
                cash += sugarcost;
                cout << "You have bought 1 lb. of sugar. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;
            }
            cout<<"Time to make that gold."<<endl;
            system ("PAUSE");
            cout<<"Type 1 to sell sugar, 2 for spice, 3 for tea."<<endl;
            cin>> y;
            if (y == 1){
                int random_integer;
                for(int index=0; index<1; index++){
                    z = (rand()%20)+1;
                    cout << "You rolled a "<< z << endl;
                    system ("PAUSE");
                    if (z == 10){
                        cout<< "You got raided by pirates... You lose one man."<<endl;
                        men -= 1;
                        cout<<"You've got "<<men<<" men."<<endl;
                    }else if (z != 10){
                        sugar -= 1;
                        cash += 1200;
                        cout << "You have sold 1 lb. of sugar. Your current stats are..."<<endl<<"$"<<cash<<endl<<men<<"men"<<endl<<sugar<<"lbs sugar"<<endl<<spice<<"lbs spice"<<endl<<tea<<"lbs of tea"<< endl;
                    }
                }
            }
        }while (men > 0);
        cout<<"you lose"<<endl;
        system ("PAUSE");
    }
    
     
  10. SWario

    SWario Sergeant

    Having looked at the code posted... How is this game not appropriate? :confused
    Anyway, JJJIrish got it. Brackets - they're important.


    How is that site even relevant to this question? W3Schools is a reference site for WEB programming, not C++.
     
  11. utgeek

    utgeek Private E-2

    ""if i remember ok look hear soz if site doesnt help sure it will www.w3schools.com""
    as said when posted it might not help the site pop'd into my head when reading post and was busy doing other things at the time to check it for him was just trying to help !
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds