3

I am asking the user to input a string on console. But I don't know the length of string.

How can I define a structure to fit the input with variable length?

int main(){
    int i;
    char s[10];

    cout << "input string:";
    cin >> s;

    return 0;
}

The sample code will cause heap corruption if the input string length exceeds 10.

billz
  • 44,644
  • 9
  • 83
  • 100
Zachary
  • 1,633
  • 2
  • 22
  • 34

6 Answers6

10

Use std::string instead. For example:

#include <string>

 std::string s;

 std::cout << "input string:";
 std::cin >> s;

Or use std::getline to get a line until endline character

std::getline(std::cin, s);
billz
  • 44,644
  • 9
  • 83
  • 100
3

In c++, you should use std::string instead of char[], especially for variable length strings.

Jiaming Lu
  • 875
  • 6
  • 20
2

This is a working, general example that allows you to read in strings including white space:

#include <string>
#include <iostream>
int main()
{
  std::string s;
  std::cout << "Type a string\n";
  std::getline(std::cin, s);
  std::cout << "You just typed \"" << s << "\"\n";
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

cplusplus.com says that the >> operator for strings from an input stream uses whitespaces as a seperator. so if you need your string to be able to contain whitespaces you have to use std::getline(...) (wich is different from istream::getline(...)!!!!)

basically it goes like this:

std::string inputString;

std::getline(cin, inputString);

my answer was inspired by this answer

Community
  • 1
  • 1
xmoex
  • 2,602
  • 22
  • 36
0
#include <iostream>
#include <string>
using namespace std;

int main(){
    int i;
    string s;

    cout << "input string:";
    cin >> s;

    return 0;
}

Use std::string instead of char[].

If you need to use char[] after the input, you can refer to these questions:

std::string to char*

convert string to char*

For example,

string s1;
cin >> s1;

char *s2;
s2 = new char[s1.length() + 1]; // Including \0
strcpy(s2, s1.c_str());

delete []s2;

You can use malloc and free if you don't know about new and delete.

Community
  • 1
  • 1
Naetmul
  • 14,544
  • 8
  • 57
  • 81
  • string class is defined in std namespace. So there is no need to include "string" header file. – Zachary May 28 '13 at 07:25
  • 2
    @Zack I'm pretty sure different things can be declared in the same namespace in different files. or, the members of a namespace can be declared in different files – matt May 28 '13 at 07:32
  • @matt This is really interesting! I didn't realize it std is defined in different files. Weird! – Zachary May 28 '13 at 07:38
  • yep, std is just a namespace, not a class, namespaces are simply used for what the name suggests, to group a collection of declarations. entire libraries, apis, engines, with loads of different classes each declared in their own header, may be put in the same namespace. namespaces are used to neatly group declarations, and to stop them conflicting with identically named declarations of other included libraries etc. it beats just tacking the library name onto the beginning of every declaration as you can shortcut the name with the 'using' keyword. – matt May 28 '13 at 07:44
0

Basically it is suggested that you should always std::string to get variable length input. Still if you need to store the input in an array to pass it to a function or something. you can go for this. Though its quite lame.

/* #include <string> */
std::string s;
std::cout<<"Enter the String";
std::getline(std::cin, s);
char *a=new char[s.size()+1];
a[s.size()]=0;
memcpy(a,s.c_str(),s.size());
std::cout<<a;  

Regards

Genocide_Hoax

Genocide_Hoax
  • 843
  • 2
  • 18
  • 42
  • why memcpy to a seperate cstring? could use s.c_str() to pass it to a function easily... – xmoex May 28 '13 at 07:59
  • 1
    s.c_str() converts it to a const char *. So just in case the coder wants to manipulate the array later he can use this. – Genocide_Hoax May 28 '13 at 08:03
  • true, but you don't know that the coder wants to manipulte the string later on. i thought it could lead to some confusion... – xmoex May 28 '13 at 08:22
  • Well , on that note , it's always better to write flexible code. I suppose the solution s.c_str() have already been posted. So , I thought to add some more flexibility to it. Just to help people who might want to convert the input to a char array which is mutable or rather more flexible. – Genocide_Hoax May 28 '13 at 08:30