• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Input question

I need a way to take an input and then take the that sentence and enter each word from that sentence into an array.  In other words, the user is going to be prompt to enter some information.  Once the user hits return the program needs to take the sentence and put each word into an array.

I just want to have one prompt and take all the information from that prompt.

ex
Enter sentence: Just testing this program

A[0]=just
A[1]=testing
A[2]=this
A[3]=program
0
fmora060901
Asked:
fmora060901
  • 4
  • 3
  • 2
  • +3
1 Solution
 
glebspyCommented:
Dear fmora,
 Written and tested.

#include<string>
#include<iostream>
void main(void){

  char temp_Char;

  string*A=new string[1];
  string*B;
  int current_String=0;

  cout<<"Enter sentence: Just testing this"
      <<" program"<<endl<<endl;

  cin.unsetf(ios::skipws);

  while(cin>>temp_Char){
    if(temp_Char=='\n')break;
    if(temp_Char==' '){
      B=A;
      A=new string[current_String+2];
      for(int i=0;i<=current_String;i++)A[i]=B[i];
      delete[]B;
      current_String++;
      continue;}
    A[current_String]+=temp_Char;}

  for(int i=0;i<=current_String;i++){
    cout<<"A["<<i<<"]="<<A[i]<<endl;}}
0
 
fmora060901Author Commented:
I don't know why I can not compile your code.  I'm using Microsoft's Visual C++ 6.0 with the lastest service pack.  These are the error messages I'm receiving:

any suggestions?

--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(8) : error C2065: 'string' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(8) : error C2065: 'A' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(8) : error C2061: syntax error : identifier 'string'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(9) : error C2065: 'B' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(9) : warning C4552: '*' : operator has no effect; expected operator with side-effect
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(12) : error C2065: 'cout' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(13) : error C2297: '<<' : illegal, right operand has type 'char [34]'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(13) : error C2065: 'endl' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(15) : error C2065: 'cin' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(15) : error C2228: left of '.unsetf' must have class/struct/union type
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(15) : error C2653: 'ios' : is not a class or namespace name
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(15) : error C2065: 'skipws' : undeclared identifier
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\Desktop\test\Cpp1.cpp(17) : fatal error C1003: error count exceeds 100; stopping compilation
Error executing cl.exe.

Cpp1.exe - 102 error(s), 1 warning(s)
0
 
glebspyCommented:
Eew, blech.

I use unix, so I'm quite worried that I might not be able to solve the problem.

As a first attempt try using
#include<string.h>
#include<iostream.h>
that's with .h

Please other experts with knowledge of microsoft, suggest something.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
glebspyCommented:
Basically it's not finding the string or iostream (that includes cin,cout) classes.

If you know how to get these included, that will certainly solve the problem. These are real standard so it couldn't be very hard.

I wish I knew the answer. Other experts could probably solve this one in a jiffy.

You are using c++ rather than c, aren't you ..
0
 
fmora060901Author Commented:
I did try using:

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

but I did get the same error.  And using I'm using c++ rather than c.
0
 
glebspyCommented:
Ok switch back to <string> and <iostream> ... and wait for real help!
0
 
irisistableCommented:
after the 'include' lines, try writing :
using namespace std;

by the way, this looks like homework and you should try to do it on your own.
0
 
smitty1276Commented:
Both of the headers (string and iostream) ARE C++ headers.  The string class is a CLASS and the iostream class is also a class.   Classes/objects are a feature of C++.
0
 
MadYugoslavCommented:
Try this.

#include "string.h"

#define MAX_LINE_LENGTH 256
void main(void)
{
   char Buffer[MAX_LINE_LENGTH]="";
   char** Ptr;
     printf("Enter sentence:\r\n");
   
   gets(Buffer);
   char *Top, *End;
   int i, Count;
   for(Count=1, Top=Buffer; End=strchr(Top, ' '); Count++)
      Top=End+1;
   Ptr=new(char*[Count]);
   for(Count=0, Ptr[Count]=Buffer; Top=strchr(Ptr[Count], ' '); Count++)
   {
      *Top='\0';
      Ptr[Count+1]=Top+1;
   }
   Count++;

   printf("There are %d %s in sentence\r\n", Count, (Count>1)?"words":"word");
   for(i=0; i<Count; i++)
      printf("%s\r\n", Ptr[i]);
   delete(Ptr);
}

0
 
jasonclarkeCommented:
All that is missing from glebspys answer is the fact that string and cout are in the std namespace.  Just change the first three lines to look like this:

#include<string>
#include<iostream>
using namespace std;
0
 
jasonclarkeCommented:
> after the 'include' lines, try writing :
> using namespace std;

sorry, I didn't see this post.  Mine is a duplicate of this advice.
0
 
irisistableCommented:
it's okay
0
 
fmora060901Author Commented:
I'm new to this system and I do appreciate all the help.  The program did work once I added the line ?using namespace std;?   Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now