Hello HypeCoderPanda,
As
salem c said write the program a little at a time compile and test until you have what you want.
I tend to take the same approach when working on a program like yours just a little bit at a time.
So to start with:
You wrote:
1 2 3 4 5 6 7 8 9
|
#include <iostream>
#include <Windows.h>
#include <String>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include<fstream>
#include <time.h>
#include <wincrypt.h>
|
It tends to look like you added header file as you thought of them without knowing what you need or what they do.
This is what I came up with that might help:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <fstream>
#include <iostream>
#include <limits>
#include <string> // <--- "String" may be working because here it may not be case sensitive here.
#include <clocale> // <--- Or did you mean the C++ header file "<locale>"? Not sure what you need it for?
#include <cstdlib> // <--- For "rand()" and "srand()".
#include <ctime> // <--- For "time()"
//#include <stdio.h> // <--- Not a standard C++ header file. Should use "cstdio". None of these may be needed.
//#include <stdlib.h> // <--- Should use "cstdlib".
//#include <time.h> // <--- Should use "ctime".
#include <Windows.h>
#undef min // <--- Need to undefine these so "limits"can redefine them in its way.
#undef max
//#include <wincrypt.h> // <--- Should be included thru "Windows.h". At least it is in MSVS 2017.
|
Line 5 is not necessary. I used it to distinguish between the C++ header files and the C header files. The header files that start with "c" are the standard C++ header files that should be used over the ".h" C header files. And for some files like "Windows.h" there is no C++ version.
I would comment about
using namespace std;
, but you are not ready for that part yet and right now you think it is the greatest thing there is.
You should avoid defining all your variables as global variables. That will just lead to problems in the future. The exception would be variables that start with "const" or "constexpr". These are constant variables that can not be changed by the program. And some of these variables should be constant.
As an example:
1 2 3 4 5 6 7 8 9
|
// Constant variables that do not change.
constexpr double version{ 0.1 };
const string goToJail{ "You were caught and sent to jail." };
const string Employeee{ "Employee" }; // <--- Watch your spelling. As a a constant it would be better as "EMPLOYEE" just 2 Es.
const string Hackerr{ "Hacker" }; // <--- Just 1 "r"
const string Criminall{ "Criminal" }; // <--- Just 1 "l"
constexpr int employee{ 10 };
constexpr int hacker{ 20 };
constexpr int criminal{ 30 };
|
I did not change these, but constant variable tend to be in all capital letters. Not required, but common.
Also if you are compiling to the C++ 2011 standards or later you should use the uniform initializer, the {}s empty the compiler will use the proper type of (0) zero needed. "std::strings", "std::vectors" and other container classes are empty when defined and do not need initialized unless you want to put something in them.
In the end most of these global variables should be in "main".
Just a note:
constexpr double version{ 0.1 }
. The "0.1" makes it look like you are not yet to the 1st version. As a "std::string" this variable could be written as "1.0.0" Where 1 represents the version and the other 2 numbers represent the type of changes made, i.e., major, but not enough to be a full version change, and minor changes or fixes.
Stating in "main"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
int main()
{
srand(static_cast<unsigned int>(time(nullptr))); // <--- Has no effect on the global variables.
// Global variables that should move to here.
string
command{ "Not Valid" }, // to choose what to do each time
name, password, // the ones that are found in the file
inName, inPassword, // the ones you are going to input from keyboard
registerName, registerPassword; //also what you're going to input
//and if you know C strings, just replace that with something like
/*
char
command[9],
name[31], password[31], //it could be any size, but like this you have got 30 characters at your
//disposal, if you consider it to be enough
inName[31], inPassword[31],
registerName[31], registerPassword[31];
*/
while (1)
{
int menuChoice{};
cout <<
/*"\n"
" (register/exit/login)\n"
" Command: "*/
"\n"
" 1. Register\n"
" 2. Login\n"
" 3. Exit\n"
" Enter Choice: ";
std::cin >> menuChoice;
if (!std::cin)
{
std::cerr << "\n Invalie Entry! Must be a number.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
continue;
}
//getline(cin, command);
switch (menuChoice)
{
case 1:
command = "register";
break;
case 2:
command = "login";
break;
case 3:
command = "exit";
break;
default:
std::cerr << "\n Invalid Choice!. Try again.\n";
break;
}
|
Line 5 seeds the PRNG before "rand()" is used. Otherwise you will always get the same numbers with a call to "rand()". This may or may not be what you want.
Every time I run the program I get these values for:
double iSecretMoneyAWeek = ceil(rand() * 4); //this is how much money they make a week 164.00
double iSecretSickly = ceil(rand() * 4); // if the user inputs this number he will become sick 73,868.00
double iSecretFired = ceil(rand() * 4); // if the user inputs this number he will be jailed 25,336.00. Also Comment does not match the variable.
double iSecretJailed = ceil(rand() * 4); // if the user inputs this number he will be jailed 106,000.00
|
This is because "srand" does not have a proper seed and used its default. Something to consider is if these numbers are what you need.
The comments about C code do not have much use in a C++ program. Either write a C program or a C++ program. Do not try to do both. The commented lines are ignored at compile time, but cause a reader to pause and wonder what you want to do.
For the menu what you started with may work, but does have its problems. What if someone enters "Exit"? You have no way of using this or an else statement to say that it is wrong. You just present the menu again so a user can make the same mistake. Setting up the if statements to check all possible combinations would create a big if statement.
The way I changed the menu around is 1 possibility. There are other shorter ways, but you may not be ready for them yet.
If you are not ready for the switch yet you can use if/else statements.
Choosing a number is better than someone not spelling a word correctly and trying to account for that.
Something to get started on while I am out for a little while.
Andy