I'm having a problem getting my buffer(string) to print out everything that the user inputs. When I run my code the buffer only prints out the last thing that is inputted. I've tried just about everything and I really do not know what else to do. I'm new to c++ so I have not got familiar with the language just yet. Any help would be greatly appreciated.

#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";
}
Ancient Dragon commented: Thanks for using code tags correctly on your very first post :) +33

It only prints the last name that was input because when cin >> name is called cin will erase the current contents if name and replace it with the new keystrokes. If you want to keep all the names in memory then you will have to put them in an array, preferably a std::vector.

std::vector<std::string> namelist;

...
cout << "Enter a name\n";
cin >> name;
namelist.push_back(name);

...
...
// now display all the names that were input
for(int i = 0; i < namelist.size(); i++)
{
   cout << namelist[i] << '\n';
}

Thanks but its still not working for some reason. I dont know what i'm doing wrong I placed it in my code but its still not repeating the whole user input at the end. Should I take my do/while loop out?

Post new code because I can't see what you have done.

Post new code because I can't see what you have done.

#include <iostream>
#include <string> 
#include <vector>
using namespace std;



int main()
{
	string name;
	string quote;
	string answer;
	
do
{
	std::vector<std::string> namelist;
	cout << "Enter a name\n";
	cin >> name;
	namelist.push_back(name);
	
	for(int i = 0; i < namelist.size(); i++)
	{
		cout << namelist[i] << '\n';
	}	
	
	std::vector<std::string> quotelist;
	cout << "Enter a quote\n";
	cin >> quote;
	quotelist.push_back(quote);
	
	for(int j = 0; j < quotelist.size(); j++)
	{
		cout << quotelist[j] << '\n';
	} while (answer == "yes");
	}

	
	if(answer == "no")
	{
		
		string buffer;
		buffer = "The buffer contains:\n ";
		cout<< buffer << name << ": " << quote;
		
	}

else
cout<< "Invalid input";
}

move the declaration of that vector up outside the do loop. Its contents are being trashed on each iteration of that do loop. You have to declare it outside the loop so that its contents are preserved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.