473,587 Members | 2,504 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 1912
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
3842
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 would really like to get away from using any old C-style functions. So, I modified the above function to make it follow the C++ convention:
1
1700
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 fieldname (name or age) and the values are stored as vectors in the indicated type (string, int). Like this i can call: result (= a string "Mac") or result (= an int of val 23)
2
1583
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 add_array(int), with an int, which populates the array (in this case with the binary equivalent of that integer). I want to make another class, messages, which encapsulates the strings
8
1639
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 it. somevariable = "Hello"; somevariable = "Goodbye"; And to print them using:
1
1468
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 = 100, c = 101 so that i can simulate them using a 32 bit int or a 64 bit set (hence 32/64 vectors at a time). I'm trying to figure out the most efficient way to quickly read in and bitpack these vectors.
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 COMPACT COHERENT CODE THAT WORKS WITH STRINGS, IT ALL BEGINS TO MAKE SINCE*/ /* The basics of C++ are Classes, that build Types. Which are used to create quick and dirty routines in the smallest possible space. The Classes & Routines uses the...
5
2152
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; // std::vector<std::vector<std::string applications (50, std::vector<std::string>(12)); applications = "Test1"; applications = "Test2";
3
3253
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 different colors In C++ I can represent the neighboring information using a 2D vector of booleans. For example, consider the following 2D neighbor vector: Countries 1 2 3 4 1 - T F T 2 ...
4
4382
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 nested for loop recovery of the stored data and properly dereferencing the data. Here is a bit of test code: #include <iostream> #include <string> #include <vector> using namespace std; struct s { string strA;
2
2814
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 collection of data storage locations, holding the same type of data and can be individually referenced by adding an index to a unique identifier. They can be declared by writing the type, the array name and then the number of elements. A vector...
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.