so far I ask user for number of terms :

cout<<"Enter the number of therms in the array \n\n";
cin>>x;
for(i=0; i<x; i++)
  {
   cin>>arr[i];
  }

But It looks preety non-user friendly, I wanna input it just like we enter a string.
We the user should hit shift array input should terminate.

cout<<"Press (shift) after the last term of the array \n\n";
for(i=0; arr[i]!= (char)16 ; i++)
 {
  cin>>arr[i];
 }

It dosen't work :(

You can't use cin like that, because your shell is most likely in canonical mode. Basically what happens is the shell waits for the user to give a newline before it supplies your input buffer with the user input, and it won't contain how many times a user hit the shift key, for instance. If you want to detect when the shift key is being hit, you'll need to look into your compiler documentation or use a third party library.

Another way of doing this would be to use Ctrl-Z, which sends an EOF character to the stream, and would be easy to check for in the stream.

what is a 3rd party library ?

A library that's written by a third party (i.e. a company other than your compiler vendor).

Whenever I want the user to enter an array of elements (and this is pretty common too) I always just ask for an extra ENTER at the end (a blank line).

So the program might run something like this:

Please enter the elements of the array.
Press ENTER after each element.
Press ENTER on a blank line to stop.
1
2
3
4

Thank you. The array you entered is: 1 2 3 4.

The way to code this is to use getline() to read each line entered by the user. If it is blank, then stop. If not, use a stringstream to convert the element to whatever you want it to be.

vector<int> ints;
string line;

cout << "Please ...";
while (true)
{
    getline( cin, line );
    if (line.empty()) break;

    stringstream ss( line );
    int value;
    ss >> value;

    ints.push_back( value );
}

cout << "Thank ...";
for (int i = 0; i < ints.size(); i++) cout << ints[ i ];
cout << endl;

Hope this helps.

Thats a bit tough for me can you explain a few parts of the program ??

while(True) -----> won't that be an ifinite loop ??

line.empty() what does that do ??

stringstream ss (line) ???

ss>> value ???

ints.push_back ???

plz give me the header files too.

It won't be an infinite loop if you break out at some point.

Google the rest of your questions. "string::empty()" and "stringstream" and "vector::push_back".

Sorry I forgot the headers. They are

#include <iostream>
#include <string>
#include <stringstream>
#include <vector>

Enjoy!

btw I am using Borland Turbo C++ V 3.0

#include <stringstream>
#include <vector> headers don't exist which cpp version u use ??

I am sorry ur code is givig 15 errors in tcc3 can you plz just gimme the logic behind the programme istead of the code btw thanx.

I said <stringstream> when I should have said <sstream> . If you had googled "stringstream" like I asked you to you would have found that to use a stringstream you need to include <sstream>.

This is standard C++ code. If your compiler can't handle it you need to upgrade to a better/newer version compiler.

Since this all seems to be going straight over your head, just use the ^Z method. You can check to see if the stream is closed using an if statement.

vector<int> ints;
int i;
while (cin) {  // while not at the end of the stream
  cin >> i;  // get an integer
  ints.push_back( i );  // add the integer to the "array" of integers
  }

Hope this helps.

my c++ version has putsback but I says it pushes a character back.

also what is this line ?
vector<int> ints;

int vector,ints; ??????

nad what is ^Z thanx for the help so far.

vector<int> ints; ???? what kind of declaration is this ???? google seems to have no answer ?? is there any simpler more understandable code ?

If you haven't learned to use vectors yet, don't worry about it. (A vector is the STL class that implements a dynamic array.)

What is important is the algorithm suggested to answer your problem. I suggested that you simply read a line at a time. If the line is empty (because the user just pressed ENTER) then you are done getting elements. If the line is not empty, then convert it to an integer (or whatever you asked for) and add it to your array/linked list/whatever.

ther is nothing like empty() or push_back() in turbo c++ nor is ther any vector.h .

there is no vector.h or empty() or push back in turbo c++.

You're using a compiler that has been out of date for a very long time. It does not compile standard C++.

Can't change my compiler coz I am not allowed to. I am a school student And this compiler is specified by my eduction board.

Well, then you're stuck learning a dead dialect.

getline( cin, line );
    if (line.empty()) break;

    stringstream ss( line );
    int value;
    ss >> value;

    ints.push_back( value );

here " cin " is the name of the array ??? What is its size ??? line ??

and if the element is found to be empty //// input breaks ??


and what does the

stringstream ss( line );
    int value;
    ss >> value;
getline( cin, line );
    if (line.empty()) break;

    stringstream ss( line );
    int value;
    ss >> value;

    ints.push_back( value );

here " cin " is the name of the array ??? What is its size ??? line ??

and if the element is found to be empty //// input breaks ??

Is my interpretation correct ???????


and what does the

stringstream ss( line );
    int value;
    ss >> value;

do ??

cin is the name of your input, so what the user enters. (cout, cin get it?)

and what does the [..code] do

It converts a string to an int.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
  {
  clrscr();

  int i,k=0;
  char  a[20];

  cout<<"Enter the array element press enter and press enter twice after";
  cout<<"\nthe last element i.e enter a blank line\n\n";

  for(i=0 ; a[0]!= 'x' ; i++,k++)
  {
  gets(a);

////  Array need to be coverted into int and stored into another arraY.
  }
  cout<<"\n\nthe elements no. is K \t"<<k;

  getch();
 }

I am almost there now I just need to convert it to integer;plz help me with that.


I dont have sstream.h in my compiler.

google for atoi()

There are a lot of non-standard functions in your code, but I guess you've already been told that?

Niek

ok I will replace gets with getline ...... but I can't change void main even though its illegal by ISO standards as I don't wanna pi*s my teacher off.

ok

suppose

for the first i loop is executed if array is 256

that is 50 53 54 ..... ACSII CODE..

then if I use atoi() It'll be 3 elements 2 5 & 6

but It has to be stored as a single element 256 in a integer array. How to proceed ??

ITS DONE BUT 1 ERROR REMAINS :

I need to clean up the char array so that it dosent contain x when the program is re-executed..

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main()
  {
  clrscr();

  int i,k=0,f=0,num[50],len=0;
  char  a[20];

  cout<<"Enter the array element press enter and press [x] after";
  cout<<"\nthe last element.\n\n";

  for(i=0 ; a[0]!= 'x' ; i++,k++)
  {
  cin.getline(a,(sizeof(a)));
  num[i] = atoi(a);
  }

  len=(k-1);

  cout<<"\n\nthe elements no. is K \t"<<len<<"\n\n";
  cout<<"\n Press any key to display the array";
  getchar();

  clrscr();
  cout<<"Your array is\n\n\n\n";

  for(i=0 ; i<len ; i++)
  {
  cout<<"\n"<<num[i]<<"\n";
  }
  getchar();
  return 0;
 }

just used a[0] = 0;

DONE

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.