0

I keep running into an issue with this code in C++:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

int main ()
{
   string words[25];
   int i = 0;
   char * word;
   cout << "Input a phrase, no capital letters please.";
   char phrase[100] = "this is a phrase";
   word = strtok (phrase, " ,.");

   while (word != NULL)
   {
      i++;
      words[i] = word;
      cout << words[i] << " ";
      word = strtok (NULL, " ,.-");
      int g = 0;
   }
   cout << endl << endl;

   int g = 0;
   while (g < i)
   {
      g++;
      char f = words[g].at(0);
      if ((f == 'a') || (f == 'e') || (f == 'i') || (f == 'o') || (f == 'u') || (f == 'y'))
      {
         words[g].append("way");
         cout << words[g] << " ";
      }
      else
      {
         words[g].erase (0,1);
         cout << words[g] << f << "ay" << " ";
      }

   }
   cout << endl;
   system("PAUSE");
}

I actually want my program user to generate the phrase to be put in char phrase[100] but I can't figure out the proper syntax to initiate input on it without screwing up the translation.

This is a program that translates phrases into pig latin BTW.

ctype.h
  • 1,470
  • 4
  • 20
  • 34
David
  • 95
  • 8
  • 1
    What's the smallest snippet of code that will cause the problem? Please post that, so we won't have to read through the entire program. In other words, I think your question is, "How do I read user input into a char array in C++?" – Adam Liss Oct 04 '12 at 23:53
  • 6
    If you are writing C++ and use `strtok` you almost certainly do something wrong. – pmr Oct 04 '12 at 23:55

2 Answers2

2

The preferred way of doing terminal I/O in C++ are streams. Use std::cin and the std::getline function to read strings from input output.

std::string input;
std::getline(std::cin, input);

After that you probably want to get rid of strtok and look at this question to understand how to do string tokenization in C++.

Community
  • 1
  • 1
pmr
  • 58,701
  • 10
  • 113
  • 156
2

What you want is:

char phrase[100];
fgets(phrase, 100, stdin);

Though, as stated in the comments and other answer, you're using C string functions in C++ and this is very odd. You should not do so unless you are required by an assignment or something.

Instead use:

string input;
getline(cin, input);

To tokenize you can do the following:

string token;
size_t spacePos;
...
while(input.size() != 0)
{
    spacePos = input.find(" ");
    if(spacePos != npos)
    {
        token = input.substr(0, spacePos );
        input = input.substr(spacePos  + 1);
    }
    else
    {
        token = input;
        input = "";
    }

    // do token stuff
}

Or, to skip all of that jazz:

string token;

while(cin >> token)
{
    // Do stuff to token
    // User exits by pressing ctrl + d, or you add something to break (like if(token == "quit") or if(token == "."))
}
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • Nice explanation and example! +1 – Adam Liss Oct 05 '12 at 00:13
  • 1
    Ty, now that I finally didn't screw something up that needed editing :P – CrazyCasta Oct 05 '12 at 00:14
  • Your last example is slightly broken: http://ideone.com/wpO2G (notice the input compared to the output) -- This is better: http://ideone.com/AzWYa – Benjamin Lindley Oct 05 '12 at 01:02
  • Really fgets worked great, I only had to erase the last character from the last word because it stored the carriage return. Also my teacher didn't see what everyone's problem with stdtok was. Thanks for the assist. – David Oct 05 '12 at 02:24
  • You compute `input.find(" ")` 3 times if the `if statement` is true. Maybe you have a good reason to do that, but to me, it just seems to be a waste. – Novak Oct 05 '12 at 04:13
  • @GuyDavid Because I was too lazy to optimize it, I was showing the basic idea, not necessarily the exact way to do it. – CrazyCasta Oct 05 '12 at 05:33