Won't do the right thing.

Please look through my code. Its to long to post here: https://ashtonfroude.wixsite.com/mysite-3/post/help-needed
Idk what can be the matter with it. Its not doing the if this number entered do this on lines 393, 404, 416, and 432. Just copy and plop it in your code editor.
Last edited on
the site you put it in has no line numbers and pasting it did not yield any if statements on those lines or near them for me.

be more specific where it is giving trouble.
You have a bunch of if's that use = when you mean == (even though you were already told about that).
Well this is a good way to make sure nothing happens for a long while.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (year < 2080) {
  Sleep(60000000);
  year = year + 1;
}
while (day <= 365) {
  Sleep(200000);
  day = day + 1;
}
while (minute <= 60) {
  Sleep(1000);
  minute = minute + 1;
}
if (day == 365 && minute == 60) {
  year = year + 1;
  day = day - 364;
  minute = minute - 59;
}


> Idk what can be the matter with it
The problem is in the chair, not the computer.

Specifically, at your stage, you should be writing no more than FIVE lines of code at a time without compiling and testing it to find out what you thought should happen is what actually happens.

Writing 100's of lines of code and having 0% clue as to WTF is happening isn't how you'll make progress.

Last edited on
Yikes! 60000000 milliseconds is 16.7 hours.
Yeah, and it does that 80 times!
It's the thick end of two months.

Still, could be worse.

He could have got the calculation right and actually be sleeping for 80 years.
ok ok. I get that i made a very large time mistake, but what can i do for the time counter? and FYI, i wasn't going for 1 year in milliseconds, i was only going for an hour but i got out of hand with the zeros... And i've only known coding C++ for like 9 days...
set that up as a constant and set it to like 10 seconds. You can't debug a program that sits there doing nothing for an hour ... just wait long enough to see that it did, indeed, wait and the keep going to test the logic. You can put in the real waits later if you really wanted that.
i didn't. Thanks!
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
Ok... but how do I make the work, store, and inventory function work? when i entered 3 to go to store incorrect name or password popped up.
If you're going to link code externally, please just use something simple like pastebin with syntax highlighting. I would ask you what line number you're talking about, but your fancy webpage doesn't have line numbers with the code.

Inside your inner (redundant) "if (inName == name && inPassword == password)" block, you have the line of code: "cout << "incorrect name or password\n"; "
Considering you already checked for username/password, that line of code must not belong there, so remove it.

You should be using an else statement in your code! I think I already mentioned this in another thread.
1
2
3
4
5
6
7
8
9
if (inName == name && inPassword == password)
{
    cout << "Authentication succeeded!\n";
    // ...
}
else
{
    cout << "Incorrect username or password\n";
}


Debug your code and step through it with a visual debugger to see what's happening at your own pace. Visual Studio, Code Blocks or similar IDEs all offer this capability.

I would suggest breaking your code up into different functions. Currently, you just have one big function and dozens of global variables.

(Also don't mark the thread with a green checkmark if there is still a problem)
Last edited on
Topic archived. No new replies allowed.