I want to know how to add values to vectors using only user input. Any help?
I have three vectors relating to fish: The type, the size, and how many they want. I want the user to be able to enter these values and then the program to be able to repeat that information in a nice table format.
These are my vectors:
For starters, how will the program know when the user is done entering all their data? If they tell you before they enter their data, you would use a for loop. If they tell you after they've entered their data by e.g. giving invalid data, you would use a while loop.
Show what you've got so far and then explain a specific thing you're stuck on.
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
usingnamespace std;
int main()
{
cout<< "Shelby Teele \n"
<<"C++ Tropical Fish Store\n"
<<"This program will help you determine the tank\n"
<<"size for your tropical fish.\n";
vector <string> fishType;
vector <int> fishSize;
vector <int> fishNum;
while()
{[code]
fishSize.push_back();
fishNum.push_back();
cout<< "What kind of fish would you like in your tank? \n";
cout<< "How long are they? \n";
cin >> fishSize;
cout<< "How many of them would you like? \n";
}
}[/code]
I left some things blank because I wasn't sure what should go there. The program isn't even close to done yet.
Ok, thanks. And, yes, I accidentally pushed the source code format button twice and it did that. As soon as I'm done with the input, I'll post it here.
Oh, yeah. when I put in the cin chevrons, it comes up with the red squiggly line and says there is no operator that matches those operands. That's never happened before, and the cout chevrons are fine.
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
usingnamespace std;
int main()
{
cout<< "Shelby Teele \n"
<<"C++ Tropical Fish Store\n"
<<"This program will help you determine the tank\n"
<<"size for your tropical fish.\n";
vector <string> fishType;
vector <int> fishSize;
vector <int> fishNum;
while()
{
string answer;
cout<< "What kind of fish would you like in your tank? \n";
cin>> fishType;
cout<< "How long are they? \n";
cin >> fishSize;
cout<< "How many of them would you like? \n";
cin>> fishNum;
cout<< "Would you like to add another type of fish? yes/no \n";
cin>> answer;
if(answer == "yes");
{
cout<< "What kind of fish would you like in your tank? \n";
cin>> fishType;
cout<< "How long are they? \n";
cin >> fishSize;
cout<< "How many of them would you like? \n";
cin>> fishNum;
cout<< "Would you like to add another type of fish? yes/no \n";
cin>> answer;
}
else
{
}
fishSize.push_back();
fishNum.push_back();
}
You need separate variables to cin into, and then you'll push_back those variables into the vector. That's why I mentioned that the input part happened first and the push_back part happened second.