User's null-terminated input - c++

just a quick question. I'm looking for the most efficient and clear way to get the user's input of any length and store it, so then I can retrieve it and compare to another input. I also need the user's input to be null-terminated. Can I write something like
string inp;
cin >> inp;

A std::string will manage itself and grow to accommodate the input that is given to the program. If you have
std::string input;
std::getline(std::cin, input);
This will get input from the user that includes spaces that can be as big as the input stream can hold. Now that you have the string if you need to pass it to some function that needs an old null terminated c-style string then you would use the c_str() function. c_str() does return a const char * so you will not be able to modify the string data with it.
If you really need a modifyable c-style string then you can make one with
char * old_style_string = new char[input.size() + 1];
std::strcpy(old_style_string, input.c_str());

Related

Extract Words from a string, then store in variables

I am trying to store user input into a string and then use that string to store individual words into character variables which I will later use to set up a class object. To be clear, I'd like a method that doesn't include using vector or stringstream since I haven't touched on those subjects yet and I'm hoping there's a way to do this without those functions or the libraries they are included in.
The class I'm working with:
class Name
{
char* m_firstName{};
char* m_middleName{};
char* m_lastName{};
}
So far the code I am using works, but obviously does not split up the names - I'm only able to send one string to one variable:
std::istream& Name::read(std::istream& istr){
string input;
getline(istr, input);
char* firstName = new char[input.length() + 1];
strcpy(firstName, input.c_str());
set(firstName);
return istr;
}
I need to be able to pull out words inside the string which will be delimited by a space. For example, if the input is "John Adam Smith", I'd like to be able to store "John", "Adam" and "Smith" inside of firstName, middleName and lastName character string variables respectively.
I will then be using those variables within a "set" function that I have for inserting the data into the class. I'm not worried about that part, once I have the variables I should be good to go. But I can't figure out how to separate the words inside the string.
Just go
string first;
string middle;
string last;
istr >> first >> middle >> last;
Things wrong in your current code:
There is no way to get data into Name all the fields a re private and you have no methods.
You should have strings everywhere, dont dynamically allocate char*s in c++ unless you have to. Dont juggle raw pointers.

How to divide the input taken from user in two parts and assign them to two different arrays in C++?

If we take the input from user by asking them, like below:
cout << "Enter your course code and course name: ";
Now if the user enters CS201 Introduction to Programming, how can I only assign the code part, i.e. CS201 to an array, let's say;
char courseCode[10];
And how can I assign the name part in the array, let's say:
char courseName[50];
I want to do this to 5 students, using the structure defined below:
struct student
{
char courseName[50];
char courseCode[10];
};
student stu[5];
It's actually kind of simple once you remember that the input operator >> stops on white-space, and also know about the std::getline function.
Then you can do something like
std::string courseCode;
std::string courseName;
std::cin >> courseCode;
std::getline(std::cin, courseName);
Note that I use std::string for the strings instead of arrays. This is what you really should use. If you're not allowed (by your teacher or something) and must use arrays, then you can't use std::getline but instead have to use std::istream::getline instead.
Store the input in a single string say x
Now on x perform linear search for the first whitespace and split the string about the first whitespace. Store the two resultant strings in your struct.
I solved my problem using the cin.getline() functions to get the string in token pointer and then used strchr(char [], cahr) of <string> header file to separate the current string from the place where the first white space comes. Then I copied both separated strings into my desired elements of my structure using strcpy() function.

Fastest way to input huge strings?

I have to read huge lines of strings from stdin so time is a critical issue. Strings are on consecutive lines and have no spaces so I can simply use while(cin>>str) { //code } but this is extremely slow. I have heard that scanf is much more faster than cin but if I use scanf("%s,str) I think that str is treated as char* and not a C++ string so I can't use the STL. I could take input as char* and copy all the chars into a C++ string but IMO that will also be slow.
Is there a way to get input using scanf or something but still get a C++ string as a result?
If you know the average or maximum size of the text, you create std::string with a pre-allocated size. One area occupying a lot of time is the memory (re) allocation by std::string.
cin >> str is the closest thing you'll find in STL to scanf("%s, str"). The only reason scanf would be faster than cin is because it would be giving you a char* instead of a string, and while you can create a new string from the char* by just passing them in to the string() constructor, that would be almost the same thing as using cin >> str.
You can use getline:
for (std::string line; getline(std::cin, line); ) {
do_something_with(line);
}
I don't know if it is any faster than cin >> line, but it might be, since it doesn't need to deal with whitespace other than newlines. But I don't believe this is as significant as the overhead of sentry construction.

Reading an input with space in c++

currently I am doing to read input with spaces in it.
int main() {
char str[100];
string st;
cin.getline(str,100);
st=str;
}
I want to utilize the functions that come along with the string, so I am reading the input into a string. Is there any other way to read the input directly into the string which also allow space.
If you're going to use std::string objects, just use std::getline.
std::string st;
std::getline(std::cin, st);
using gets () function.
It accepts even space as input.
Eg. )
gets (variable_name);

Getting input from user using cin [duplicate]

This question already has answers here:
C++ "cin" only reads the first word [duplicate]
(5 answers)
Closed 4 years ago.
I am using Turbo C++ 3.0 Compiler
While using the following code ..
char *Name;
cin >> Name;
cout << Name;
When I gave input with space ... its only saving characters typed before space ..
like if I gave input "QWERT YUIOP" ... Name will contain "QWERT";
Any explaination why ??
When cin is used to read in strings, it automatically breaks at whitespace unless you specify otherwise.
std::string s;
std::cin >> noskipws >> s;
Alternatively, if you want to get a whole line then use:
std::getline(cin, s);
You'll also want to allocate storage for a raw char array, but with C++ you should use std::string or std::wstring anyway.
You need to allocate space for the char array into which you want to read the Name. char *Name; will not work as it only declares a char pointer not a char array. Something like char Name[30];
Also the cin << only allows us to enter one word into a string (char name[30]).
However, there is a cin function that reads text containing blanks.
cin.get(name, MAX)
get will read all characters including spaces until Max characters have
been read or the end of line character (ā€˜\nā€™) is reached and will put them
into the name variable.
You just declared a character pointer that doesn't point at anything. You need to allocate space for your string. The most common method would be to allocate space on the stack, IE:
char Name[50];
Remember a char pointer by itself is just a place to put an address to where the real memory is. You still have to get a block of memory and store the address in your pointer. The code above creates an array of Names on the stack and you can use Name to store up to 49 chars plus a null terminal.
Alternatively, for variable length strings use std::string.