Storing input into a string buffer

Sep 9, 2010 at 1:10am
I am trying to write a code that will continually prompt the user for their name and then prompt for their favorite quote. When the program reads the input it then stores the user name and quotes into a buffer that displays everything that the user inputted. It also ask the user "Any more quotes" and if the user inputs "no" it says "The buffer contains: " and list everything that the user has inputted. So far I have the entire code but I do not know how to access the shared buffers concurrently. Any help is wanted1
Sep 9, 2010 at 1:44am
Do you mind if we see that code? That problem sounds like it could be solved with an std::vector<std::string> with no need for any shared buffers (assuming the "shared" portion refers to them being shared by threads).

-Albatross

P.S.: I'm enclosing a link to vectors, in case you don't know what they are but want to find out.

http://cplusplus.com/reference/stl/vector/
Sep 9, 2010 at 2:13am
#include <iostream>
#include <string>
using namespace std;



int main()
{
string name;
string quote;
string answer;

cout<< "Enter user name: ";
cin>> name;
cout<< "Enter the quote: ";
cin>> quote;
cout<< "\nAny more users?(enter yes or no) ";
cin>> answer;

do
{
cout<< "\nEnter user name: ";
cin>> name;

cout<< "Enter the quote: ";
cin >> quote;

cout<< "\nAny more users?(enter yes or no): ";
cin>> answer;

} while (answer == "yes");

if(answer == "no")
{

string buffer;

buffer = "The buffer contains:\n ";

cout<< buffer << name << ": " << quote;
}
else
cout<< "Invalid response";
}

Sep 9, 2010 at 2:18am
Ah. In that case, I recommend using a pair of vectors. Check 'em out above. Of note is push_back().

You might still benefit from those temporary objects when using them.

You might benefit from a loop to read out from the vectors.

If you feel that you will have some trouble with vectors, I will happily give some extra advice. :)

-Albatross


P.S.- Warning: The first block of couts and cins will not permit the program to work properly with that loop.
Sep 9, 2010 at 2:26am
Thanks so much! But if I were to try to store it in a buffer how would I go about doing that? As of now when I run the program the only part that is stored in the buffer is the last thing I input. Is there anyway I could fix that?
Sep 9, 2010 at 2:29am
Indeed. Use vectors.

1
2
3
4
//Albatross's Standard Vector Snippets
vector<string> vectorname; //Creates a new vector that stores strings.
vectorname.push_back(string somethingtoadd); //Adds a copy of a string onto the end of a vector.
vectorname.at(int i); //Accesses a stored string at location i. 


You'll probably want use some combination of these snippets within your loop(s). :)

-Albatross
Topic archived. No new replies allowed.