cout not working through double function call

I am making a command-line game (if that makes any sense). I currently have it to where if you make the move "DEBUG", it will call an enemy attack. However, the attack announcement isn't showing up.

After picking a difficulty from 1-5, the getPlayerChoice() function is correctly called. However, selecting the DEBUG option isn't running the attack() function properly. Is there something wrong with converting the input to uppercase (calling the uppercase() function on the string)? Is there something wrong with the attack() function itself?

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <locale>

using namespace std;

string uppercase(string Ustr)
{
    locale loc;
    string newstr;
    for (string::size_type i=0; i<Ustr.length(); ++i)
    newstr = toupper(Ustr[i],loc);
    return newstr;
}

int IRand(int ir_min,int ir_max)
    {
    int ir_res = ir_min+(rand() % (ir_max-ir_min));
    return ir_res;
    }

void attack(string bot,string wpn,int damage,int rate,int accuracy,string wpn2 = "",int rate2 = 0)
{
    cout << bot << " attacks with " << wpn << "!\n";
    //TODO factor in difficulty
    int acc = 8; //TODO add a real formula that makes sense

}

void getPlayerChoice()
{
    cout << "What would you like to do?\n";
    string act;
    string action;
    cin >> act;
    action = uppercase(act);
    if (action == "DEBUG")
    {
        attack("TestBot","Laser Lvl. 1",4,4,5);
    }
}

int main()
{
srand(time(0));
int diff = 0;
while (diff < 1 || diff > 5)
{
    cout << "Please select difficulty, from 1 (Trainee) to 5 (Insane):\n";
    cin >> diff;
    if (diff < 1 || diff > 5)
    cout << "Invalid difficulty! ";
}
getPlayerChoice();
}
Last edited on
The problem is in your uppercase function. When I enter debug the return value is only "G"
Thanks! I got rid of the uppercase thing entirely and switched to an exclusively lowercase system (only lowercase inputs accepted) and it works now.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <locale>

using namespace std;

int IRand(int ir_min,int ir_max)
{
    {
     int ir_res = ir_min+(rand() % (ir_max-ir_min));
     return ir_res;
    }
}
void attack(string bot,string wpn,int damage,int rate,int accuracy,string wpn2 = "",int rate2 = 0)
{
cout << bot << " attacks with " << wpn << "!\n";
//TODO factor in difficulty
int acc = 8; //TODO add a real formula that makes sense

}

void getPlayerChoice()
{
    cout << "What would you like to do?\n";
    string act;
    cin >> act;
    if (act == "debug")
    {
        attack("TestBot","Laser Lvl. 1",4,4,5);
    }
}

int main()
{
srand(time(0));
int diff = 0;
while (diff < 1 || diff > 5)
{
    cout << "Please select difficulty, from 1 (Trainee) to 5 (Insane):\n";
    cin >> diff;
    if (diff < 1 || diff > 5)
        cout << "Invalid difficulty! ";
}
getPlayerChoice();
}


I now want to know if there is a way to do what I was trying to do. I found a method earlier, but it appears to only work when printing - basically that "G" thing you found for each letter.
Last edited on
This should do:
1
2
3
4
5
6
7
8
string uppercase(string Ustr)
{
  locale loc;
  string newstr;
  for (string::size_type i = 0; i<Ustr.length(); ++i)
    newstr += toupper(Ustr[i], loc);
  return newstr;
}
Thanks!
Topic archived. No new replies allowed.