Hello. I'm just starting out in C++ and am trying to figure out how to split a users string input into individual words and then put each word into a slot in an array. I think it's going rather well, but I'm running into some problems, and it would be great if you could help! Please try not to make it TOO complicated, I'm still learning. =)

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

using namespace std;

int main ()
{

  ifstream DataBank;
  DataBank.open("DataBank.txt",ios_base::app);

  if(!DataBank.is_open()){
	  cout << "Sorry, the program is experiencing problems with the DataBank." << endl;
  }else{
  cout << "DataBank opened successfully!" << endl;
  cout << "Type something to be processed." << endl;
  }

  while(!DataBank.eof()){

  char str[1000];
  cin >> str;

  char * tok;
  tok = strtok (str," ,.-");

  int i;

  for(i=0;i<10;i++){
  string myArray[] = {tok+i};

  cout << myArray[i] << endl;

		}

    }
  system("PAUSE");
  return 0;
}

First of all you don't need that while loop.Secondly any declaration should be made outside a loop.
You could use tok as an iterator parsing the str string like this

char* myarray[1024];
int step=0;
char* tok=strtok(str,", ");
while (tok!='\0')
 { 
    myarray[step]=strtok(str,NULL);
    step++;
  }

Subsequent calls to strtok(char*,char) must be made with a NULL as the second argument so this should explain that NULL.

#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<fstream>
#include<cstdlib>
 using namespace std;
 int main (){ 
 int x=1;
 string str;
 cout<<"Enter a statement:";
 getline(cin,str);//Use this to input for strings
 char* ntr=new char[str.size()+1];//Declaring a char array
// with str's size + 1 on the heap
 strcpy (ntr, str.c_str());//Copy str's contents to ntr
 char* pch;
 pch=strtok(ntr," ");//first break
 while(pch!=NULL){//strtok returns NULL if it can't break ntr's char 
//array, so if pch is not null, dat means the 1st break was successful
 cout<<pch<<endl;//show the result of the 1st break
 pch=strtok(NULL," ");//next break, 
 }
 delete []ntr;//cleanup
 return 0;
 }

Read below

#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<fstream>
#include<cstdlib>
 using namespace std;
 int main (){ 
 int x=1;
 string str;
 cout<<"Enter a statement:";
 getline(cin,str);//Use this to input for strings
 char* ntr=new char[str.size()+1];//Declaring a char array
// with str's size + 1 on the heap
 strcpy (ntr, str.c_str());//Copy str's contents to ntr
 char* pch;
 pch=strtok(ntr," ");//first break
 while(pch!=NULL){//strtok returns NULL if it can't break ntr's char 
//array, so if pch is not null, dat means the 1st break was successful
 cout<<pch<<endl;//show the result of the 1st break
 pch=strtok(NULL," ");//next break, 
 }
 delete []ntr;//cleanup
 return 0;
 }

Aha! It works perfectly except I would like to place the words into an array word by word, rather than letter by letter. Thanks for your help so far!

This is what I have and have changed so far:

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

      using namespace std;

      int main (){

      int x=1;

      string myString;
      cout<<"Enter a question or statement for the AI to process:";
      getline(cin,myString);

      char* myArray=new char[myString.size()+1];//Declaring a char array with str's size + 1
      strcpy (myArray, myString.c_str());//Copy str's contents to the array

      char* mySentenceBreak;
      mySentenceBreak=strtok(myArray," ");//first break

      while(mySentenceBreak!=NULL){//strtok returns NULL if it can't break ntr's char array, so if pch is not null, dat means the 1st break was successful
      cout<<myArray[1]<<endl;
      mySentenceBreak=strtok(NULL," ");//next break,
      }

      delete []myArray;//cleanup
      return 0;
      }
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.