Sign in to follow this  

C++ Functions & The STL

This topic is 4217 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I created a little program that gets input and shows the output and everything using vectors and algorithms, but I have come across a problem. When you put in all your input and it shows the output, only one of the bands the user inputs is showed. For Example, I tested it, and entered 2 for how manyt bands are my favorite, i then enter them, but when it distributes the output atr the end, only one of the bands i keyed in shows up. Source Code:
/*---------------------------------------------------------
|
|  Brandon Wall
|
|  7/1/06
|
|  Using Functions along with the STL Member Functions.
|
|--------------------------------------------------------*/

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string first_name(string question);
string last_name();

int counter = 0;

int main()
{
	int numBands;
	int numGames;

	string fname = first_name("What is your First Name? ");
	string lname = last_name();

	string whole_name = fname + " " + lname;

	system("cls");

	cout << fname << ", please enter the number of your favorite bands: ";
	cin >> numBands;

	cout << "Thank you, now enter in the number of your favorite video games: ";
	cin >> numGames;

	vector<string> favBands(numBands);
	vector<string> favGames(numGames);
	vector<string> allItems(4);

	vector<string>::iterator iter;
	vector<string>::const_iterator cIter;

	system("cls");

	cout << "Please Enter in your " << numBands << " favorite bands:\n\n";
	for(cIter = favBands.begin(); cIter != favBands.end(); cIter++)
	{
		cout << counter+1 << ": ";
		cin.ignore();
		getline(cin, favBands[counter]);
		cout << "\n";
		counter++;
	}

	counter = 0;
	cout << "\n\nPlease Enter in your " << numGames << " favorite games:\n\n";
	for(cIter = favGames.begin(); cIter != favGames.end(); cIter++)
	{
		cout << counter+1 << ": ";
		cin.ignore();
		getline(cin,favGames[counter]);
		cout << "\n";
		counter++;
	}

	system("cls");

	favBands.erase(favBands.begin() + 1);
	
	cout << whole_name << ", Here is a list of your " << numBands << " Favorite Bands:\n\n";

	counter = 0;
	for(cIter = favBands.begin(); cIter != favBands.end(); cIter++)
	{
		++counter;
		cout << counter << ": " << *cIter << " - " << cIter->size() << " Letters." << endl;
	}

	cout << "\nAlso, here is a list of your " << numGames << " Favorite Games:\n"
		 << "I added one, I know you like it.\n\n";
	
	favGames.reserve(numGames + 1);
	string addGame = "Half Life 2";
	favGames.insert(favGames.begin(), addGame);

	counter = 0;
	for(cIter = favGames.begin(); cIter != favGames.end(); cIter++)
	{
		++counter;
		cout << counter << ": " << *cIter << " - " << cIter->size() << " Letters." << endl;
	}

	merge(favBands.begin(), favBands.end(), favGames.begin(), favGames.end(), allItems.begin());
	random_shuffle(allItems.begin(),allItems.end());

	counter = 0;

	cout << "\nBoth Lists Merged into one!:\n\n";
	for(cIter = allItems.begin(); cIter != allItems.end(); cIter++)
	{
		++counter;
		cout << counter << ": " << *cIter << " - " << cIter->size() << " Letters." << endl;
	}

	allItems.clear();

	cout << "\nNow that i think of it, i dont think you really like this crap!\n\n";
	if(allItems.empty())
		cout << "You have NO Favorite Bands or Games!\n";
	else
	{
		counter = 0;
		for(cIter = allItems.begin(); cIter != allItems.end(); cIter++)
		{
			++counter;
			cout << counter << ": " << *cIter << " - " << cIter->length() << endl;
		}
	}
	

	cout << "\n";
	system("pause");

	return 0;
}

string first_name(string question)
{
	string fName;

	cout << "Please Enter your First Name: ";
	cin >> fName;

	return fName;
}

string last_name()
{
	string lName;

	system("cls");
	cout << "Please Enter your Last Name: ";
	cin >> lName;

	return lName;
}

Share this post


Link to post
Share on other sites
Coul dit be this line, just before you list:

favBands.erase(favBands.begin() + 1);


Share this post


Link to post
Share on other sites
Quote:
Original post by Trip99
Coul dit be this line, just before you list:

favBands.erase(favBands.begin() + 1);


lol, i forgot i had this line in there. [sad]

Share this post


Link to post
Share on other sites
Not related to your problem, but why pass a string into first_name() if it is ignored?

Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Quote:
Original post by Trip99
Coul dit be this line, just before you list:

favBands.erase(favBands.begin() + 1);


lol, i forgot i had this line in there. [sad]


just one more problem, when i get the input from the user, the games they input, the firstr letter of each word is cut off. Ive had this problem before, but cannot figure out how to fix this.

Share this post


Link to post
Share on other sites
I think it's because you're ignoring the first character they enter. Getline will grab and ignore the return when the user pushes enter, so you don't need to flush it from the buffer, I believe...

Share this post


Link to post
Share on other sites
Quote:
Original post by Vopisk
I think it's because you're ignoring the first character they enter. Getline will grab and ignore the return when the user pushes enter, so you don't need to flush it from the buffer, I believe...


I tried taking off getline, but when i do, it skips the users first choice on their inputing of their favorite band.

Share this post


Link to post
Share on other sites
Try std::cin >> std::ws;. It only eats whitespace.

Or do a std::cin.ignore(std::numeric_limits<int>::max(), '\n'); to empty the input buffer up to and including the newline.

Either way, taking them out of the for-loop may be a good idea.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Try std::cin >> std::ws;. It only eats whitespace.

Or do a std::cin.ignore(std::numeric_limits<int>::max(), '\n'); to empty the input buffer up to and including the newline.

Either way, taking them out of the for-loop may be a good idea.


Thank you very much, it worked perfectly. BTW, whats the name of this line of code "std::cin >> std::ws;" ?

Share this post


Link to post
Share on other sites
Quote:
Original post by NUCLEAR RABBIT
Thank you very much, it worked perfectly. BTW, whats the name of this line of code "std::cin >> std::ws;" ?


std::ws is a stream manipulator just like std::endl, std::hex, std::setw, std::setprecision, etc. Some are directly available, some are in the <iomanip> header.

std::ws consumes any leading whitespace in the stream.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by NUCLEAR RABBIT
Thank you very much, it worked perfectly. BTW, whats the name of this line of code "std::cin >> std::ws;" ?


std::ws is a stream manipulator just like std::endl, std::hex, std::setw, std::setprecision, etc. Some are directly available, some are in the <iomanip> header.

std::ws consumes any leading whitespace in the stream.


Roger that. Thanks for the help. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this