C++ File editing without deleting.other information - c++

I am programming a stock trading program.
For this function, I need to change the balance in a specific account, but whenever I output back to the file, it updates the balance for one account and deletes everyone else.
Source Code:
void Buying()
{
ifstream Companies;
ifstream Account;
Account.open("Account.txt");
{
int ID;
double cash;
string sym;
ifstream Account;
Account.open("Account.txt");
//Check for errord
if (Account.fail())
{
cout << "Failed" << endl;
exit(1);
}
account S_accounts;
for (int i = 0; i < 5; i++)
{
Account >> S_accounts.id[i] >> S_accounts.cash[i];
}
cout << "Please enter your last 4 digits of your ID: ";
do
{
cin >> ID;
cout << endl;
switch (ID)
{
case 1111:
cash = S_accounts.cash[0];
break;
case 2222:
cash = S_accounts.cash[1];
break;
case 3333:
cash = S_accounts.cash[2];
break;
case 4444:
cash = S_accounts.cash[3];
break;
case 5555:
cash = S_accounts.cash[4];
break;
default:
cout << "ID number is invalid. please try again." << endl;
}
break;
} while (ID != 1111 || ID != 2222 || ID != 3333 || ID != 4444 || ID != 5555);
if (cash > 36.80)
{
cout << "You have $" << cash << " what stock would you like to purchase?" << endl;
listing();
cout << endl;
cout << "Type the stock symbol of the company that you would like to purchase." << endl;
cin >> sym;
double price;
int number_of_stock;
double total;
int i = 0;
while (i < 7)
{
Stock S_companies;
Companies >> S_companies.price[i];
i++;
}
//Stock S_companies;
if (sym == "AAPL" || sym== "aapl")
{
price = 450.00;
}
else if (sym == "BA" || sym == "ba")
{
price = 75.50;
}
else if (sym == "INTC" || sym == "intc")
{
price = 22.30;
}
else if (sym == "RMBS" || sym == "rmbs")
{
price = 5.55;
}
if (sym == "SIRI" || sym == "siri")
{
price = 3.15;
}
if (sym == "SWKS" || sym == "swks")
{
price = 25.35;
}
if (sym == "XLNX" || sym == "xlnx")
{
price = 36.80;
}
cout << "How many stock(s) would you like to buy?" << endl;
cin >> number_of_stock;
total = number_of_stock*price;
if (cash > total)
{
account S_account;
ofstream Oaccount;
Oaccount.open("Account.txt");
if (Oaccount.fail())
{
"Failed to open the file.";
}
I know this is where I need to edit but I am absolutely clueless on what to do so it only changes the account balance for the person that logged in using their ID.
if (ID == 1111)
{
Oaccount << S_accounts.id[0] << '\t' << '\t' << S_accounts.cash[0] - total;
}
Oaccount.close();
}
else
cout << "You do not have enough money.";
}
else
{
cout << "Sorry, you do not have enough money in your account.";
exit(1);
}
Account.close();
}
}

Im not debugging your code.But This is how you update/modify an entry...
let number be the value with which you want to find an account...lets say you want to change his name to newname...
while(!file.eof())
{
if(obj.number==given_value)
{
file.tellg(pos);
{
//modify values here like..
strcpy(obj.name,newname);
file.seekg(pos);
file.write((char*)&obj,sizeof(obj));
}
}
}

Related

How to have all options in one function?

So i'm making a text adventure game and I want to be able to have a function which has all the inputs in it. So instead of every time me making an if statement to check for every direction I can just put a function that has all of that in it. How do I make this?
string input;
while (input != "n", "s", "e", "w")
{
getline(cin, input);
if (input == "n")
{
dTable();
}
if (input == "e")
{
cout << "You sit on the sofa. Congratulations" << endl;
}
if (input == "w")
{
cout << "You just ran into a wall" << endl;
}
if (input == "s")
{
outsideHouse();
}
if (input == "stats")
{
stats();
}
if (input == "help")
{
help();
}
else
{
cout << "Sorry I am not Siri I don't understand" << endl;
getline(cin, input);
}
break;
}
return 0;
}
You see different approaches of using functions in here,
thats how I would solve this.
You can focus on adding new questions in the main() part and doesn't have to write
all the if-requests over and over.
#include <iostream>
#include <string>
using namespace std;
void help();
void stats();
string check(string input,string n,string s, string w, string e)
{
string result;
if (input == "n"){
result = n;}
else if (input == "s"){
result = s;}
else if (input == "w"){
result = w;}
else if (input == "e"){
result = e;}
else if (input == "help"){
cout << "help" << endl;} //help()
else if (input == "stats"){
cout << "stats" << endl;} // stats()
else if (input == "exit"){
result = "exit";}
else
cout << "Sorry I am not Siri I don't understand. Try again" << endl;
return result;
}
string call()
{
string input;
cout << "Where do you want to go? n s w e" << endl;
getline(cin, input);
return input;
}
int main()
{
bool slope = false;
string input, result;
input = call();
result = check(input,"outside","dTable", "You just ran into a wall", "You sit on the sofa. Congratulations");
while (slope != true)
{
if(result == "house")//room1
{
cout << "You are in the house" << endl;
input = call();
result = check(input,"outside","dTable", "You just ran into a wall", "You sit on the sofa. Congratulations");
}
else if(result == "outside")//room2
{
cout << "You are outside"<< endl;
input = call();
result = check(input,"You just ran into a wall", "house","dTable", "You sit on the sofa. Congratulations");
}
else if(result == "dTable")//room3
{
cout << "You are dTable" << endl;
input = call();
result = check(input,"You just ran into a wall", "You sit on the sofa. Congratulations","outside","house");
} ///adding one room after another and connect it with each other
else if(result == "exit")
{
slope = true;
}
else
{
cout << result << endl;
input = call();
//some winning or loosing message here
}
}
return 0;
}

C++ . Accessing Objects and Setting Values

Hello StackOverflow community.
I cant seem to change a value on a class.
I am new to C++, and I have been looking for a solution but I cant seem to figure it out.
This is my Account.cpp
#include "Account.h"
#include <iostream>
using namespace std;
Account::Account(string accName, int accBalance)
{
name = accName;
balance = accBalance;
std::vector<std::string> Report();
}
bool Account::Deposit(Account a,int amt) {
if (amt >= 0) {
a.balance += amt;
return 1;
}
else
return 0;
}
bool Account::Withdraw(Account a, int amt) {
if (amt >= 0 && (a.balance - amt > 0)) {
a.balance -= amt;
return 1;
}
else
return 0;
}
int equal(string a, string b) {
if (a == b) return 1;
else return 0;
}
Account::~Account()
{
}
This is my main.cpp
int main() {
int aux_bal;
string aux_name;
int choice;
std::string InputName;
std::list< Account > arr;
std::list<Account>::iterator result;
do
{
cout << endl
<< " 1 - Create New Account.\n"
<< " 2 - View Balance.\n"
<< " 3 - Make a Deposit\n"
<< " 4 - Make a Withdraw\n"
<< " 5 - Check log\n"
<< " 6 - Exit.\n"
<< " Enter your choice and press return: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Input holder's account name: \n";
cin >> InputName;
arr.push_back(Account(InputName, 0));
break;
case 2:
cout << "Enter Account Name:\n";
cin >> InputName;
for (result = arr.begin(); result != arr.end(); result++) {
int aux_bal = result->balance;
std::string aux_name = result->name;
if (aux_name == InputName) {
cout <<"Account Balance:"<< aux_bal << endl;
cout << "Account Holder:" << aux_name << endl;
}
}
break;
case 3:
cout << "Enter Account Name:\n";
cin >> InputName;
for (result = arr.begin(); result != arr.end(); result++) {
int aux_bal = result->balance;
std::string aux_name = result->name;
if (aux_name == InputName) {
cout << "Enter ammount to Deposit:\n";
cin >> aux_bal;
(*result).Deposit(*result,aux_bal);
}
}
break;
case 4:
//code to help the user like give him
//extra information about the mode and the controller
break;
case 5:
break;
case 6:cout << "End of Program.\n";
break;
default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}
} while (choice != 6);
return 0;
}
For some reason when I output the account info after making a Deposit, the Balance value remains unaltered at 0.
Any help please?
The deposit function should act on the account object that it belongs to, you don't need to pass it as an argument. Also, integers are not booleans. Lastly, why is the deposit amount an int? What if I want to deposit $20.25 ?
bool Account::Deposit(int depositAmt)
{
if (depositAmt >= 0) {
this->balance += depositAmt; // "this->" is optional
return true;
}
else
return false;
}

C++ How to add the sum from values in multiple if statements

I'm having an issue in gathering and printing the total the sum(totalPrice) in my program.
The program repeatedly asks the user to enter ticket information and then calculates the total price for all the tickets entered.
If a ticket is requested to some other destination, the program informs the user that no tickets are available to that destination. The user can enter the city names as given or in all lower case.
The round trip price is the one-way price multiplied by 1.6. There is a 20% discount for students.
When the user enters the word “Done” or “done” when asked for the destination, the program outputs the total number of tickets and their total price and exits.
Everything is working fine as far as I know, except the final output of for total cost. Any help guys as to what I'm doing. I believe it might be my logic, but I'm too invested to see my mistake! Any help will be appreciated, thank you :)
using namespace std;
int main() {
double totalPrice = 0; // the sum of all the values after the do while loop is done.
int numberOfTicketsSold; // ehhhh counter?
int counter; // keep track of the number of flights
double temp = 0; // to hold the values while they're being adjusted
double discount = 0; // another temp but for discount
double boston = 350; // constant at 350 per trip
double london = 600;
double paris = 700;
double tokyo = 800;
string input; // might have to use char instead. Lets try it out anyways.
string roundTrip;
string studentDiscount;
cout << "**************************************************************" << endl;
cout << "Enter the information for each ticket." << endl;
cout << "When no more tickets to enter, enter 'Done' for destination." << endl;
cout << "**************************************************************" << endl;
do{
cout << "Enter the destination city:" ; // Want to collect the string inputted to start if statements
cin >> input;
if ( input == "boston" || input == "Boston")
{
counter++; // keeps track of how many flights. For last output
cout << "\nRound trip? [Y/N]" << endl;
cin >> roundTrip;
if (roundTrip == "y" || roundTrip == "Y")
{
temp = boston;
temp = temp * 1.6;
}
else if(roundTrip == "n" || roundTrip == "N")
{
temp = boston;
}
cout << "\nStudent Discount? [Y/N]";
cin >> studentDiscount;
if (studentDiscount == "y" || studentDiscount == "Y")
{
discount = temp;
discount = discount *.20;
temp = temp - discount;
totalPrice = temp;
}
else if(studentDiscount == "N" || studentDiscount == "n")
{
totalPrice = temp;// do nothing right?
}
}
else if ( input == "london" || input == "London")
{
counter++; // keeps track of how many flights. For last output
cout << "\nRound trip? [Y/N]" << endl;
cin >> roundTrip;
if (roundTrip == "y" || roundTrip == "Y")
{
temp = london;
temp = temp * 1.6;
}
else if(roundTrip == "n" || roundTrip == "N")
{
temp = london;
}
cout << "\nStudent Discount? [Y/N]";
cin >> studentDiscount;
if (studentDiscount == "y" || studentDiscount == "Y")
{
discount = temp; // 600
discount = discount *.20;
temp = temp - discount;
totalPrice = totalPrice + temp;
}
else if(studentDiscount == "N" || studentDiscount == "n")
{
totalPrice = totalPrice + temp;// do nothing right?
}
}
else if ( input == "paris" || input == "Paris")
{
counter++; // keeps track of how many flights. For last output
cout << "\nRound trip? [Y/N]" << endl;
cin >> roundTrip;
if (roundTrip == "y" || roundTrip == "Y")
{
temp = paris;
temp = temp * 1.6;
}
else if(roundTrip == "n" || roundTrip == "N")
{
temp = paris;
}
cout << "\nStudent Discount? [Y/N]";
cin >> studentDiscount;
if (studentDiscount == "y" || studentDiscount == "Y")
{
discount = temp;
discount = discount *.20;
temp = temp - discount;
totalPrice = temp;
}
else if(studentDiscount == "N" || studentDiscount == "n")
{
totalPrice = temp;// do nothing right?
}
}
else if ( input == "tokyo" || input == "Tokyo")
{
counter++; // keeps track of how many flights. For last output
cout << "\nRound trip? [Y/N]" << endl;
cin >> roundTrip;
if (roundTrip == "y" || roundTrip == "Y")
{
temp = tokyo;
temp = temp * 1.6;
}
else if(roundTrip == "n" || roundTrip == "N")
{
temp = tokyo;
}
cout << "\nStudent Discount? [Y/N]";
cin >> studentDiscount;
if (studentDiscount == "y" || studentDiscount == "Y")
{
discount = temp;
discount = discount *.20;
temp = temp - discount;
totalPrice = temp;
}
else if(studentDiscount == "N" || studentDiscount == "n")
{
totalPrice = temp;// do nothing right?
}
}
} while (input != "done");
cout << "--------------------------------------------------------------"<< endl;
cout << counter << " tickets sold for a total sum of $" << temp << endl;
cout << "-------------------------------------------------------------";
}

Tic Tac Toe , Why am I getting the error non standard syntax use & to create a pointer to a member [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
here is my main . I get the error for the part I am checking whether the game had a winner or not. the errors are referring to the if parts where i wanna check the returned data from chkwin method
#include<iostream>
#include<string>
#include "TicTacToe.h"
using namespace std;
int main()
{
string choice = "";
string name1 = "";
string name2 = "";
string change = "";
int choice_num;
TicTacToe game;
cout << "Welcome to TicTacToe World!\n"
<< "In order to Start Please Enter the name of the first player\n\n";
getline(cin, name1);
cout << "\nGreat, Now Please Enter the name of the second player\n\n";
getline(cin,
name2);
cout << "\nAwesome Let's Get Started!\n";
do
{
cout << "\nPlease choose what do you want to do by entering the number of your choice\n"
<< "1.Start the game.\n"
<< "2.Change the names.\n"
<< "3.View Scores\n"
<< "4.Exit the Game :(\n\n";
getline(cin, choice);
if (choice == "1")
{
for (int i = 1; 1 <= 9; i++)
{
if (i % 2 != 0)
{
cout << "It's " << name1 << " Turn. Please Make Your move.";
cin >> choice_num;
cin.ignore();
game.setMove(choice_num, 1);
if (game.chkWin == 1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout <<"The Game is a Draw! ";
}
}
else
{
cout << "It's " << name1 << " Turn. Please Make Your move.";
cin >> choice;
cin.ignore();
game.setMove(choice_num, 2);
if ( 1 == game.chkWin)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout << "The Game is a Draw! ";
}
}
}
}
else if (choice == "2")
{
do
{
cout << "\nwhich player do you want its name to be changed? (Enter 1 for the first, 2 for the second and 3 for both)\n";
getline(cin, change);
if (change == "1")
{
cout << "\nPlease Enter the new name for the player one.\n";
getline(cin, name1);
}
else if (change == "2")
{
cout << "\nPlease Enter the new name for the player two.\n";
getline(cin, name2);
}
else if (change == "3")
{
cout << "\nPlease Enter the new name for the player one.\n";
getline(cin, name1);
cout << "\nPlease Enter the new name for the player two.\n";
getline(cin, name2);
}
else
{
cout << "\nPlease Enter a Valid Choice.\n";
}
} while (change != "1" && change != "2" && change != "3");
}
else if (choice == "3")
{
cout << "\n" << game.getResults(name1, name2);
}
else if (choice != "4")
{
cout << "\nPlease Enter a Correct number\n";
}
} while (choice!= "4");
cout << "\nFinal Results are: \n\n"
<< game.getResults(name1, name2);
cout << "\nThank you for using our program. Hope to see You Again. Bye Bye!!\n";
system("Pause");
return 0;
}
my class
#include<iostream>
#include<string>
using namespace std;
#ifndef TICTACTOE_H
#define TICTACTOE_H
class TicTacToe
{
private:
const static int SIZE = 3;
int table[SIZE][SIZE];
int results[SIZE];
int winner = 2;
public:
TicTacToe();
void setMove(int , int );
int chkWin();
string getResults(string , string );
};
#endif
here is my methods declaration:
#include<iostream>
#include<string>
#include "TicTacToe.h"
using namespace std;
TicTacToe::TicTacToe()
{
for (int i = 0; i < SIZE; i++)
{
results[i] = 0;
for (int j = 0; j < SIZE; j++)
{
table[i][j] = 0;
}
}
}
void TicTacToe::setMove(int place, int player)
{
int field = place;
if (field == 1)
{
if (table[0][0] == 0)
{
table[0][0] = player;
}
else
{
cout << "\nYou cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 2)
{
if (table[0][1] == 0)
{
table[0][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 3)
{
if (table[0][2] == 0)
{
table[0][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 4)
{
if (table[1][0] == 0)
{
table[1][0] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 5)
{
if (table[1][1] == 0)
{
table[1][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 6)
{
if (table[1][2] == 0)
{
table[1][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 7)
{
if (table[2][0] == 0)
{
table[2][0] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 8)
{
if (table[2][1] == 0)
{
table[2][1] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
else if (field == 9)
{
if (table[2][2] == 0)
{
table[2][2] = player;
}
else
{
cout << "You cannot make this move this place is already occupied.Please choose another Field\n";
cin >> field;
cin.ignore();
setMove(field, player);
}
}
}
int TicTacToe::chkWin()
{
for (int i = 0; i < SIZE; i++)
{
if (table[i][0] == table[i][1] && table[i][0] == table[i][2])
{
if (table[i][0] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[i][0] == 2)
{
results[2] += 1;
winner = -1;
}
}
else if (table[0][i] == table[1][i] && table[0][i] == table[2][i])
{
if (table[0][i] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[0][i] == 2)
{
results[2] += 1;
winner = -1;
}
}
}
if ((table[0][0] == table[1][1] && table[0][0] == table[2][2]) || (table[0][2] == table[1][1] && table[0][2] == table[2][0]))
{
if (table[1][1] == 1)
{
results[0] += 1;
winner = 1;
}
else if (table[1][1] == 2)
{
results[2] += 1;
winner = -1;
}
}
else
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if(table[i][j] == 0)
{
winner = 2;
}
}
}
results[1] += 1;;
return winner;
}
}
string TicTacToe::getResults(string name1, string name2)
{
return ( name1 + " : " + to_string(results[0]) + "\n"
+ "Draws : " + to_string(results[1]) + "\n"
+ name2 + " : " + to_string(results[2]) + "\n");
}
Compareing functions with integers doesn't make sense. You have to use () operator to call functions like this:
if (game.chkWin() == 1)
instead of
if (game.chkWin == 1)
Poor:
game.setMove(choice_num, 1);
if (game.chkWin == 1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == -1)
{
cout << name1 << " has won this Game!";
}
else if (game.chkWin == 0)
{
cout <<"The Game is a Draw! ";
}
Better:
game.setMove(choice_num, 1);
switch (game.chkWin()) {
case 1:
cout << name1 << " has won this Game!";
break;
case -1:
cout << name1 << " has won this Game!";
break;
case 0:
cout <<"The Game is a Draw! ";
break;
}
The basic problem is using "==" with function game.chkWin, instead of with the result of function game.chkWin().
But in cases like this (pun intended) a "switch()" block is probably more readable and better style than multiple "if/else" statements.
'Hope that helps...

C++ Cout & Cin & System "Ambiguous" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
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-----
}
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.