Class cin error.

I was wondering if there was any way to cin the class function parameter in main to set the private variable. I know I could make the variable in my class a public and then cin that but is there any other way via a function. Oh and I know that I could make an another variable and then make the function set itself it to that. However is there any other to directly cin that parameter within the class?

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
#include <iostream>

using namespace std;
struct enemies {
  int Bandit;
};
class Enemies {
private:
int bandito;
public:
void setBandito(int bandit) {
    bandito = bandit;
}
float getBandito() {
    return bandito;
}
    
};
void enemyCheck(enemies senemies, Enemies stuff);

int main() {
    char response;
    enemies sFrank;
    Enemies bob;
    cin >> sFrank.Bandit;
    cin >> bob.setBandito(bandit);
    enemyCheck(sFrank, bob);
    cin >> response;
    return 0;
    
}
void enemyCheck(enemies senemies, Enemies stuff) {
    cout << senemies.Bandit;
    cout << stuff.getBandito();
}  

EDIT: Oh yeah this is just test messing with classes and structs and stuff.
Last edited on
closed account (E0p9LyTq)
You could overload std::istream's operator>> to deal with using cin to get the data needed for your struct and class data members. Same as you would overload std::ostreams' operator<< for cout.

Now if that confuses you a bit of studying overloading functions and the stream operators might benefit you.
Topic archived. No new replies allowed.