Overclock.net banner

1 - 6 of 6 Posts

·
Registered
Joined
·
7 Posts
Discussion Starter · #1 ·
Code:

Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <ctime>
#include <time.h>
#include <cstdlib>

using namespace std;
void Driver();

int main(){
srand (time(NULL));
system("mode 650");
system("pause");
int c1;
int case1c1;
int case2c1;
int case24c1;
int case3c1;
int case3c2;
int membership = 0;
char gymc1;
int gym2;
int gymc3;
int case4c1;
char gymc4;
int gymc2;
int amountw;
int case6takeout;
int case4choice2;
char case24c2;
int Intellect = 20;
int Weight = 120;
int job;
int jobstatus = 0;
char choicechar;
int energy = 100;
int salary;
int depositamount;
int enterbankpin;
int money = 1000;
int bankmoney = 0;
string bankname;
int bankpin;
int pinw;
string bankpass;
label:
cout << "Welcome to a Life Simulator! Enjoy\n What would you like to do?\n1.Rest\n2.Watch TV\n3.Go to Work\n4.Go to the Bank\n5.Go Workout\n6.Exit Game" << endl;
cin >> c1;
switch (c1){
case 1: cout << "I see that you would like to rest on your Bed... How long would you like to rest?\n1.2 Hours+20 Energy\n2.4 Hours+40 Energy\n3. 8 Hours+80 Energy\n4. 12 Hours+100 Energy\n5.Leave Bedroom" << endl;
cin >> case1c1;
switch(case1c1){
case 1: if(energy >= 100){
cout << "You are already fully rested" << endl;
goto label;
}
else{
    cout << "You rested for 2 Hours" << endl;
    energy = energy + 20;
    goto label;
    if(energy >= 100){
        cout << "You are now Fully Rested" << endl;
goto label;
    }
    else{
        cout << "You have got +20 Energy Points... You now have " << energy << " energy Points" << endl;
        goto label;
    }
}
break;
case 2: if(energy >= 100){
cout << "You are already fully rested" << endl;
goto label;
}
else{
    cout << "You rested for 4 Hours" << endl;
    energy = energy + 40;
    if(energy >= 100){
        cout << "You are now Fully Rested" << endl;
        goto label;

    }
    else{
        cout << "You have got +40 Energy Points... You now have " << energy << " Energy Points" << endl;
        goto label;
    }
}
break;
case 3: if(energy >= 100){
cout << "You are already fully rested" << endl;
goto label;
}
else{
    cout << "You rested for 8 Hours" << endl;
    energy = energy + 80;
    if(energy >= 100){
        cout << "You are now Fully Rested" << endl;
        goto label;

    }
    else{
        cout << "You have got +80 Energy Points... You now have " << energy << " Energy Points" << endl;
        goto label;
    }
}
break;
case 4: if(energy >= 100){
cout << "You are already fully rested" << endl;
goto label;
}
else{
    cout << "You rested for 12 Hours" << endl;
    energy = energy + 100;
    if(energy >= 100){
        cout << "You are now Fully Rested" << endl;
        goto label;

    }
    else{
        cout << "You have got +100 Energy Points... You now have " << energy << " Energy Points" << endl;
    }
}
case 5:
    goto label;
}
break;
case 2: cout << "So you want to watch TV... So how long do you want to watch TV\n1.30 Minutes +5 Intellect +5 Pounds\n2.1 Hour +10 Intellect +10 Pounds\n3.2 Hours +20 Intellect +20 Pounds\n4.Enter a Number +?? Intellect +?? Pounds\n5.Leave Living Room" << endl;
cin >> case2c1;
switch(case2c1){
case 1:
cout << "You watch Tv for 30 Minutes and get +5 Intellect and you now have gained 5 Pounds " << endl;
Weight = Weight + 5;
Intellect = Intellect + 5;
cout << "You now have " << Intellect << " Intellect and you weigh " << Weight << " Pounds" << endl;
goto label;
break;
case 2:
cout << "You watch Tv for 1 Hour and get +10 Intellect and you now have gained 10 Pounds " << endl;
Weight = Weight + 10;
Intellect = Intellect + 10;
cout << "You now have " << Intellect << " Intellect and you weigh " << Weight << " Pounds" << endl;
goto label;
break;
case 3:
cout << "You watch Tv for 30 Minutes and get +20 Intellect and you now have gained 20 Pounds " << endl;
Weight = Weight + 20;
Intellect = Intellect + 20;
cout << "You now have " << Intellect << " Intellect and you weigh " << Weight << " Pounds" << endl;
goto label;
break;
case 4:
b:
cout << "How long do you want to watch tv in Hours? " << endl;
cin >> case24c1;
cout << "Are your Sure?\n Y/N" << endl;
cin >> case24c2;
if(case24c2 == 'y'){
    Intellect = case24c1 * 10;
    Weight = case24c1 * 10;
cout << "you now have " << Intellect << " Intellect and you weigh " << Weight << " Pounds" << endl;
goto label;
}
if (case24c2 == 'n'){
    goto b;
}
case 5:
    goto label;
}
break;
case 3:
label2:
    if(jobstatus == 1){
        cout << "Hello Welcome back what would you like to do?\n1.Work\n2.Check Salary\n3.Talk to Boss\n4.Leave Work" << endl;
        cin >> case3c1;
        switch (case3c1){

        case 1:
     if(energy <= 20){
        cout << "You are fatigued, go get some rest" << endl;
    }
    else{
    cout << "You worked for 8 Hours" << endl;
    energy = energy - 60;
    money += salary*8;
    cout << "You made " << salary*8 << "$" << endl;
    }

    if(Weight >= 250){
        energy = energy - 25;
        cout << "Since you weigh more than everyone you get fatigued faster" << endl;
    }
    goto label2;
    break;
    case 2: cout << "Right now your Salary is " << salary << " $ a Hour" << endl;
    goto label2;
    break;
    case 3: cout << "This is a type of gambling game, so you are taking a risk...What do you want to do?\n1.Talk to your Boss\n2.Don't go into his office" << endl;
    cin >> case3c2;
    switch (case3c2){
case 1:
    string gamble[]={"You Get A Raise","You Get A Raise","You Get A Raise","YOURE FIRED!!!","Leave","Leave","Leave","Leave","Leave","YOURE FIRED!!!"};
    string gamble_value = gamble[rand()%8];
    cout << "You boss says  " << gamble_value << endl;
    if(gamble_value == "You Get A Raise"){
    int raise[] = {1,2,3,4,5,6,7,8,9,10};
    int raise_value = raise[rand()%10];
    cout << "You get a " << raise_value << "$ Raise" << endl;
    salary += raise_value;
    cout << "You now make " << salary << "$ an hour" << endl;
    goto label;
    }
    if(gamble_value == "Leave"){
        goto label2;

    }
    if(gamble_value == "YOURE FIRED!!!"){
        cout << "You got fired by your boss, time to get a new job." << endl;
        jobstatus = 0;
        goto label;
    }

    }
case 4: goto label;
    }

    }
    else{
        cout << "So we need to find you a job, one second i got 3 for you to choose from, Here Look" << endl;
        string words[]{"Police Officer", "Vet", "Doctor", "IT", "Nurse", "Repairman", "Waiter", "Window Washer", "Babysitter"};
        string job1 = words[rand()%9];
        string job2 = words[rand()%9];
        string job3 = words[rand()%9];
        label3:
        cout << "\n1." << job1 << "\n2." << job2 << "\n3." << job3 << "\n" << endl;
        cin >> job;

        if(job == 1){
            if(job1 == "Repairman" && Intellect < 100 && Weight > 250){
                cout << "You dont meet the requirements for this job go workout at the gym, or go watch tv to get smarter" << endl;
                goto label3;
            }
            else{
            if(job1 == "Repairman" && Intellect > 100 && Weight < 250){
                cout << "You salary is 10$" << endl;
                salary = 10;
                jobstatus = 1;
                goto label;
            }
            }

            if(job1 == "Police Officer"){
                cout << "You salary is 9$" << endl;
                salary = 9;
                jobstatus = 1;
                goto label;
            }
            if(job1 == "Vet"){
                cout << "You salary is 8$" << endl;
                salary = 8;
                jobstatus = 1;
                goto label;
            }
            if(job1 == "Doctor"){
                cout << "You salary is 7$" << endl;
                salary = 7;
                jobstatus = 1;
                goto label;
            }
            if(job1 == "IT"){
                cout << "You salary is 6$" << endl;
                salary = 6;
                jobstatus = 1;
                goto label;
            }
            if(job1 == "Nurse"){
                cout << "You salary is 5$" << endl;
                salary = 5;
                jobstatus = 1;
                goto label;
            }
            if(job1 == "Waiter"){
                cout << "You salary is 4$" << endl;
                salary = 4;
                jobstatus = 1;
                goto label;
            }
            if(job1 == "Window Washer"){
                cout << "You salary is 3$" << endl;
                salary = 3;
                jobstatus = 1;
                goto label;
            }
            if(job1 == "Babysitter"){
                cout << "You salary is 2$" << endl;
                salary = 2;
                jobstatus = 1;
                goto label;
            }

        }
        if(job == 2){
            cout << "You are now a " << job2 << endl;
            jobstatus = 1;
            if(job2 == "Repairman"){
                cout << "You salary is 10$" << endl;
                salary = 10;
                goto label;
            }
            if(job2 == "Police Officer"){
                cout << "You salary is 9$" << endl;
                salary = 9;
                jobstatus = 1;
                goto label;
            }
            if(job2 == "Vet"){
                cout << "You salary is 8$" << endl;
                salary = 8;
                jobstatus = 1;
                goto label;
            }
            if(job2 == "Doctor"){
                cout << "You salary is 7$" << endl;
                salary = 7;
                jobstatus = 1;
                goto label;
            }
            if(job2 == "IT"){
                cout << "You salary is 6$" << endl;
                salary = 6;
                jobstatus = 1;
                goto label;
            }
            if(job2 == "Nurse"){
                cout << "You salary is 5$" << endl;
                salary = 5;
                jobstatus = 1;
                goto label;
            }
            if(job2 == "Waiter"){
                cout << "You salary is 4$" << endl;
                salary = 4;
                jobstatus = 1;
                goto label;
            }
            if(job2 == "Window Washer"){
                cout << "You salary is 3$" << endl;
                salary = 3;
                jobstatus = 1;
                goto label;
            }
            if(job2 == "Babysitter"){
                cout << "You salary is 2$" << endl;
                salary = 2;
                jobstatus = 1;
                goto label;
            }
        }

        if(job == 3){
            cout << "You are now a " << job3 << endl;
            jobstatus = 1;
            if(job3 == "Repairman"){
                cout << "You salary is 10$" << endl;
                salary = 10;
                goto label;
            }
            if(job3 == "Police Officer"){
                cout << "You salary is 9$" << endl;
                salary = 9;
                jobstatus = 1;
                goto label;
            }
            if(job3 == "Vet"){
                cout << "You salary is 8$" << endl;
                salary = 8;
                jobstatus = 1;
                goto label;
            }
            if(job3 == "Doctor"){
                cout << "You salary is 7$" << endl;
                salary = 7;
                jobstatus = 1;
                goto label;
            }
            if(job3 == "IT"){
                cout << "You salary is 6$" << endl;
                salary = 6;
                jobstatus = 1;
                goto label;
            }
            if(job3 == "Nurse"){
                cout << "You salary is 5$" << endl;
                salary = 5;
                jobstatus = 1;
                goto label;
            }
            if(job3 == "Waiter"){
                cout << "You salary is 4$" << endl;
                salary = 4;
                jobstatus = 1;
                goto label;
            }
            if(job3 == "Window Washer"){
                cout << "You salary is 3$" << endl;
                salary = 3;
                jobstatus = 1;
                goto label;
            }
            if(job3 == "Babysitter"){
                cout << "You salary is 2$" << endl;
                salary = 2;
                jobstatus = 1;
            goto label;
        }

}
    }
break;
case 4:
labelbankstart:
cout << "Welcome to HighTrue Bank, What would you like to do?\n1.Create a Account\n2.Deposit\n3.Withdraw\n4.Leave Bank" << endl;
cin >> case4c1;
switch (case4c1){
case 1: cout << "So you want to create a account, Ok what is your name?" << endl;
cin >> bankname;
labelbank:
cout << "Ok Hello " << bankname << " Please type in what you would like your pin to be:";
cin >> bankpin;
if(bankpin > 9999 || bankpin < 1000) {
    cout << "Please make your pin 4 Digits" << endl;
    goto labelbank;
}
else{
    labelpass:
    cout << "Ok Great now enter a quick password unless you lose your pin number: ";
    cin >> bankpass;
    if(bankpass == bankname){
        cout << "Dont make your password the same as your name" << endl;
        goto labelpass;
    }
    cout << "Ok you're all set, you can now deposit, and withdraw money from the bank" << endl;
goto labelbankstart;
}
break;
case 2:
cout << "Please enter your pin:";
cin >> enterbankpin;
if(enterbankpin == bankpin){
    labeldeposit:
    cout << "Welcome to the depository " << bankname << " How much would you like to deposit today?\n1.20$\n2.40$\n3.60$\n4.80$\n5.100$\n6.Enter a Amount" << endl;
    cin >> depositamount;
    switch (depositamount){
    case 1: if(money < 20){
    cout << "Sorry you dont have enough money to put in" << endl;
    cout << "If you cant put in 20$, you cant put in other amount" << endl;
    goto label;
    }else{
        cout << "-20$" << endl;
        money -= 20;
        bankmoney += 20;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in you bank account" << endl;
        goto labeldeposit;
    }
  case 2: if(money < 40){
    cout << "Sorry you dont have enough money to put in" << endl;
    goto labeldeposit;
    }else{
        cout << "-40$" << endl;
        money -= 40;
        bankmoney += 40;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in you bank account" << endl;
        goto labeldeposit;
    }
    case 3: if(money < 60){
    cout << "Sorry you dont have enough money to put in" << endl;
    goto labeldeposit;
    }else{
        cout << "-60$" << endl;
        money -= 60;
        bankmoney += 60;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in you bank account" << endl;
        goto labelbankstart;
    }
    case 4: if(money < 80){
    cout << "Sorry you dont have enough money to put in" << endl;
    goto labeldeposit;
    }else{
        cout << "-80$" << endl;
        money -= 80;
        bankmoney += 80;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in you bank account" << endl;
        goto labeldeposit;
    }
    case 5: if(money < 100){
    cout << "Sorry you dont have enough money to put in" << endl;
    goto labeldeposit;
    }else{
        cout << "-100$" << endl;
        money -= 100;
        bankmoney += 100;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in you bank account" << endl;
        goto labeldeposit;
    }
    case 6: cout << "Go head and enter how much you would like to take out" << endl;
    cin >> case6takeout;
    if(case6takeout < 20){
        cout << "Sorry you cant take out less than 20 dollars" << endl;
        goto labeldeposit;
    }
    if(money < case6takeout){
        cout << "You dont have enough money to take out sorry" << endl;
        goto labeldeposit;
    }
    else{
        cout << "-" << case6takeout << endl;
        money -= case6takeout;
        bankmoney += case6takeout;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in you bank account" << endl;
        goto label;

    }

    }
    }
    break;
    case 3:
        cout << "Please enter your Pin" << endl;
        cin >> pinw;
        if(pinw == bankpin){
        labelwithdraw:
        cout << "Hello " << bankname << " Welcome to the withdrawing area of the bank... How much would you like to take out today?\n1.20$\n2.40$\n3.60$\n4.80$\n5.100$\n6.Enter a Amount" << endl;
        cin >> case4choice2;
        switch(case4choice2){
        case 1:
        if(bankmoney < 20){
        cout << "You dont have enough money to take out from the bank" << endl;
        goto labelwithdraw;
        }
        else{
        cout << "+20$" << endl;
        money += 20;
        bankmoney -= 20;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in your bank account" << endl;
        goto label;
        }
        break;
         case 2:
        if(bankmoney < 40){
        cout << "You dont have enough money to take out from the bank" << endl;
        goto labelwithdraw;
        }
        else{
        cout << "+40$" << endl;
        money += 40;
        bankmoney -= 40;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in your bank account" << endl;
        goto label;
        }
        break;
         case 3:
        if(bankmoney < 60){
        cout << "You dont have enough money to take out from the bank" << endl;
        goto labelwithdraw;
        }
        else{
        cout << "+60$" << endl;
        money += 60;
        bankmoney -= 60;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in your bank account" << endl;
        goto label;
        }
        break;
         case 4:
        if(bankmoney < 80){
        cout << "You dont have enough money to take out from the bank" << endl;
        goto labelwithdraw;
        }
        else{
        cout << "+80$" << endl;
        money += 80;
        bankmoney -= 80;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in your bank account" << endl;
        goto label;
        }
        break;
         case 5:
        if(bankmoney < 100){
        cout << "You dont have enough money to take out from the bank" << endl;
        goto labelwithdraw;
        }
        else{
        cout << "+100$" << endl;
        money += 100;
        bankmoney -= 100;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in your bank account" << endl;
        goto label;
        }
        break;
        case 6:
        cout << "Enter how much you would like to take out" << endl;
        cin >> amountw;
        cout << "Are you sure?\nY\N" << endl;
        cin >> choicechar;
        if(choicechar == 'Y'){
        if(bankmoney < amountw){
        cout << "You dont have enough money in your bank account take out" << endl;
        goto labelwithdraw;
        }
        else{
        cout << "-" << amountw << endl;
        money += amountw;
        bankmoney -= amountw;
        cout << "You now have " << money << "$ and have " << bankmoney << "$ in your bank account" << endl;
        goto label;
        }
        }
        }
        }
        case 4: goto label;
}
break;
case 5:
labelgymstart:
if(membership == 0){
cout << "Hello Would you like to make a memebership\nY/N" << endl;
cin >> gymc1;
if(gymc1 == 'Y' || 'y'){
cout << "Ok It will cost 250$ to get a membership" << endl;
system("pause");
if(money < 250){
    cout << "You dont have enough to pay for the membership" << endl;
goto label;
}
else{
cout << "-250$" << endl;
money -= 250;
membership = 1;
cout << "Thank you for your service" << endl;
goto label;
}
}
if(gymc1 == 'n' || gymc1 == 'N'){
goto label;

}
}
if(membership == 1){
cout << "Welcome back how long would you like to work out?\n1.1 Hour -10 Weight, -10 Energy\n2.2 Hours -20 Weight, -20 Energy\n3.4 Hours -40 Weight, -40 Energy\n4.6 Hours -60 Weight, -60 Energy\n5.Enter a Amount(Hours)\n6.Leave Gym" << endl;
cin >> gym2;
switch (gym2){
case 1:
if(energy <= 10){
cout << "You are too fatigued to workout, rest and come back later" << endl;
goto labelgymstart;
}
else{
cout << "You lost 10 Weight, and 10 Energy" << endl;
Weight -= 10;
energy -= 10;
cout << "You now weigh " << Weight << " and you have " << energy << " remaining" << endl;
goto label;
}

case 2:
if(energy <= 20){
cout << "You are too fatigued to workout, rest and come back later" << endl;
goto labelgymstart;
}
else{
cout << "You lost 20 Weight, and 20 Energy" << endl;
Weight -= 20;
energy -= 20;
cout << "You now weigh " << Weight << " and you have " << energy << " remaining" << endl;
goto label;
}

case 3:
if(energy <= 40){
    cout << "You are too fatigued to workout" << endl;
goto labelgymstart;
}
else{
cout << "You lost 40 Weight, and 40 Energy" << endl;
Weight -= 40;
energy -= 40;
cout << "You now weigh " << Weight << " and you have " << energy << " remaining" << endl;
goto label;
}
case 4:
if(energy <= 60){
cout << "You are too fatigued to workout, rest and come back later" << endl;
goto labelgymstart;
}
else{
cout << "You lost 60 Weight, and 60 Energy" << endl;
Weight -= 60;
energy -= 60;

goto labelgymstart;
}
break;
case 5:
cout << "Please enter how long you want to workout in hours" << endl;
cin >> gymc3;
if(energy <= gymc3){
    cout << "If you work out this long you could possibly die from a heart attack\nAre you sure you want to keep working out? Y/N" << endl;
    cin >> gymc4;
    if(gymc4 == 'y' || gymc4 == 'Y'){
        string deadono[] = {"Heart Attack", "Your Still Alive"};
        string deadono_value = deadono[rand()%2];
        if(deadono_value == "Heart Attack"){
            cout << "You had a Heart Attack from working out too hard, You are dead" << endl;
        }
        if(deadono_value == "Your Still Alive"){
            cout << "You are still good, go back home and get some rest" << endl;
            energy = -50;
            goto label;
        }
            }
        if(gymc4 == 'n' || gymc4 == 'N' ){
            goto labelgymstart;
        }

}
else{
    if(energy <= 0){
        cout << "You are too fatigued to work out" << endl;
        goto label;
    }
    else{
        cout << "You lost " << gymc3 << " Weight and lost " << gymc3 << " Energy" << endl;
        Weight -= gymc3;
        energy -= gymc3;
        cout << "You now weigh " << Weight << " Pounds and have " << energy << " left" << endl;
        goto label;
    }
}
case 6: goto label;
}
}
break;
case 6:
    cout << "Ok Bye " << endl;
}

}
Dont Judge me
 

·
Registered
Joined
·
7 Posts
Discussion Starter · #2 ·
I know i should've used comments, and classes, but hey i think i did okay on this, i was doing this as my own little assignment, so i could learn the basics of code now i need to figure out how to implement classes, and comments on here
 

·
Registered
Joined
·
653 Posts
It's been a while so I'm not sure if I remember this correctly but I think I've always been thought not to use the goto command because of something with security. I may be wrong though, maybe someone with more experience could answer this.
 

·
Stock *ahem*
Joined
·
6,382 Posts
Honestly, I've found it way less hassle to just code in a "goto" when validating input taken from a scanf, especially when I need to check for multiple valid inputs.

That being said, that many gotos in the code in the first post almost certainly could be replaced with switch-case blocks. I would argue that they are then superfluous since both switch-case and if-then-else blocks will 'fall through' on failure of a test and/or termination of the code block. For example:

Code:

Code:
switch(a)
  {
    case 1:
    code;
    break;
    case 2:
    code;
    break;
    default:
    break;
  }
 

·
Registered
Joined
·
14 Posts
Okey I see several ways of doing this much easier to read.

1. Declare the same type of variables in a single line:

Code:

Code:
int a, b, c;
2. Us a switch statement like Quantum Reality wrote above me:

Code:

Code:
switch(a)  {
    case 1:
    code;
    break;
    case 2:
    code;
    break;
    default:
    break;
}
3. Use lowercase on all variables (looks better to me).

4. Avoid using namespace if it gets bigger.
 

·
Registered
Joined
·
265 Posts
There are not any inherent security flaws with goto, but its use is not recommended because it makes your code more brittle. Generally you want to use a function rather than use goto. Functions also give you new scope, which means you don't have to name variables like case2c1.

I would recommend changing the main outer loop to use functions rather than gotos just to make things much easier to change.

Example:

Code:

Code:
struct item {
 char *description;
 int Weight;
 int Cost;
}
struct person {
 int Weight ;
 int Intellect ; 
 std::vector<item> inventory;
 //etc
}

void WatchTV(person &player){
 cout << "how long?";
 //etc
 player.Weight += 5 * duration;
}
//etc

int main(){
 bool not_exit = true;
 person player;
 while(not_exit){
  switch(prompt_player()){
   case 1: 
    WatchTV(player);
    break;
   case 2:
    something_else(player);
    break;
   default: 
    not_exit=false;
    break;
  }
 }
}
 
1 - 6 of 6 Posts
Top