Help, problem with struct and vector!

Pages: 12
Ok thanks a lot:) But the thing is that i can't check if it works because as i said in an earlier comment i can only type in one name, second name and town. When i press enter to enter the next name etc, the program closes and gives me a list with only one row. It is supposed to end when hitting control-D.
closed account (j3Rz8vqX)
Interpreting the last several posts:
Quoted from NT3:
1
2
3
4
    for (int i = 0; i < iterations; ++i) {
        cout << "Please type in first name, last name and your hometown: ";
        input(v);
    }

Assign iteration a value equal to the number of inputs you would like to enter.

Quote from NT3:
1
2
3
4
//In that case, use std::random_shuffle from algorithm:
    std::random_shuffle(v.begin(), v.end());
    std::cout << "Your list: " << std::endl;
    print(v);

Use the above logic to shuffle your vector before outputting it.

Quote from Catfish666:
1
2
//so somewhere we should call:
std::srand(std::time(NULL));

Ensure to declare this before std::random_shuffle, though it may not be necessary.

Possibly something like this (credit to the above appropriate programmers):
1
2
3
4
5
6
7
8
    std::srand(std::time(NULL));
    for (int i = 0; i < iterations; ++i) {
        cout << "Please type in first name, last name and your hometown: ";
        input(v);
    }
    std::random_shuffle(v.begin(), v.end());
    std::cout << "Your list: " << std::endl;
    print(v);

I believe srand requires the cstdlib.
 
#incldue <cstdlib> 

As for your escape sequence, possibly parse your inputs against "Ctrl -D"?
Ctrl-D is character 'EOT'.

When should the user be able to exit data entry process? (first, last, town, all, or possibly during the control loop)

Maybe a do-while loop instead of a for-loop if you want a user control loop?
1
2
3
4
5
6
7
char choice;
do
{
    //process code here;
    //may need a cin.get() here if cin is flagged.
    choice=cin.get();
}while(choice!='EOT');

Possible end result:
1
2
3
4
5
6
7
8
9
10
11
12
    char choice;
    std::srand(std::time(NULL));
    do
    {
        cout << "Please type in first name, last name and your hometown: ";
        input(v);
        //cin.get(); //may need to clear buffer in cin
        choice=cin.get();
    }while(choice!='EOT');
    std::random_shuffle(v.begin(), v.end());
    std::cout << "Your list: " << std::endl;
    print(v);

There are lots of design possibility.

References to Ctrl-D ASCII:
http://www.asciitable.com/
http://www.unix-manuals.com/refs/misc/ascii-table.html

Reference to cin.get():
http://www.cplusplus.com/reference/istream/istream/get/

Note: I have not tested any of the above code, again credits to NT3 and Catfish666.
Last edited on
Hmm, i can't get the random generator to work, anyone tried it? I can still only put one name in and then it closes the program. I want to be able to enter like 20 names or so:)

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
46
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
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);
  random_shuffle(v.begin(), v.end());
  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<<(i+1)<<setw(20)<<v[i].first_name<<setw(9)<<" "<<v[i].last_name<<setw(8)<< " "<<v[i].town<<endl<<endl;
  }

}

Last edited on
> I can still only put one name in and then it closes the program.
> I want to be able to enter like 20 names or so:)

Put this in a loop:
1
2
  cout<<"Please type in first name, last name and your hometown: ";
  input(v);
Ok i get it quite to work :) But i don't really know how many i should type in. What should i use instead of i<list?

1
2
3
4
int list=20;
  vector<name>v;
  for(int i=0; i<list; ++i){


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int main()
{
  
  int list=20;
  vector<name>v;
  for(int i=0; i<list; ++i){
  cout<<"Please type in first name, last name and your hometown: ";
  input(v);
  }
  random_shuffle(v.begin(), v.end());
  cout<<"Your list:"<<endl;
    print(v);

  return 0;
}
Last edited on
> What should i use instead of i<list?

1
2
3
4
5
6
7
8
9
10
11
12
13
// returns false on eof
// (for eof, type in CTRL+D on Unix or Unix clones, CTRL+Z on Windows)
// returns true after adding element to vector otherwise
bool input( vector<name> &v )
{
    name newName;
    if( cin>>newName.first_name >> newName.last_name >> newName.town )
    {
        v.push_back(newName);
        return true ; 
    }
    return false ;
}


And then, in main(), instead of the for loop:

1
2
3
4
vector<name>v;

do cout<<"Please type in first name, last name and your hometown: ";
while( input(v) ) ;

Oh thanks!:D Works great now!
But it still won't give me different lists on cout:D

The program should generate different start lists for each cout, and even with the same participants.

hmm anyone? Otherwise it works great!
You need to seed the random number generator at the beginning of main(), as shown above:
std::srand(std::time(NULL));.

Otherwise, the random number generator will initialize the same exact way every time and produce the same sequence.
> The program should generate different start lists for each cout, and even with the same participants.

http://www.cplusplus.com/forum/beginner/119550/#msg656957
http://www.cplusplus.com/forum/beginner/119550/#msg656959
Topic archived. No new replies allowed.
Pages: 12