unable to cout string in C++ -
i new programming , can use c++. have been working on rpg, , reason can't program print out value set string to. started defining string "weapon" in void main.
void main() { string weapon; cin >> weapon; if(weapon = "a") { weapon == "sword"; } }
i had code sort of , had function above uses "weapon" (which set sword can see code above) @ end of had print out, in function (which above void main) in order both defined variables had define them in both void main , function, when that, nothing appears in program when it's run. had written correctly (what put above example) way doesn't create error defining in both parts of code. says 1 of them hasn't been defined yet defined both function , void main. why isn't working? how fix it? thanks
p.s. did include string library , namespace.
at least read it, have code vaguely this:
void f() { string weapon; cout << weapon; } int main(){ string weapon = "sword"; f(); }
...and problem value you're assigning weapon
in main
isn't being used used when call f
.
assuming that's correct, you're seeing normal, expected behavior. weapon
you've defined in main
separate variable weapon
define in f
. assigning value 1 has no effect on other.
to desired effect, need "pass" value 1 other parameter:
void f(string w) { cout << w; } int main() { string weapon = "sword"; f(weapon); }
this way, calling f
gives copy of current value weapon
in main
has been assigned, f
can use same value.
Comments
Post a Comment