I am wanting to make a list of names and other information. Then print them all out at the end. I am working with this code but can't seem to manipulate it the right name.
What I am looking to do is something like...
Add(A) for adding information or List(L) for listing all the input.
Enter Name: (get the input)
Enter Place of birth: (get the input)
Year of birth: (get the input)
and repeat until I say for it to. So I can input A, and then do it again. Once I decide that I have put enough information in, I want to press, L...and list all the people's information.
So I think I need an array and vector...but I could be wrong.
What you have is OK - vectors are better than arrays. Here's a slight modification. Now what you have to consider is the data structure of what you are planning to store. A class or struct is ideal. Your (single) vector would store Student objects rather than just marks alone. ( vector<Student> not just vector<double> )
#include <cctype> // <--- Added.
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
vector<double> student_marks;
double mark;
char answer;
cout << "\n Enter marks (only good for 1 student.)? \n"; // <--- Changed.
//cin >> answer;
std::cout << '\n';
//while (std::tolower(answer) == 'y')
//{
// cout << "\nEnter value: ";
// cin >> mark;
// student_marks.push_back(mark);
// cout << "More marks (y/n)? "; // <--- Changed.
// cin >> answer;
//}
while (cout << " Enter value (-9 to quit): " && cin >> mark && mark != -9)
{
student_marks.push_back(mark);
}
std::cout << "\n ";
for (size_t idx = 0; idx < student_marks.size(); idx++)
{
std::cout << student_marks[idx] << (idx < student_marks.size() - 1 ? ", " : "\n");
}
return 0; // <--- Not required, but makes a good break point.
}
You could use parallel vectors for each piece of information, but the struct would be a better choice and not that hard to change the above code to deal with it.
It looks like you would want to start with a menu to "add" or "list", do what you need and return to the menu to either add more or list what you have.
You would also need a loop for "main", I am thinking a do/while loop to keep repeating until you choose to quit the program.
An array is a very simple, low level thing. It is little more than a fixed-sized block of memory -- you can't assign/copy/resize/more to them. Most programmers would say to use a vector and keep arrays out of your code most of the time. A vector is an object/type that is a 'better array' really. It can do those things I said and more -- resize, assign, copy, etc are all trivial because the class hides the loops and low level details.
If you can use structs/classes, the right thing to do is to make a vector of those, which is what againtry tried to say.
If you cannot yet use those, you need another approach -- parallel arrays is mentioned.
at a KISS level:
int x[10];
vector<int> x(10);
will do the same thing. x can do MORE, but right off the bat vec x is pretty much an array with 10 locations and ready to do anything array x could.
I didnt say anything new here, just trying to condense the points above so you can understand what you were told. If it isnt helping, say why -- not sure what you know and do or do not understand...
Ok, what about this? Now I am running into this issue...when I first run it, it kind of smudges it together. And I don't want a set number. I want as many as I want until I say list. So not just a set number of 3 like I currently have.