Help, problem with struct and vector!

Pages: 12
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:)

Here is my unfinished code:

#include <iostream>
#include <vector>
using namespace std;
void input(vector<name>&v);
void print(const vector<name> &);

struct name
{
string first_name;
string last_name;
string town;
};

int main()
{
name list;
cout<<"Please type in first name, last name and your hometown: ";
input(list);
cout<<"Your list:"<<endl;
print(list);

return 0;
}

void input(vector<name> &v)
{
cin>>v.first_name >> v.last_name >> v.town;
}

void print(const vector<name> &v)
{
cout<<"Startlist: "<<endl;
coutt<<"Startnumber Firstname Secondname Town"<<endl;
cout<<v.first_name<<" "<<v.last_name<< " "<<v.town<<endl<<endl;
}
So what is wrong with the code you have now?
It doesn't work:) I can't get it to work.
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.
1
2
3
4
5
6
void input(vector<name> &v)
{
    name newName;
    cin >> newName.first_name >> newName.last_name >> newName.town;
    v.push_back(newName);
}


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.
Last edited on
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);
The struct definition for name should be done before using it as an argument.
Ok thanks:) The code looks like this now but i still get 3 errors when compiling:(

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

struct name
{
string first_name;
string last_name;
string town;
};

void input(vector<name>&v);
void print(const vector<name> &);

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);

return 0;
}

void input(vector<name> &v)
{
name newName;
cin>>newName.first_name >> newName.last_name >> newName.town;
v.push_back(newName);
}

void print(const vector<name> &v)
{
cout<<"Startlist: "<<endl;
cout<<"Startnumber Firstname Secondname Town"<<endl;
cout<<v.first_name<<" "<<v.last_name<< " "<<v.town<<endl<<endl;
}
well yes, you have to fix your print function to print each name that is contained in your vector. Use a for loop...
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;
};

void input(vector<name>&v);
void print(const vector<name> &);

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);

return 0;
}

void input(vector<name> &v)
{
name newName;
cin>>newName.first_name >> newName.last_name >> newName.town;
v.push_back(newName);
}

void print(const vector<name> &v)
{
cout<<"Startlist: "<<endl;
cout<<"Startnumber Firstname Secondname Town"<<endl;
for(int i=0; i<v.size(); ++i){
cout<<v.first_name<<" "<<v.last_name<< " "<<v.town<<endl<<endl;
}

}

It can't be much left? I have no idea why it isn't working:)
Change this:
1
2
3
for(int i=0; i<v.size(); ++i){
cout<<v.first_name<<" "<<v.last_name<< " "<<v.town<<endl<<endl;
}
to
1
2
3
for(int i=0; i<v.size(); ++i){
cout<<v[i].first_name<<" "<<v[i].last_name<< " "<<v[i].town<<endl<<endl;
}
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! :)
Last edited on
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 :)
Read this for random:

http://www.cplusplus.com/reference/cstdlib/rand/


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
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:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

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

struct name
{
  string first_name;
  string last_name;
  string town;
};

void input(vector<name>&v);
void print(const vector<name> &);

int main()
{
  
  vector<name>v;
  cout<<"Please type in first name, last name and your hometown: ";
  input(v);
  cout<<"Your list:"<<endl;
    print(v);

  return 0;
}

void input(vector<name> &v)
{
  name newName;
  cin>>newName.first_name >> newName.last_name >> newName.town;
  v.push_back(newName);
}

void print(const vector<name> &v)
{
  cout<<"Startlist: "<<endl;
  cout<<"Startnumber    Firstname      Secondname        Town"<<endl;
  for(int i=0; i<v.size(); ++i){
  cout<<v[i].first_name<<" "<<v[i].last_name<< " "<<v[i].town<<endl<<endl;
  }

}

Last edited on
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.
In that case, use std::random_shuffle from algorithm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
//...

int main() {
    // ...
    input(v);
    std::random_shuffle(v.begin(), v.end());
    std::cout << "Your list: " << std::endl;
    print(v);

    //...
}

//... 


See http://www.cplusplus.com/reference/algorithm/random_shuffle/
Last edited on
@ NT3: don't forget that std::random_shuffle() uses std::rand() by default so somewhere we should call:

std::srand(std::time(NULL));

Edit: checked the documentation, it usually does but it's implementation-defined.
Last edited on
@Catfish666:
When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness.


In other words, not necessarily. Then again, you are right, it is probably best to seed the generator, for maximum portability.
Last edited on
Pages: 12