Solved

Input question

Posted on 2001-07-01
13
234 Views
Last Modified: 2008-02-01
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
Comment
Question by:fmora060901
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 1

Accepted Solution

by:
glebspy earned 50 total points
ID: 6243689
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
 

Author Comment

by:fmora060901
ID: 6243733
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
 
LVL 1

Expert Comment

by:glebspy
ID: 6243740
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
 
LVL 1

Expert Comment

by:glebspy
ID: 6243745
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
 

Author Comment

by:fmora060901
ID: 6243760
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
 
LVL 1

Expert Comment

by:glebspy
ID: 6243769
Ok switch back to <string> and <iostream> ... and wait for real help!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:irisistable
ID: 6243778
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
 
LVL 2

Expert Comment

by:smitty1276
ID: 6243786
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
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6243938
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6244009
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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6244015
> after the 'include' lines, try writing :
> using namespace std;

sorry, I didn't see this post.  Mine is a duplicate of this advice.
0
 

Expert Comment

by:irisistable
ID: 6244021
it's okay
0
 

Author Comment

by:fmora060901
ID: 6245475
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now