-3

I'm making a game where the user has to stop people from hacking into their company's database/security.

Currently, I have a variable int threats; that I can assign a value to. This doesn't work because I need to have multiple threats that have their own type, like DDos or Malware, and they also need names, like $DDoSThreat%.

This has been killing me for weeks, all help would be appreciated.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Soccs
  • 1
  • A variable has one value at a time, but you can subdivide that value, bit masking for example, or have an array of values. – user4581301 Jun 02 '20 at 03:50
  • Maybe you need a container like std::vector or std::map – Omid Jun 02 '20 at 04:03
  • Has it really been taking you WEEKS just to define a few variables? Sounds like you need to get yourself a [good C++ book](https://stackoverflow.com/questions/388242/). – Remy Lebeau Jun 02 '20 at 04:29

1 Answers1

1

You could've used std::vector<> for storing multiple struct types using a single variable.

Look at the following:

#include <iostream>
#include <vector>

struct Malware {
    std::string name;
    bool danger;
};

void getMalwareDetails(Malware); // parameter: struct

int main(void) {
    std::vector<Malware> malware; // HERE'S WHAT I'M TALKING ABOUT
    Malware mTemp; // for temporary
    char ask;

    do {
        std::cout << "Malware name and is that danger (1/0)? : ";
        std::cin >> mTemp.name >> mTemp.danger;

        malware.push_back(mTemp); // adds the info to struct

        std::cout << "Add more? (Y/n): ";
        std::cin >> ask;

    } while (ask == 'Y' || ask == 'y');

    for (int i = 0; i < malware.size(); i++)
        getMalwareDetails(malware[i]); // get the struct info

    return 0;
}

void getMalwareDetails(Malware m) {
    std::cout << "Name: " << m.name << " Danger?: " << m.danger << std::endl;
}

Create a struct, use std::vector<STRUCT_NAME>, add the struct values to the vector variable on each iteration then finally display them. Similarly, you may use one more struct for DDos.

Sample Output

Malware name and is that danger? : $DDoSThreat% 1 // --- INPUT
Add more? (Y/n): y
Malware name and is that danger? : Trojan 1
Add more? (Y/n): y
Malware name and is that danger? : Google 0
Add more? (Y/n): n
Name: $DDoSThreat% Danger?: 1 // --- OUTPUT
Name: Trojan Danger?: 1
Name: Google Danger?: 0
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34