Win32 - Processing the backspace command? - c++

TL;DR I need to know which characters are deleted with Win32.
Basically, I need to know what characters are deleted, each time they are. Considering that there are three methods to delete, (Backspace, delete, and Right click>Delete) I'm not sure if I'll just be re-using the same code or what. There's also the option of multiple characters being selected, as well as the option for undo/redo. There's probably something else I'm missing.
As I said above, I need to know which characters are deleted, and how to use undo/redo and how to tell if when using those if characters were added or deleted. (If that's something easily Google, tell me, I just thought of them as I've been writting this post)

What exactly are you talking about? The win32 default control windows? You can subclass them to do that...
http://msdn.microsoft.com/en-us/library/bb773183%28v=vs.85%29.aspx
Well, if you come up with something specific code-wise and don't quite know what to do, just ask here and see if someone can't help you.
With regard to undo/redo, I believe it all depends on the size of the focus area, and I'm not sure how MS does it for, say, Word, or Excel, etc.
But for short stuff, what I posted above should work in a universal sense, but apparently not in your case.
Nevertheless, if anyone wanted to shorten the iteration in the above, they could replace the loop above by starting right at the end of shorter string, like this --
for(int i = iLen2, x=0; i < iLen1; i++, x++)
szAnswer[x]=str1[i];
This simply confines the interation to the missing characters.
Here is a small Win32 Console application that will demonstrate a very simple way to determine which characters have been deleted. You can easily adapt this to a Win32 App, add iterations to it, and so on
#include <iostream>
#include <string>
using namespace std;
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
int main()
{
int iLen1, iLen2, iResult;
char str1[]="The old red box.";
char str2[]="The old red";
char szAnswer[MAX_PATH]="";
// strcmp() will be greather than 0
// if str1 is longer than str2
if(strcmp(str1, str2) > 0)
{
iLen1=strlen(str1);
iLen2=strlen(str2);
iResult=iLen1-iLen2;
cout << "The number of missing characters is " << iResult << endl << endl;
// now lets see which characters are missing
// we iterate through the entire length
// of str1, which is the full text, but
// we only start puting characters into our
// result when we get to the end of str2
for(int i=0, x=0; i < iLen1; i++)
{
if(i >= iLen2)
{
szAnswer[x++]=str1[i];
}
}
cout << endl << "The missing characters are --" << endl << endl << szAnswer << endl << endl;
}
return 0;
}

Related

Console has input prompt then closes without output on a very simple program

I have a very simple program that won't give any console output.
I've tried getting input at the end using cin.get() and holding with system("pause"). I've also tried getting input at the start of the program then outputting at the end.
#include <iostream>
using namespace std;
int main(){
int bulb, bulbOpen=0, multiple;
for ( bulb=1; bulb<101 ; bulb=bulb+1 ){
for ( multiple=1; 100; multiple++){
if (bulb/multiple==0){
bulb = bulb * (-1);
}
}
if ( bulb<<0 ){
bulbOpen = bulbOpen + 1;
}
}
cout << "The remaining open light bulbs are " << bulbOpen << "." << endl;
return 0;
}
I'm a beginner programmer so any help, recommendations and explanations are very welcome.
EDIT:
Thanks to Rapha for the fixes and the advice, here's the updated code:
#include <iostream>
int main(){
int bulb, bulbCopy, bulbOpen=0, multiple;
for ( bulb=1; bulb<101 ; bulb++ ){
bulbCopy = bulb;
for ( multiple=1; multiple<101; multiple++){
if (bulbCopy%multiple==0){
bulbCopy = bulbCopy * (-1);
}
}
if ( bulbCopy<0 ){
bulbOpen = bulbOpen + 1;
}
}
std::cout << "The remaining open light bulbs are " << bulbOpen << "." << std::endl;
std::cin.get();
return 0;
}
The exercise went like this: You've got 100 light bulbs. You take every number from 1-100 and for every lightbulb with the position a multiple of said number, you switch it's current state. So basically if you've got bulb 2, you first switch it ON because it's a multiple of 1, then you switch it OFF because it's a multiple of 2.
And you've got to check how many remaining lightbulbs are still open by the end.
The answer is 10.
The Main-Problem why you get no output is, that the code is causing an infinity-loop (The loop cant escape and will run forever) and you never reach the std::cout part of the code
Ok there's a lot going on and the first thing is (You probably will hear this a lot on this platform) don't use using namespace std; instead use the std::-prefix for c++-Standard Things. I think its ok to use if you start out, but its a really bad Practice.
Then another thing is, cin.get() already 'pauses' or interrupting the program until you entered an input so system("pause") really isn't needed here.
To get input simply do it like that:
int input;
std::cin >> input;
std::cout << "My output was: " << input;
Then another thing is, i dont really know what you try to do with the nested for-loops but in the second for-loop you have a conditions that doesnt really make sense
for(multiple=1; 100; multiple++)
^^^
What you probably want is something like
for(multiple=1; multiple<100; multiple++)
And then saying bulb/multiple==0 doesn't really make sense either, because its only true if bulb is 0, maybe you mean bulb%multiple==0 (modulo).
And there's probably a typo in one condition where you wrote bulb<<0 where you probably want to write bulb<0
But no matter what you do, it still runs into a infinite loop, because the conditions are weird. And in normal cases you really shouldn't change the iteration-variable of your loop inside your loop (only if you know thats exactly what you want) but in most cases that just breaks your program, especially if youre starting to learn the language.
Maybe if you say exactly what you want, we can help you more.

trouble with loops, functions, and organization´

and I would like to preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an old assignment. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.
MORE INFO:
Player 1 will enter word(of any length / i have been using "Testing") for Player 2 to guess. Player 2 will have 5 letter guesses, and 5 word guesses. If Player 2 enters "Testing" it should be able to ignore the case between upper/lower (WITHOUT using toupper / tolower)
IF: Player 2 enters more than 1 letter for a guess: "aa" make them guess again until they only guess 1 letter "a".
The problems I'm facing is: I don't know where to place everything, I feel I'm mixing up or messing up the functions, and everytime I try to organize it, it only gets worse. I've restarted it several times, I'm just having trouble getting it all laid out.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string word, wordguess, lower, dashes;
string letterguess;
int i = 0;
bool GameOver = false, Validletterguess = true, Validwordguess = true;
cout << "Player 1, enter a word for Player 2 to guess: " << endl;
getline(cin, word);
cout << endl;
cout << "Player 2, you have 5 letter guesses, and 5 word guesses." << endl;
cout << "Guess your first letter: " << endl;
while (GameOver == false) // Start of Game. Setup for Round 1 letter guess and word guess.
{
while (letterguess.length() != 1) // 1 letter only. loop until they enter 1 letter
{
cout << endl << "Type a single letter and press <enter>: ";
cin >> letterguess; // enter letter guess
for (int i = 0; i < letterguess.length(); i++) //ignore case of letter guess
{
if (letterguess.at(i) >= 'A' && letterguess.at(i) <= 'Z')
{
lower += letterguess.at(i) + 32;
}
else
{
lower += letterguess.at(i);
}
}
if (letterguess.at(i) == word.at(i) && Validletterguess == true) //if Player2 guesses a correct letter, replace the dash with letter and display location: ex. If "T" then "You guessed the 1st and 4th letter"
{
cout << "You guessed the first letter right!" << endl; // figure out how to display dashes?
dashes.at(i) = letterguess.at(i);
cout << "Enter your first word guess: " << endl;
cin >> wordguess;
}
else
cout << "Wrong letter! Enter your first word guess: " << endl;
cin >> wordguess;
if (wordguess == word & Validwordguess = true)
{
cout << "You guessed the word correctly in 1 try! " << endl;
Gameover = true;
}
}
}
}
There are several things in C++ that can assist you. It's good to see that you're already using std::string and std::getline to deal with the user input. The problem seems to be that you've gotten tangled up in organizing the game logic so that it flows, and setting up structures that can help you.
I did actually go ahead and write a game just for kicks. The hope is that I can provide some of that and describe it so you can digest in chunks, and you can see how one can build a program up a bit at a time.
So let's start by making a stub for the function that will actually run the game. You can call this from main. It simplifies the actual running of the game by separating it from the other setup and shutdown stuff. It also means you can run several games in a row later, without having to modify the game loop.
enum GameResult {
None,
Win,
Loss,
};
GameResult PlayHangman(const std::string& target, int wrongLetterLimit, int wrongWordLimit)
{
return None;
}
And to illustrate that point, here is the full main that I ended up writing to invoke this game. Even though it's a one-off, you can see that it can be useful. In this case, I chose to read the game settings from the command line:
void ExitSyntaxMessage(int code = -1)
{
std::cerr << "Syntax: hangman <word> [guesses [wordGuesses]]\n";
exit(code);
}
int main(int argc, char** argv)
{
// Get game settings
std::string target;
int letterGuesses = 5;
int wordGuesses = 5;
try {
if (argc < 2) throw std::runtime_error("Not enough arguments");
target = argv[1];
if (argc > 2) letterGuesses = std::stoi(argv[2]);
if (argc > 3) wordGuesses = std::stoi(argv[3]);
}
catch(...)
{
ExitSyntaxMessage();
}
// Play game
GameResult result = PlayHangman(target, letterGuesses, wordGuesses);
// Deliver result and exit
switch(result)
{
case Win:
std::cout << "Congratulations!\n";
return 0;
case Loss:
std::cout << "Better luck next time!\n";
return 1;
default:
std::cout << "Game stopped.\n";
return -2;
}
}
So, now there's a simple framework for your game to run in. There's not much code, but it's something you can immediately start testing before moving on to fleshing out the game itself.
At this point, I should mention some headers that this program will be needing. Some will have been required already. Others are required for stuff we're about to do.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <set>
On to the game... A helper function to turn a string into lowercase is always handy. We'll definitely make use of that. Note that this uses a lambda function. If you don't have a modern C++ compiler (with C++11 support) you can just use an ordinary function pointer instead.
std::string Lowercase(const std::string& s)
{
std::string lc(s);
std::transform(lc.begin(), lc.end(), lc.begin(),
[](char c)->char{ return std::tolower(c); });
return lc;
}
Now it's time to expand on the PlayHangman stub. It's still gonna be a stub, but we can set up a few things that we'll be needing and get those tested before proceeding.
GameResult PlayHangman(const std::string& target, int wrongLetterLimit, int wrongWordLimit)
{
GameResult result = None;
// Create lowercase target and add its characters to set of remaining letters
std::string lcTarget = Lowercase(target);
std::set<char> lettersRemaining(lcTarget.begin(), lcTarget.end());
std::set<std::string> guesses;
// Set up game parameters
int letterGuessesRemaining = wrongLetterLimit;
int wordGuessesRemaining = wrongWordLimit;
// Sanity-test to ensure game is winnable
if (wordGuessesRemaining == 0 && letterGuessesRemaining < lettersRemaining.size())
{
std::cout << "Game is not winnable...\n";
return None;
}
// Game loop until stream error or game finishes
bool done = false;
while (!done)
{
done = true; // The loop is a stub for now
}
//// ^^^ for now, just use this bit to test the game setup stuff.
//// Make sure that your lowercase bits are working and the set of
//// remaining letters works. You can add some output code to debug
//// their values and run tests from the command line to verify.
return result;
}
That is going to be the primary structure of a single game. So let's talk about it. Notice again how I'm still not going into detail. At this point, I've already thought about how I should logically be running the game.
Now, I should say that in reality, most people don't write code in a linear way like this from the outside in. It's more of an organic process, but I do take care to separate stuff out into logical bits, and reshuffle/organize stuff as I go. I also try not to do too much at once.
You'll see by the way I've presented this, that I'm encouraging you to develop a solid platform in which to write your game logic. By the time you're writing that logic, you should be able to trust that everything else already works because you tested it.
Some things happening up there are:
The target string is copied into a lowercase version of itself. This will be used to test the word-guesses. There are other ways to test strings ignoring case, but this is just a simple way.
Because we've built that string, we can also use it to construct a std::set containing exactly one of each unique character in that string. That's a one-liner, constructing the set from the string's iterators. Very neat and tidy!
We also have a set of strings called guesses -- this will track all the guesses (correct/incorrect inclusive) so that you don't get penalized for accidentally repeating something you already guessed.
There's a sanity check, which is a duplicate of what will eventually be the end-game test inside the loop. To be honest, that was one of the last things I added, but I've put it here because it's part of the pre-game setup, and apart from the stubbed loop, this is the entire "game" sequence.
Checkpoint : Game skeleton complete
At this point, you might have seen enough to go off and complete the game. There are some important concepts introduced up there. In particular, the idea of storing the remaining letters as a std::set might be just the kind of trick that makes everything click into place.
Reading from here on will complete the program. It's up to you whether you want to do that, or stop reading and have a crack at it yourself first.
Let's start fleshing out some of the game loop. First, you probably wanna deal with showing the current game state and requesting input. That happens in two steps. The first part builds a string by hiding characters that are not yet guessed and then outputs it. The second part is an input-validating loop that discards empty lines, ignores duplicate guesses and handles end-of-stream.
Note that the input is converted to lowercase. This just simplifies things. Especially when checking for duplicate guesses.
while (!done)
{
// Create prompt from original string with a dash for each hidden character
std::string prompt(target);
for(char& c : prompt)
{
if (lettersRemaining.count(std::tolower(c)) != 0) c = '-';
}
std::cout << prompt << "\n";
// Get input
std::string input;
for (bool validInput = false; !validInput && !done; )
{
std::cout << "> " << std::flush;
if (!std::getline(std::cin, input))
{
done = true;
}
else if (!input.empty())
{
input = Lowercase(input);
validInput = guesses.insert(input).second;
if (!validInput)
{
std::cout << "You already guessed that!\n";
}
}
}
if (done)
continue;
// TODO: Process guess, update game state, and check end-game conditions
}
Once again, we have expanded on the implementation and now have something to test. So make sure it all compiles and works the way you want it to. Obviously the game will run forever right now, but that's fine -- you can just terminate the process.
When you're happy, move on to the actual logic. This is where we start putting together everything that has already been set up.
Thanks to our input loop, we now know that the input is now a new guess comprising either 1 letter or a word. So I start by branching for either the letter guess or the word guess. You should start to see a pattern here, right? Once again, I write an empty section of code to do something, and then start actually filling it in...
// Check the guessed letter or word
bool correctGuess = false;
if (input.size() == 1)
{
if (letterGuessesRemaining == 0)
{
std::cout << "No more letter guesses remain.\n";
}
else
{
// Test the guessed letter
}
}
else
{
if (wordGuessesRemaining == 0)
{
std::cout << "No more word guesses remain.\n";
}
else
{
// Test the guessed word
}
}
So, the letter test... Recall we already built the lettersRemaining set and tested it. And those are the only ones obscured by dashes in the prompt. So it then becomes trivial to determine whether they guessed one. If it's in the set, they guessed correctly and you remove it from the set. Otherwise, they burn up one of their guesses.
Because the input is already lowercase, we can use the letter verbatim to search within the values stored in the set (which are also lowercase).
// Test the guessed letter
char letter = input[0];
if (lettersRemaining.count(letter) != 0)
{
correctGuess = true;
lettersRemaining.erase(letter);
}
else
{
std::cout << "Nope!\n";
--letterGuessesRemaining;
}
The word test is even easier. Recall that we stored a lowercase version of the target word already, and the input was also converted to lowercase. So we just compare. You see how all this lowercase business has actually made life less complicated?
// Test the guessed word
if (input == lcTarget)
{
correctGuess = true;
lettersRemaining.clear(); //<-- we can use this to test for a win
}
else
{
std::cout << "Nope!\n";
--wordGuessesRemaining;
}
We are quite literally almost done! The only thing left to do is check whether the game should stop due to being won or lost. That's the last part of the game loop.
Because the code handling a correct word guess is also polite and clears the lettersRemaining set, we can use that as a test for a winning condition regardless of whether a letter or word was guessed.
You'll also see that bit of logic again for the game losing condition. Recall that from before the main loop where we checked if it was even possible to win.
// If guessed incorrectly, show remaining attempts
if (!correctGuess)
{
std::cout << "\nAttempts remaining: "
<< letterGuessesRemaining << " letters, "
<< wordGuessesRemaining << " words.\n";
}
// Check if game is complete
if (lettersRemaining.empty())
{
std::cout << target << "\n";
result = Win;
done = true;
}
else if (wordGuessesRemaining == 0 && letterGuessesRemaining < lettersRemaining.size())
{
std::cout << target << "\n";
result = Loss;
done = true;
}
I hope this has been helpful, that you've been able to follow along, and that you understand the breakdown and explanation. This is generally how I approach programming. I like to build up pieces of code that I can rely on, instead of getting lost in some details and overlooking more fundamental things.
There may be some techniques, language features or parts of the standard library used here that you have not encountered before. That's fine -- you can use that to learn, experiment and research online. Keep https://cppreference.com bookmarked in your browser.
If nothing else, I hope that this gives you some insight in breaking down tasks into small bits that you care about now, and other stuff that you can worry about later. Building up a program iteratively this way enables you to test code regularly and increases your chances of finding silly mistakes that could hamstring you later. It is so common to see beginners just write a whole program in one hit, run it, then freak out because it doesn't "work".

How can I read accented characters in C++ and use them with isalnum?

I am programming in French and, because of that, I need to use accented characters. I can output them by using
#include <locale> and setlocale(LC_ALL, ""), but there seems to be a problem when I read accented characters. Here is simple example I made to show the problem :
#include <locale>
#include <iostream>
using namespace std;
const string SymbolsAllowed = "+-*/%";
int main()
{
setlocale(LC_ALL, ""); // makes accents printable
// Traduction : Please write a string with accented characters
// 'é' is shown correctly :
cout << "Veuillez écrire du texte accentué : ";
string accentedString;
getline(cin, accentedString);
// Accented char are not shown correctly :
cout << "Accented string written : " << accentedString << endl;
for (unsigned int i = 0; i < accentedString.length(); ++i)
{
char currentChar = accentedString.at(i);
// The program crashes while testing if currentChar is alphanumeric.
// (error image below) :
if (!isalnum(currentChar) && !strchr(SymbolsAllowed.c_str(), currentChar))
{
cout << endl << "Character not allowed : " << currentChar << endl;
system("pause");
return 1;
}
}
cout << endl << "No unauthorized characters were written." << endl;
system("pause");
return 0;
}
Here is an output example before the program crashes :
Veuillez écrire du texte accentué : éèàìù
Accented string written : ʾS.?—
I noticed the debugger from Visual Studio shows that I have written something different than what it outputs :
[0] -126 '‚' char
[1] -118 'Š' char
[2] -123 '…' char
[3] -115 '' char
[4] -105 '—' char
The error shown seems to tell that only characters between -1 and 255 can be used but, according to the ASCII table the value of the accented characters I used in the example above do not exceed this limit.
Here is a picture of the error dialog that pops up : Error message: Expression: c >= -1 && c <= 255
Can someone please tell me what I am doing wrong or give me a solution for this? Thank you in advance. :)
char is a signed type on your system (indeed, on many systems) so its range of values is -128 to 127. Characters whose codes are between 128 and 255 look like negative numbers if they are stored in a char, and that is actually what your debugger is telling you:
[0] -126 '‚' char
That's -126, not 126. In other words, 130 or 0x8C.
isalnum and friends take an int as an argument, which (as the error message indicates) is constrained to the values EOF (-1 on your system) and the range 0-255. -126 is not in this range. Hence the error. You could cast to unsigned char, or (probably better, if it works on Windows), use the two-argument std::isalnum in <locale>
For reasons which totally escape me, Windows seems to be providing console input in CP-437 but processing output in CP-1252. The high half of those two code pages is completely different. So when you type é, it gets sent to your program as 130 (0xC2) from CP-437, but when you send that same character back to the console, it gets printed according to CP-1252 as an (low) open single quote ‚ (which looks a lot like a comma, but isn't). So that's not going to work. You need to get input and output to be on the same code page.
I don't know a lot about Windows, but you can probably find some useful information in the MS docs. That page includes links to Windows-specific functions which set the input and output code pages.
Intriguingly, the accented characters in the source code of your program appear to be CP-1252, since they print correctly. If you decide to move away from code page 1252 -- for example, by adopting Unicode -- you'll have to fix your source code as well.
With the is* and to* functions, you really need to cast the input to unsigned char before passing it to the function:
if (!isalnum((unsigned char)currentChar) && !strchr(SymbolsAllowed.c_str(), currentChar)) {
While you're at it, I'd advise against using strchr as well, and switch to something like this:
std::string SymbolsAllowed = "+-*/%";
if (... && SymbolsAllowed.find(currentChar) == std::string::npos)
While you're at it, you should probably forget that you ever even heard of the exit function. You should never use it in C++. In the case here (exiting from main) you should just return. Otherwise, throw an exception (and if you want to exit the program, catch the exception in main and return from there).
If I were writing this, I'd do the job somewhat differently in general though. std::string already has a function to do most of what your loop is trying to accomplish, so I'd set up symbolsAllowed to include all the symbols you want to allow, then just do a search for anything it doesn't contain:
// Add all the authorized characters to the string:
for (unsigned char a = 0; a < std::numeric_limits<unsigned char>::max(); a++)
if (isalnum(a) || isspace(a)) // you probably want to allow spaces?
symbolsAllowed += a;
// ...
auto pos = accentedString.find_first_not_of(symbolsAllowed);
if (pos != std::string::npos) {
std::cout << "Character not allowed: " << accentedString[pos];
return 1;
}

In C/C++, how do you edit a certain 'coordinate' in stdout?

I've been using Vim a lot lately, and I was wondering how the program manages to change the characters at certain positions in the terminal. For example, when using :rc, it replaces the character under the cursor with c.
I have also seen similar things done with Homebrew, which prints a progress bar to the screen and updates it when necessary.
How is this done in C/C++?
There is no standard way of doing this in C++.
It is done with OS dependent lbiraries, such as curses and similar libraries (ncurses) in the Unix/Linux world. Some of these libraries have been ported on across platforms (example: PDCurses)
For very simple things such as a progress bar or a counter, and as long as you remain on a single line there is the trick of using "\r" (carriage return) in the output, to place the cursor back at the begin of the current line. Example:
for (int i = 0; i < 100; i++) {
cout << "\rProgress: " << setw(3) << i;
this_thread::sleep_for(chrono::milliseconds(100));
}
Certainly, using ncurses or similar library is a good answer. An alternative may be to use ANSI Escape Codes to control the cursor in some terminal emulators (but not Windows command shell). For example, this code prints a line in multiple colors and then moves the cursor to 2,2 (coordinates are 1-based with 1,1 being the upper left corner) and prints the word "red" in the color red.
#include <iostream>
#include <string>
const std::string CSI{"\x1b["};
const std::string BLUE{CSI + "34m"};
const std::string RED{CSI + "31m"};
const std::string RESET{CSI + "0m"};
std::ostream &curpos(int row, int col)
{
return std::cout << CSI << row << ';' << col << 'H';
}
int main()
{
std::cout << "This is " << BLUE << "blue" << RESET << " and white.\n";
curpos(2,2);
std::cout << RED << "red" << RESET << '\n';
}
As mentioned that's not a matter of any C/C++ standard operations provided with stdout or cout (besides writing the necessary control characters to the screen).
Controlling the screen cursor of an ASCII terminal totally depends on implementation of the particular terminal program used, and besides a very narrow set of control characters, there's no standard established.
There are libraries like ncurses for a broader variety of linux terminal implementations, or PDcurses for a windows CMD shell.
I'm not sure to understand you completely but with creating an array of 100 elements of type char you can modify any position of the array and loop it with a std:cout to mostrate it on the console.
Perhaps could be better define the array of 50 chars to resuce the size of the printed result.
For example, if you have to print a progessbar in the 1% process, you should print:
Char progressbar[100] = {'X','','','','','','','','',........}

tokenizing and converting to pig latin

This looks like homework stuff but please be assured that it isn't homework. Just an exercise in the book we use in our c++ course, I'm trying to read ahead on pointers..
The exercise in the book tells me to split a sentence into tokens and then convert each of them into pig latin then display them..
pig latin here is basically like this: ball becomes allboy in piglatin.. boy becomes oybay.. take the first letter out, put it at the end then add "ay"..
so far this is what i have:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cstring>
using std::strtok;
using std::strcat;
using std::strcpy;
void printPigLatin( char * );
int main()
{
char sentence[500];
char *token;
cout << "Enter string to tokenize and convert: ";
cin.getline( sentence, 500 );
token = strtok( sentence, " " );
cout << "\nPig latin for each token will be: " << endl;
while( token != NULL )
{
printPigLatin( token );
token = strtok( NULL, " " );
}
return 0;
}
void printPigLatin( char *word )
{
char temp[50];
for( int i = 0; *word != '\0'; i++ )
{
temp[i] = word[i + 1];
}
strcat( temp, "ay" );
cout << temp << endl;
}
I understand the tokenizing part quite clearly but I'm not sure how to do the pig latin.. i tried to start by simply adding "ay" to the token and see what the results will be .. not sure why the program goes into an infinite loop and keeps on displaying "ayay" .. any tips?
EDIT: this one works fine now but im not sure how to add the first letter of the token before adding the "ay"
EDIT: this is how i "see" it done but not sure how to correctly implement it ..
You're running over your input string with strcat. You need to either create a new string for each token, copying the token and "ay", or simply print the token and then "ay". However, if you're using C++ why not use istream iterators and STL algorithms?
To be honest, I severly doubt the quality of the C++ book, judging from your example. The “basic stuff” in C++ isn't the C pointer style programming. Rather, it's applying high-level library functionality. As “On Freund” pointed out, the C++ standard library provides excellent features to tackle your task. You might want to search for recommendations of better C++ books.
Concerning the problem: your printPigLatin could use the existing function strcpy (or better: strncpy which is safer in regards to buffer overflows). Your manual copy omits the first character from the input because you're using the i + 1st position. You also have a broken loop condition which always tests the same (first) character. Additionally, this should result in an overflow anyway.
As the people before me pointed out, there are several other methods of achieving what you want to do.
However, the actual problem with your code seems to be the use of strcat, I see that you changed it a bit in the edit. Here is an explanation of why the initial one did not work char* and size issues
Basically, the pointer does not allocate enough memory to add the "ay" to the string provided. If you create a pointer using the technique shown in the link, it should work fine.
I got your program to work, taking the strcat out and using
cout << word << "ay" << endl
Your loop is infinite because of *word != '\0'.
The word pointer is not changed at any time in the loop.
This seemed to have worked:
void printPigLatin( char *word )
{
cout << word + 1 << word[0] << "ay" << endl;
}
Just not sure if it's a good idea to do that.