Hello! I am trying to make a simple notification system. I want to type in first name, second name and your hometown club for like 5 to 10 different people. The program should store information as struct and put them in a vector. When i have typed in the whole list i want the program to change places on some people, just a random function and then send it to cout. I am trying to solve this using subprograms.
I would really appreciate answers, sorry for the bad english:)
Well first of all, you can't declare functions to use a type which you haven't yet defined. Your two function declarations at the top of the file have to go below the declaration of struct name.
Second, your functions are expecting an argument of a vector of names, but you are trying to pass them a single name structure. Your main should declare a vector of name objects: vector<name> list;.
Your input function needs to be adding a single name structure to the list.
Your print function needs to access individual names from your vector. Look up the documentation for vectors (and loops if you're not familiar with them already) and give it a try first.
It's pointless to help you on other things until your code compiles properly.
Ok thanks a lot. I can't really figure out how the main function will look like though. I get this error when running it:
use of undeclared identifier 'name'
void input(vector<name>&v);
int main()
{
//name list;
vector<name>list;
cout<<"Please type in first name, last name and your hometown: ";
input(list);
cout<<"Your list:"<<endl;
print(list);
I still get 3 errors saying:
error: no member named 'first_name' in 'std::__1::vector<name,
std::__1::allocator<name> >'
cout<<v.first_name<<" "<<v.last_name<< " "<<v.town<<endl<<endl;
~ ^
And the other errors are the same for last_name and town.
Not sure if i did the foor loop right?
#include <iostream>
#include <vector>
using namespace std;
struct name
{
string first_name;
string last_name;
string town;
};
int main()
{
//name list;
vector<name>v;
cout<<"Please type in first name, last name and your hometown: ";
input(v);
cout<<"Your list:"<<endl;
print(v);
OH SHIT, it kinda works:) The problem is that it can only take one first name, last name and town. It is supposed to take a whole list of names and end input after Ctrl-D.
Thanks! :)
Does anyone know how to use math random or such to get the list random generated on cout after typing it in? I want the list to be random generated every time :)
Haha i really suck at c++ :) I have tried to understand how to do the random part but i have no idea. The rand function seems to just generate a random number but i want to generate a random list?:) I know that i should do the rand function in the void print sub program but how? I would really appreciate answers, really stuck here:)
Do you mean that you want to enter a random number of inputs? You could do that by getting a random number and then repeating your input() statement the number of times that random is repeated, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <ctime>
//...
int main() {
vector<name> v;
srand(time(NULL)); // Set the numbers to based off current time
int iterations = (rand() % 10) + 1 // repeat 1-10 times
for (int i = 0; i < iterations; ++i) {
cout << "Please type in first name, last name and your hometown: ";
input(v);
}
//...
}
//...
No i mean that i want the list that gets generated after i input names and hometown etc is random on cout:) So the list won't be in the same order when i get it back.