D&D player and Int help

Pages: 12

Hi Im new to C++. To help me learn I made a program that shows me my charters states and iteams and what not. Took me a long time but I feel like I got loops down after doing it. The thing is I wanted to keep going with the app by making a combat section. I already made a if loop with a cin asking if you took damage or healed but now what I would like to do is add a function where you can add your party members and NPCs that you are fighting allong with the turn order. There is more stuff to go with it but I think if someone could just give me a hint not so much the answer I could learn how to do it from there. It's more about me learning than making the app because this is just for me. If anyone would like it after I'm done ill send it to you but I need hint to get me to learn this part first haha. I don't have any coding friends (because people are lazy) and I don't even know how to word this to look for it online. I tried to do a char [20 , 20 , 20] hoping it would make 3 differnt stings if I did a string p1 = ""; but that didn't work xp
This became a how to make caps not matter when really the above about how to do the thing is more imporant
Last edited on
If lookup is a char variable, not a string, either std::toupper or std::tolower (found in <cctype>) would work:

if (std::toupper(lookup) == 'X') { std::cout << "do something"; }
or
if (std::tolower(lookup) == 'x') { std::cout << "do something"; }
If lookup is a std::string, then you can access the first element in the string and convert with std::toupper or std::tolower:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <cctype>

int main()
{
   std::string yeah = "yes";
   std::string nay  = "NO";

   if (std::toupper(yeah[0]) == 'Y')   { std::cout << "YES!\n"; }

   if (std::tolower(nay.at(0)) == 'n') { std::cout << "no.\n"; }
}
YES!
no.
using only 1 letter works but as soon as I try 2 or more it breaks I was only use X as a exp because its was faster to type. I didn' t think that would matter that much. All of lookup will be 3 charters
I was trying to look up the reason for that but decided to post this before just to clear it up
If lookup is a C string (char array):

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cctype>

int main()
{
   // C strings have to have a size greater than the total number of characters to be stored
   // to terminate the string with the null character ('\0')
   char yeah[4] = "yes";

   if (std::toupper(yeah[0]) == 'Y')   { std::cout << "YES!\n"; }
}
I don't think you understand or I don't understand what your trying to tell me. Because the input from the user can't just be Y or N it will be something like "EXP" then enter this is come code all ending with cin >> lookup; to get to the next loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
string lookup = " ";
int = 5;
do{
if(lookup == "IFC" || list "== "ifc")
{
cout << "D&D talk";
cin >> lookup;
}

if(lokup == "IBK" || list "== "ibk")
{
cout << "D&D talk";
cin >> lookup;
}
}
while(x > 0)
}


It keeps going on like that witch is why I can't use one letter for input. I am not looking to make the output all caps just make the caps of the input not matter so I can type IbK and still get a result
Last edited on
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>
#include <cctype>
#include <string>

void toUpper(std::string&);
void toLower(std::string&);

int main()
{
   std::string str = "A sTrInG fUll oF ChArAcTeRs";

   toUpper(str);

   std::cout << str << '\n';

   toLower(str);

   std::cout << str << '\n';
}

void toUpper(std::string& str)
{
   for (unsigned i = 0; i < str.size(); i++)
   {
      str[i] = std::toupper(str[i]);
   }
}

void toLower(std::string& str)
{
   for (unsigned i = 0; i < str.size(); i++)
   {
      str[i] = std::tolower(str[i]);
   }
}
A STRING FULL OF CHARACTERS
a string full of characters


Good luck, I'll let others try to help now.
Using your code and putting it into mine does not work as shown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cctype>
#include <string>

void toUpper(std::string&);
void toLower(std::string&);
int main()
{
  string lookup = "";
  toUpper(lookup);
cout << "lookup test type htl";
cin >> lookup;
if(lookup == "HTL")
{
  cout << "it worked";
}
else{cout<< "fail";}


return 0;
}

this is copied and pasted IT DOES NOT WORK DUDE
Last edited on
It does work, when you copy and paste ALL of the relevant code, not just the function declarations:
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
#include <iostream>
#include <cctype>
#include <string>

void toUpper(std::string&);
void toLower(std::string&);

int main()
{
   std::string lookup = "";
   std::cout << "lookup test type htl";
   std::cin >> lookup;

   toUpper(lookup);

   if (lookup == "HTL")
   {
      std::cout << "it worked\n";
   }
   else { std::cout << "fail\n"; }
}

void toUpper(std::string& str)
{
   for (unsigned i = 0; i < str.size(); i++)
   {
      str[i] = std::toupper(str[i]);
   }
}

void toLower(std::string& str)
{
   for (unsigned i = 0; i < str.size(); i++)
   {
      str[i] = std::tolower(str[i]);
   }
}
lookup test type htlhtl
it worked

But continue to complain if you want, I'm done.
Last edited on
you have to remember something dude I JUST learned how to do all this last week. I dont even understand your for loop you didn't explain any of the things you just gave me what looks like random code. or why you put that void stuff under the finshed code if you would explain to someone who said they where NEW this wouldn't have gone on for as long. AND ON TOP OF THAT THATS NOT EVEN THE QUESTIon
Last edited on
you should use standard string. that makes it easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    string y = "yes";
    string s;
    cin >> s;
    std::transform(s.begin(), s.end(),s.begin(), ::tolower); //this converts all of the letters to lower case for easy compare
    if (y == s) //easy to compare.  
     cout << "Matched";
}


that will exactly match the input against your list of known words.
Last edited on
Ask questions when confronted by code you don't understand instead of complaining "it doesn't work!"

Unless you tell us we don't know what you do or don't know. We are not mind readers, we don't have a crystal ball.

Copy and pasting blindly code you don't understand can be quite frustrating, as you know.

Don't understand the for loop?

Do you understand what the std::string size() function does? It returns the number of characters contained in the string.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cctype>
#include <string>

int main()
{
   std::string str1 = "One";
   std::string str47 = "Forty-seven";

   std::cout << str1 <<  ", " << str1.size() << '\n'
             << str47 << ", " << str47.size() << '\n';
}
One, 3
Forty-seven, 11


Do you understand arrays? How for loops can be used to access each element? C++ strings are (VERY simplistic explanation) C char array strings, plus a lot of functions to make working with std::string easier.

The for loops walk through the string, from 0 to the string's size(), using std::toupper or std::tolower to change each individual character in the string.

I could have written more compact for loops for the string conversions that really would have been confusing to someone new to C++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void toUpper(std::string& str)
{
   for (auto& itr : str)
   {
      itr = std::toupper(itr);
   }
}

void toLower(std::string& str)
{
   for (auto& itr : str )
   {
      itr = std::tolower(itr);
   }
}

dude I said AND I WENT BACK TO REREAD IT "eaither I dont undestand or you dont understand what I am trying to do" you made yourself mad I was being nice up to the point you said "Im done" you made yourself mad by not reading everything
Please recall that you are the one asking for someone's free time on a professional forum. Acting entitled isn't going to result in better or faster help. In general, you'll just embarrass yourself, and then be ignored.
Last edited on
eyes of the beholder
Please read http://www.catb.org/~esr/faqs/smart-questions.html .
Despite some significant flaws, it is still the best advice I can find regarding getting good technical help.
I wasn't mad at all, simply getting mildly annoyed trying to help someone who wanted to complain about the help being FREELY offered.

No one here is paid to be your private tutor.
a professional forum
There are even rumours that it is technical oriented.
Pages: 12