0

I want to write a program in C or C++ that asks to user to enter the string at run time of different lengths at different times as given by user (either space separated or non space separated) and store it into an array. Please give me the sample code in C and C++ for it.

e.g.

1st run:
Enter string
Input: Foo 

Now char array[]="foo";

2nd run:
Enter string
Input:
Pool Of

Now char array[]="Pool Of";

I have tried:

#include<iostream>
using namespace std;

int main()
{
    int n;
    cout<<"enter no. of chars in string";
    cin>>n;
    char *p=new char[n+1];
    cout<<"enter the string"<<endl;
    cin>>p;
    cout<<p<<endl;
    cout<<p;
    return 0;
} 

But it's not working when the string is space separated.

I have tried this too, but it's not working either.

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout<<"enter no. of chars in string";
    cin>>n;
    char *p=new char[n+1];
    cout<<"enter the string"<<endl;
    cin.getline(p,n);
    cout<<p<<endl;
    cout<<p;
    return 0;
}
10
  • 2
    Post the code and explain what part you are having difficulty with. Also is this supposed to be C or C++ ? Commented Jul 10, 2011 at 17:29
  • 2
    The code needs to go in the question, not in a comment. I've edited it into the question now for you but please pay more attention to detail when posting questions on SO in future. Commented Jul 10, 2011 at 17:34
  • 2
    @gautam kumar by the way that is c++ Commented Jul 10, 2011 at 17:36
  • 1
    @gautam: Why are you shouting at me? And why did you edit unformatted code into your question, despite being able to plainly see that it's not displaying properly, and despite having been previously shown how to format your code? And despite the huge "Formatting FAQ" pane that appears next to the place where you write your post? And despite having been a member of Stack Overflow for almost a year and a half, with 26 questions behind you? Come on... Commented Jul 10, 2011 at 17:49
  • 1
    @Tomalak: given that the first letter of both sentences are the only uncapitalized letters, I think caps lock was accidentally turned on. Not that it's much of an excuse, though. Commented Jul 10, 2011 at 18:05

3 Answers 3

2

Use getline.

Take a look a this example:
http://www.cplusplus.com/reference/iostream/istream/getline/

Sign up to request clarification or add additional context in comments.

4 Comments

But its not asking for user to give the lengh of the string.It is statically declaring a char array at compile time.I want the arrray to be initialised at run time.
@gautam-kumar I am pretty sure you can do it without me writting the program for you. Allocate the buffer like you did in the code you posted. then use getline as shown in this example.
:Ihave tried it but its not working.please see it I ghave given it too in the question
If you use string library version of std::getline then you can pass it a std::string object, and not have to worry about allocating the right amount of memory.
0

You need to read chars from stdin until you meet some terminator (newline for example) and append that char to the end of your temporary char array (char*). For all this you should manually control overflows and extend (reallocate+copy) array as necessary.

Comments

0

When you press enter you have to take care of the extra char that is also fed to cin.getline() If that is taken care of it will work fine. This is the behavior of cin in Windows. May happen same in Linux. If you run this code in Windows it will give you what you want.

#include <iostream>
using namespace std;

int main () {
    int n;

    // Take number of chars
    cout<<"Number of chars in string: ";
    cin>>n;

    // Take the string in dynamic char array
    char *pStr = new char[n+1];
    cout<<"Enter your string: "<<endl;

    // Feed extra char
    char tCh;
    cin.get(tCh);

    // Store until newline
    cin.getline(pStr, n+1);
    cout<<"You entered: "<<pStr<<endl;

    // Free memory like gentle-man
    delete []pStr;

    // Bye bye
    return 0;
}

HTH!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.