error c2678 and c2065

Pages: 123
@closed account 5a8Ym39o6
Yes it did thank you.
Good to hear :)
@Jao

But you are better not to use a string for this purpose. It's not worth trying to cope with all the variations of case a user might potentially input. So just stick with a single char and convert it to upper or lower case then test that. There is an example here.

http://en.cppreference.com/w/cpp/locale/ctype/toupper

1
2
3
4
5
// convert to upper case
// then
if (direction == L'N') {
    // ...
}


Edit:

Another idea is to use enums:

http://en.cppreference.com/w/cpp/language/enum

Preferably a scoped enum. With enums in general, they allow one to refer to the full word in the code, as in:

case Direction::North
Last edited on
@TheIdeasMan
So you are the one reporting my posts :-X
It could be more than one person. It's certainly something which is not surprising.
@TheIdeasMan
Ok I will look into enums and I will try to figure it out. I thank you for your help and if i need any more assistance I will let you know
closed account (E0p9LyTq)
@Jao,

You asked what I would do for changing over from a wide character to a std::string:

Add the <string> header.

Add the <locale> header so I can use a C++ function template, tolower().
http://www.cplusplus.com/reference/locale/tolower/

"Massage" the current code, and come up with:
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
#include <iostream>
#include <string>
#include <locale>  // needed for std::tolower()

int main()
{
   std::locale loc;  // needed for tolower()

   std::cout << "Welcome to a world of imagination. You are in Area 1.\n"
             << "(Go North to Area 2.) : ";
   
   std::string direction;
   std::getline(std::cin, direction); // why getline?  So you get ALL the characters entered.
   std::cout << "\n";

   // take the first element in the string, direction[0]
   // use tolower() to convert the character to lower case
   // compare if that equals a single-byte character
   if (std::tolower(direction[0], loc) == 'n')
   {
      std::cout << "Good, you made it to Area 2.\n";
   }
   else
   {
      std::cout << "No, I said, \"Go North!\"\n";
   }
}


Welcome to a world of imagination. You are in Area 1.
(Go North to Area 2.) : w

No, I said, "Go North!"


Welcome to a world of imagination. You are in Area 1.
(Go North to Area 2.) : N

Good, you made it to Area 2.
@FurryGuy
Thank you for the time you put into making the code (even though for you it probably took like 2 minutes lol). As stated before im freshly learning and everything everyone is posting is really helpful. Thank you all.

Edited:
Currently I am reading through the Tutorials (http://www.cplusplus.com/doc/) which so far i find better than the C++ for dummies (6ed) I have been using and my next step is to create a loop for when the user does not input the correct direction (without the infinite loop dilemma) so the user can attempt to reenter the correct input.

(Hopefully I sounded a little smart there lol)
Last edited on
Cheers, all the best :)
@FurryGuy
The function tolower() is in #include <cctype> , not in the header #include <locale> .

Can you explain?
closed account (E0p9LyTq)
@Jao,

Reworking the code too longer than two minutes, really. :)

Any time I used was worth it because it helped you to learn some new C++ concepts that will help you in the future if you decide to pursue a programming career.

You did most of the work by posting your code, so congratulate yourself. :)

TheIdeasMan had good suggestions, using enums. I used another method (std::string/tolower) to arrive at the same place, code that works the way you want.
Last edited on
So I think it is not really advisable to use the standard function tolower() that is normally supposed to be defined in the header file #include <cctype> ? Right?
closed account (E0p9LyTq)
The function tolower() is in #include <cctype> , not in the header #include <locale>


BUZZ! Wrong!

My usage of std::tolower() IS in <locale>, if you would ever bother to look:
http://www.cplusplus.com/reference/locale/tolower/

A less robust version is in the C Library <cctype>, but I chose to use the C++ version.

<locale> std::tolower works with templated char types. <cctype> tolower only works with int.

<locale> std::tolower overloads the C <cctype> function. Notice "overloads." So the <locale> function template can use more than a single type of input.
@FurryGuy
> So the <locale> function template can use more than a single type of input.
What do you mean by "more than a single input"? Since it is a template function, can it do the following :

1
2
3
4
5
char *c_s = "abc";
std::string s = "def";

std::string s1 = std::tolower(c_s);
std::string s2 = std::tolower(s);
closed account (E0p9LyTq)
my next step is to create a loop for when the user does not input the correct direction (without the infinite loop dilemma) so the user can attempt to reenter the correct input.


Mash some code, then. While studying.

And when you have problems and errors, not if, post the code so others can help you. :)

There are books, good books far better than dummy books, that would be a help for learning C++. Here's one list:
https://isocpp.org/wiki/faq/how-to-learn-cpp#buy-several-books

CPlusPlus has a good tutorial section: http://www.cplusplus.com/doc/

Another C++ tutorial site: http://www.learncpp.com/
@FurryGuy
I sent you a pm. Just FYI
@Jao
I sent you a PM. Just FYI
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main ()
{
    char  str[] = "Test String";
    for (int i = 0; i < sizeof(str); ++i)
        std::cout << (char)std::tolower(str[i]);
    std::cout << '\n';

    for (int i = 0; i < sizeof(str); ++i)
        std::cout << (char)std::toupper(str[i]);
    
    return 0;
}

test string
TEST STRING
Last edited on
@kemort
std::tolower() without the header <locale>??
closed account (48T7M4Gy)
Yes without anything other than what I have shown. I chose char[] to avoid hidden linkage via <string>. I noticed it the other day on my machine, saw the comments here, et voila you can try it yourself.
Last edited on
Pages: 123