First off, I am very new to C++ and have worked primarily with languages such as Java, JavaScript, C#, and Scala.
I am trying to write a program that will take a number from the console, which will determine the number of teams (in form of linked lists (Ex. numTeams = 5; means 5 linked lists)) and then will take the following input such that the first value will be added to the first linked list, then the second value to the second linked list, and so on until all i values are distributed to all all n teams. Also, when all linked lists are filled with with a an equal amount of values it will start again at the first linked list and go through them again adding the values to each list.
I know I have to make an array of linked lists, but am unsure how to go further than where I am with distributing the values in the fashion I stated.
Any help would be very much appreciated!
Here is what I have so far:
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
class balanceTeams{
// Struct inside the class LinkedList
struct Node {
int x;
Node *next;
};
public:
// constructor
balanceTeams(){
head = NULL; // set head to NULL
}
// New value at the beginning of the list
void addUnit(int val){
Node *n = new Node(); // create new Node
n->x = val; // set value
n->next = head; // make the node point to the next node.
// If the list is empty, this is NULL
head = n; // head point at the new node.
}
// returns the first element in the list and deletes the Node.
int popUnit(){
Node *n = head;
int ret = n->x;
head = head->next;
delete n;
return ret;
}
//void swapUnit()
//void moveUnit()
private:
Node *head; // pointer to the first Node
};
int main() {
string line;
int numTeams = 0;
int lines = 0;
vector<balanceTeams> vect;
//int team_size = lines / numTeams;
getline(cin, line);
numTeams = atoi (line.c_str());
vect.resize(numTeams);
cout << "Num teams: " << numTeams << endl;
while (getline(cin, line)) {
cout << "Unit " << line << endl;
}
return 0;
}
Edit: Three Errors:
balanceTeams.cpp: In function ‘int main()’:
balanceTeams.cpp:72:23: error: no matching function for call to
‘balanceTeams::addUnit(std::string&)’
vect[i].addUnit(line);
^
balanceTeams.cpp:72:23: note: candidate is:
balanceTeams.cpp:25:7: note: void balanceTeams::addUnit(int)
void addUnit(int val){
^
balanceTeams.cpp:25:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘int’