0

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’
Jaromando
  • 309
  • 2
  • 3
  • 9
  • 2
    It might be helpful if you're new to c++ [to read a book first](http://stackoverflow.com/a/388282/332733) c++ is a bit complicated – Mgetz Mar 18 '15 at 23:28
  • You are trying to tackle two problems at once: distributing values to elements in an array, and adding values to a linked list. Which one do you need help with? – Beta Mar 18 '15 at 23:33
  • Probably distributing values to elements in an array more so – Jaromando Mar 18 '15 at 23:36
  • Please format your code so that it's easy to read for the rest of us. Particularly, please fix indentation. – pts Mar 18 '15 at 23:42
  • What is the desired effect of `line.substr(0, line.find(' '));`? Currently it looks line a no-op, because it doesn't change `line`. – pts Mar 18 '15 at 23:43
  • Always check the success all input-reading and parsing function calls. (In this case, `atoi` and `getline`.) – pts Mar 18 '15 at 23:43
  • Was looking to parse, but ended up not really using it, I removed it. – Jaromando Mar 18 '15 at 23:45
  • You are changing the value of `numTeams` after `balanceTeams list[numTeams];`. Please not that such a change doesn't unpedate the size of `list`. Use `std::vector` instead of an array, and call its `.resize` method whenever the size needs to be changed. – pts Mar 18 '15 at 23:45
  • Changed it to a vector. – Jaromando Mar 18 '15 at 23:56

1 Answers1

0

All that seems to be missing from your code is:

1) Some error checking. The number of entered teams must be at least one. If someone enters 0 or a negative value, display an error message, and bail out.

2) Initialize

size_t i=0;

before the existing loop.

3) Inside the loop, parse the existing value, then call

vect[i].addUnit(new_value);

4) Increment the index at the end of the loop, for the next iterator

i=(i+1) % vect.size();

That seems to be it. Each time around the loop, you'll be adding a value to the next vector.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • I'm getting three errors when trying to implement that. I'll include in my original post so you can see it better. – Jaromando Mar 19 '15 at 02:39
  • The compilation error is self-explanatory. You declared your class method as taking an int for an argument, while you're passing a std::string as a parameter. You need to make up your mind whether your link list stores ints, or std::strings, and then change the code accordingly. That's exactly what the compiler error says. What part of the error text you didn't understand? – Sam Varshavchik Mar 19 '15 at 23:45