Thread: Problem Creating Functions

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Problem Creating Functions

    Hello,

    I have the following code all written and working properly:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <list>
    #include <queue>
    using namespace std;
    
    void updateRun();
    void updateReady();
    void updateWait();
    
    int main () {
      ifstream infile("in_file.txt");
      ifstream event("events.txt");
      
      list<string> vec;
      list<string> tempPro;
      
      queue<string> run;
      queue<string> ready;
      queue<string> wait;
      
      if (!infile || !event) {
        cerr << "error: unable to open input file!\n";
        return -1;
      }
    
      string word;
      while (infile >> word) {
          vec.push_back(word);
          string tempEl;
          if (word == "running") {
                tempEl = tempPro.front();
                tempPro.pop_front();
                run.push(tempEl);
          }
          else if (word == "ready") {
                tempEl = tempPro.front();
                tempPro.pop_front();
                ready.push(tempEl);
          }
          else if (word == "waiting") {
                tempEl = tempPro.front();
                tempPro.pop_front();
                wait.push(tempEl);
          }
          else tempPro.push_front(word);
        
          if (word == "running" || word == "ready" || word == "waiting") {  
              int runSize = run.size();
              int readySize = ready.size();
              int waitSize = wait.size();
          
              cout << "Running: ";
              while (runSize != 0) {
                  string running = run.front();
                  cout << running << " ";
                  run.pop();
                  run.push(running);
                  runSize--;
              }
              cout << endl;
              cout << "Ready: ";
              while (readySize != 0) {
                  string readyToGo = ready.front();
                  cout << readyToGo << " ";
                  ready.pop();
                  ready.push(readyToGo);
                  readySize--;
              }
              cout << endl;
              cout << "Waiting: ";
              while (waitSize != 0) {
                  string waiting = wait.front();
                  cout << waiting << " ";
                  wait.pop();
                  wait.push(waiting);
                  waitSize--;
              }
              cout << endl;
              cout << endl;
          }
      }
      
      string word2;
      int eventNum = 1;
      
      while (event >> word2) {
        if (word2 == "1") {
            string elem1 = run.front();
            if (ready.size() != 0 ) {
                    string toRun1 = ready.front();
                    ready.pop();
                    run.push(toRun1);
            }
        }
        else if (word2 == "2") {
            string elem2 = run.front();
            run.pop();
            wait.push(elem2);
            if (ready.size() != 0) {
                    string toRun2 = ready.front();
                    ready.pop();
                    run.push(toRun2);
            }
            else {
                    string toRun2 = wait.front();
                    wait.pop();
                    run.push(toRun2);
            }
        }
        else if (word2 == "3") {
            string elem3 = wait.front();
            wait.pop();
            ready.push(elem3);
            if (ready.size() == 0 || run.size() == 1) {
                    string toRun3 = ready.front();
                    ready.pop();
                    run.pop();
                    run.push(toRun3);
            }
        }
        
        int runSize = run.size();
        int readySize = ready.size();
        int waitSize = wait.size();
      
        cout << endl;
        cout << "Event #" << eventNum << endl;
        cout << "Running: ";
        while (runSize != 0) {
            string running = run.front();
            cout << running << " ";
            run.pop();
            run.push(running);
            runSize--;
        }
        cout << endl;
        cout << "Ready: ";
        while (readySize != 0) {
            string readyToGo = ready.front();
            cout << readyToGo << " ";
            ready.pop();
            ready.push(readyToGo);
            readySize--;
        }
        cout << endl;
        cout << "Waiting: ";
        while (waitSize != 0) {
            string waiting = wait.front();
            cout << waiting << " ";
            wait.pop();
            wait.push(waiting);
            waitSize--;
        }
        cout << endl;
        eventNum++;
      }
      return 0;
    }
    What I want to do is to take the parts of the code in bold and make them into their own functions. I tried declaring the queue objects outside of the main function, but that gave me a compiler error. Can someone show me the code to make the functions out of those lines of code in bold?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Is there a reason you can't just make 3 functions of type void, put your code in there, and call those functions in place of your bold code?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    I can't do that because it gives me an error saying that the queues that are used in that code are undeclared. For example, in the first bolded code, it says the queues ready and run are undeclared.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Thats because of the scope of ready and run.

    They are declared within the main function so only the main function has direct access to them.

    You could either make it so you can pass the queues into a function.. or make them global.
    What is C++?

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    How would I go about making them global? Just declare them outside of the main function?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How would I go about making them global?
    Start by exhausting all other options, such as passing them as arguments.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok, how would I write a function that takes them as arguments? I'm getting an error when I do it the way I think I'm supposed to do it. Sorry about all the questions but I'm new to C++.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    void f ( queue<string>& q )
    {
      // Use q
    }
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Thanks, it works now! I wasn't putting the data type that the queue would hold in my arguments. One more question, this is just for my general knowledge, but what is the purpose of those ampersands in the arugments? Thanks.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the purpose of those ampersands in the arugments?
    It's a reference, it means that instead of passing a copy (an operation that the queue container adapter is not technically capable of AFAIK), you pass a kind of synonym for the actual object. That way any changes you make in the function will remain when you return to the caller. For example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void f1 ( int i )
    {
      ++i;
    }
    
    void f2 ( int& i )
    {
      ++i;
    }
    
    int main()
    {
      int i = 0;
      int j = 0;
    
      f1 ( i );
      f2 ( j );
    
      cout<< i <<endl; // Prints 0
      cout<< j <<endl; // Prints 1
    }
    f1 passes by value, so a copy is made and the change does not take effect in main. f2 passes by reference, so the original object is used and the change is seen back in main.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about Creating structure
    By albert3721 in forum C Programming
    Replies: 3
    Last Post: 06-05-2007, 07:33 PM
  2. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  3. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  4. Creating menu, problem
    By Tehy in forum C Programming
    Replies: 2
    Last Post: 04-13-2007, 05:19 AM
  5. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM