• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Poigahn

Picking A letter From a Char String

16 posts in this topic

I am able to do this in 3 other Computer Languages, But I can not find the command to do it in C++.

Here it is :

 

char Alphabet[] = "ABCDEFGHIJK";

 

How do I get the 5th letter out of this string ?  Keep in mind that at any given time the 5th letter may change ( I am not looking for the 'E")

Just the 5th  or sometimes maybe the 8th letter.

 

The purpose is to add that letter to another string such as

 

strcat(Letter,<The 5th letter in the Alphabet[list]>);

 

Plus any include files that may not be obvious.

0

Share this post


Link to post
Share on other sites

Alphabet[4]

 

? (It's just an array! Because it is a string literal it is null terminated of course).

 

Or are you looking for the 5th unique letter from the string instead, that is more complicated (i.e. "AAAAABBBCCDDDEEEE" would want 'E' returned).

 

EDIT: Note you can't use a char as an argument to strcat though, you would need to make a null terminated char array, or do it by hand.
 

EDIT2: It's easy to make a null terminated char array out of it though:

 

char buff[2];

 

buff[0] = Alphabet[4];

buff[1] = '\0';

 

// strcat away using buff

Edited by Paradigm Shifter
2

Share this post


Link to post
Share on other sites

Yeah, probably, depends what you're doing. (EDIT: e.g. having a char array alphabet[26] that doesn't change probably isn't shocking to a C++ programmer).

 

Using strcat is a sign you're not doing the correct thing though - a std::string knows it's length without having to scan through looking for a null terminator.

 

EDIT2:

 

std::string Alphabet = "ABCDEFGHIJK";

 

char fifthCharacter = Alphabet[4]; // fifthCharacter == 'E'

 

is still true though.

 

EDIT3:

 

std::string fifthCharacterAsString = Alphabet.substr(4, 1);

 

is probably what you want though.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

Paradigm  -  Your response was spot on.  Believe it or not, however, once I posted my question the way I did, the light bulb went on!!  I went back to what I was doing, Looked at it, then wrote a small program , realizing it was an array of characters, and then added the added that character to my string as follows:

 

string =  What;

What = "Option"

What = What+ Alphabet[4];

What Now equals "OptionE"

 

That did solve my problem.  Prior to that I was using doing this

 

What = ("Option"+Alphabet[x]);

 

and getting a Type MisMatch type compile error.  So my problem was solved.

 

Apoch, After reading yours I am still scratching my head. 

0

Share this post


Link to post
Share on other sites


That did solve my problem.  Prior to that I was using doing this
 
What = ("Option"+Alphabet[x]);
 
and getting a Type MisMatch type compile error.  So my problem was solved.

 

What is 'What'?

0

Share this post


Link to post
Share on other sites

What is a Variable Name to hold WhatEver I assign to it.

 

I apologize for not being clear.

 

Which type of variable is What?

0

Share this post


Link to post
Share on other sites


Which type of variable is What?

 

An std::string, with using namespace std; tacked on somewhere before.

0

Share this post


Link to post
Share on other sites

 


Which type of variable is What?

 

An std::string, with using namespace std; tacked on somewhere before.

 

 

Thanks.

 

Your line of code is interesting to me because it doesn't look like there should be a compile error, just an annoying lack of compile error and some unexpected value in 'What'?

std::string What = ("Option"+Alphabet[x]);

"Option" is a string literal, which should behave a const char array.

Alphabet[x] should be a char.

 

Now, a char is just a 1-byte integer, and a char array also acts like a char pointer, so I'd expect pointer arithmetic out of this.

 

To confirm, I added this to a piece of code I had open:

        std::string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int x = 4;
        std::string What = ("Option"+Alphabet[x]);

There were no compile errors.

 

My expectation is that, because 'E' is some number beyond the string length of "Option", the assignment for string 'What' ends up filling itself from whatever happens to be in that memory location beyond "Option", until it hits a byte equal to 0.

 

Oh well, it feels like I'm probably just wasting your time. It's good you have everything working now.

0

Share this post


Link to post
Share on other sites

String literals do not respond to addition in the way you're expecting. That will take the address of the string literal, interpret it as a char pointer, then add the value of the char you're getting from Alphabet[x], which will be in the 0x41-0x5A range. (Edit - Nevermind, it looks like you realized that.)

 

If you want to append to a std::string then you can use the append() method, though it doesn't support single char as input. You need to pass it a std::string or a C-style string, so we've more or less come full circle here.

 

You could do this:

#include <string>
#include <iostream>

int main() {
  using namespace std;
  string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string str = "Option";
  int x = 4;
  char temp[2] = {Alphabet[x], 0}; //kludge together a C string from two char values
  str.append(temp);
  cout << str << endl;

  system("pause");
  return 0;
}

Edited by Khatharr
0

Share this post


Link to post
Share on other sites


If you want to append to a std::string then you can use the append() method, though it doesn't support single char as input. You need to pass it a std::string or a C-style string, so we've more or less come full circle here.

 

You can append a single char N times.

 

std::string What = "Option";
What.append(1, Alphabet[x]);
1

Share this post


Link to post
Share on other sites

In Visual C++ you may declare:

CString Alphabet = "ABCDEFGHIJK";

 

the 5th letter out of this string:

CString c5 = Alphabet.Mid(4,1);

-2

Share this post


Link to post
Share on other sites

In Visual C++ you may declare:
CString [background=#fafbfc]Alphabet = "ABCDEFGHIJK";[/size][/background]
 
[background=#fafbfc]the 5th letter out of this string:[/size][/background]
CString c5 = [background=#fafbfc]Alphabet.Mid(4,1);[/size][/background]

That is plain horrible advice. There is C++ standard library class which does the same job (only apparently better if Mid(4, 1) is indeed the way to retrieve the character with index 4). CString is useless if you have the intention of writing standard compliant code and perhaps port the program to a different platform. Even when using MSVC only you are pulling in a rather large dependency you really do not need under normal circumstances. Edited by BitMaster
1

Share this post


Link to post
Share on other sites

 

In Visual C++ you may declare:
CString Alphabet = "ABCDEFGHIJK";[/size]
 
the 5th letter out of this string:[/size]
CString c5 = Alphabet.Mid(4,1);[/size]

That is plain horrible advice. There is C++ standard library class which does the same job (only apparently better if Mid(4, 1) is indeed the way to retrieve the character with index 4). CString is useless if you have the intention of writing standard compliant code and perhaps port the program to a different platform. Even when using MSVC only you are pulling in a rather large dependency you really do not need under normal circumstances.

 

 

Well I already mentioned the C++ equivalent which is Alphabet.substr(4,1);

 

I agree don't pull in ATL or worse MFC if you can use the standard library for the job.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0