Hey guys, I just signed up, and I have a question. I am trying to develop a text-based game using Dev C++, and I am trying to get out of the "enter 1 to go east, enter 2 to go north, etc." approach, and am trying to move onto a system in which the user types in what they cant to do. But I need to develop a snippet of code that capitalizes the first letter in a string and lowercases all the others. This is what I have:

// Capitalize the first letter in the string...
  szword[0] = toupper( szword[0] );
  
  // ...and lowercase all the others
  while (nCaps < 1000)
  {
    szword[nCaps] = tolower( szword[nCaps] ); 
    nCaps = nCaps + 1;
  }

Unfortunately, this system ignores spaces, rendering it useless if a user were to type something like "go east". However, typing "go_east" works. So, I see three solutions (in order of which would be best for me):

One, a version of the above code that ignores the spaces
Two, a snippet of code that turns all spaces into underscores
Three, a section of code that removes all spaces completely

Sorry if there's already been a topic like this, but I've searched to no avail. Thanks in advance!

The problem with the spaces is not in the code you posted but the way you are getting keyboard input. You need to post that part of the program.

The code you posted has a major flaw though. You don't want to check the entire size of the buffer szword but only the number of characters the player typed. In C that would be strlen(szword).

int len = strlen(szword);
for(ncaps = 1; ncaps < len; ncaps++)
    szword[ncaps] = tolower(szword[ncaps]);

I am not entirely sure what you mean by how my keyboard recieves data. I believe you're talking about this:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

If that's not what you're talking about, tell me how to find it.

>>Unfortunately, this system ignores spaces, rendering it useless if a user were to type something like "go east". However, typing "go_east" works.

What I mean is that cin >> szword only accepts keystrokes up to the first space or the '\n' character. If you want everything up to the '\n' character then you have to use getline(), like this: getline(cin, szword); , assiming szword is std::string. If szword is a character array, then use this: cin.getline(szword, sizeof(szword)); And you don't need either cstdio or cstdlib because you are writing a c++ program, not a c program.

It worked, thanks!

Now, what did you mean in your first post?

> The code you posted has a major flaw though. You don't want to check the entire size of the buffer szword but only the number of characters the player typed. In C that would be strlen(szword).

oh, and yes, I am working with an std::string.

It worked, thanks!

Now, what did you mean in your first post?

> The code you posted has a major flaw though. You don't want to check the entire size of the buffer szword but only the number of characters the player typed. In C that would be strlen(szword).

oh, and yes, I am working with an std::string.

He means if you entered 3 characters, say NOR, for NORTH, the comparison won't work. You need to compare only the first 3 characters of your internal string.

And why capitalize the first letter of the sentence? Just force everything to lower case.

Example of how to convert a string to (upper/lower/title)-case.

#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>

class lowercase {
  public:
    int operator () ( int c ) { return tolower( c ); }
  };

std::string toTitle( std::string s ) {
  std::string result( s );
  if (!s.empty()) {
    std::transform(
      s.begin()+1,
      s.end(),
      result.begin()+1,
      lowercase()
      );
    result[ 0 ] = toupper( s[ 0 ] );
    }
  return result;
  }

int main() {
  using namespace std;
  string s;
  cout << "Please enter a string> ";
  getline( cin, s );

  cout << toTitle( s ) << endl;

  return EXIT_SUCCESS;
  }

Enjoy!

class lowercase {
  public:
    int operator () ( int c ) { return tolower( c ); }
  };
std::transform(
  s.begin()+1,
  s.end(),
  result.begin()+1,
  lowercase()
  );

*gives Duoas a cookie*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.