Solved

Input question

Posted on 2001-07-01
13
236 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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