473,513 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add strings to vectors?

5 New Member
Hi,
I have a vector I want to add book titles to, then i want to print my updated vector. This is best I have come up with but the program fails at the getline line. why? and how can I fix this?

string book;
cout << "Enter book to add: "<< endl;
getline(cin, book);
books.push_back(book);

for(int i = 0; i < books.size(); ++i)
{
cout << i+1 << ". " << books[i] << endl;
}
Apr 19 '12 #1
1 1903
weaknessforcats
9,208 Recognized Expert Moderator Expert
getline cannot put data into a C++ string object. Just look at the function prototype for getline(). You will need a char buffer. getline will put the data in that buffer and add a null terminator after the last byte. The buffer will now have a C string in it and you can assign that string to your C++ string object:

Expand|Select|Wrap|Line Numbers
  1. char buffer[80];  //79 character data + 1 for null terminator
  2. string book;
  3. getline(cin, buffer);
  4. book = buffer;     //assign buffer to string object
Apr 20 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

30
3826
by: zexpe | last post by:
I have an extremely cpu/data intensive piece of code that makes heavy use of the following function: void convertToDouble(const std::string& in, double& out) { out = atof(in.c_str()); } I...
1
1693
by: Dekker | last post by:
Hi I would like to transform the result of a csv-string (eg.: "name,age\nstring,int\nMac,23\nMax,24\nMike,78") into a map of vectors map<string, vector<???> >. The key of the map will be the...
2
1578
by: Ben Wheare | last post by:
Hiya, I'm fairly new to C++, and trying to figure out how to do the following: I have a class, strings, which has a private char array. To add to this array, I call the member function...
8
1635
by: LinuxDuud | last post by:
Hi, First, Sorry if the title was bad, I really didn't know how to describe this. So, What I need to do is something like this: char somevariable; // The tricky part. I don't know how to define...
1
1464
by: pallav | last post by:
I am writing a logic simulator for boolean circuits. I have a textfile which will contain vectors like this abc 001 100 111 ... I want to be able to bit pack these vectors to get a = 110, b...
4
455
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
5
2147
by: andrewmorrey | last post by:
Hello, I've got a VC++ project containing multiple classes and a main function. In one of the class functions, it reads from a text file and places the data into a vector; //...
3
3244
by: mersinli | last post by:
Hello; I will use standard library vectors and implement simple map coloring algorithm. The goal of a map coloring problem is to color a map so that regions sharing a common border have...
4
4373
by: Caudata | last post by:
I am by no means an experienced c++ programmer, but I am trying to use a vector of vectors because it is convenient to store some strings while parsing a text file. I am having trouble with the...
2
2805
by: Laura Wilkinson | last post by:
I've got many differences between vectors/strings and arrays but I need more details. I guess I don't really understand it enough to do this but this is what I've concluded so far; An array is a...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.