How do I call an item in an array using fragments of the value?

So I am (obviously) new to programming, and trying to figure out how to make this (what should be) simple code work. My issue is in my last line, where I'm trying to call an item in my array, by using an int value to target it. The goal for this code is to display sword1 and sword2 stats, give the user a choice to pick either 1, or 2, and then use their input to situate the choice. ( Sword + equipped value + .name = sword1.name or "Strata")

This is my first code, and if anyone can help guide me in this, or a better method to do this... I would be grateful. I was trying to search up for solutions, but I don't even know the way to write it in a search, that would warrant usable results.
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
#include <iostream>
#include <string>


using namespace std;
int main()
{
struct weapon
{
        string name;
        int skill;
        int strength;
};

weapon sword1 = { "Strata", 0, 30 };
weapon sword2 = { "Terra", 15, 15 };
int equippedw = 1;


cout << " Weapon number 1 " << sword1.name << " skill: " << sword1.skill << " strength " << sword1.strength << endl;
   
cout << " Weapon number 2 " << sword2.name << " skill: " << sword2.skill << " strength " << sword2.strength << endl;

cout << " Which weapon number will you use? " <<  endl;
    //assume input was 1//
    
cout << " Weapon chosen: " << sword. + equippedw + .name << endl;
}
This would be an array:
1
2
3
    weapon sword[2] = { 
        { "Strata", 0, 30 },
        { "Terra", 15, 15 } };


Array has two elements, valid subscripts are in range 0 to 1.

You would access it like this:
1
2
3
4
5
6
7
    cout << " Weapon number 1 " << sword[0].name 
         << " skill: " << sword[0].skill 
         << " strength " << sword[0].strength << endl;
    
    cout << " Weapon number 2 " << sword[1].name 
         << " skill: " << sword[1].skill 
         << " strength " << sword[1].strength << endl;


//assume input was 1//
 
cout << " Weapon chosen: " << sword[equippedw -1].name << endl;


Last edited on
Topic archived. No new replies allowed.