Home > Software design >  C fixing queues and extra comma
C fixing queues and extra comma

Time:10-13

So I have this problem:

Input file: The first line of input will contain the list of books in queue 1. The second line will contain the list of books in queue 2. Each book will be separated by a comma. Book name can have spaces in between the string. There will be no duplicate book name. No empty input will be given.

Output file: The first line of output should display the total time. The second line should display the order of book after organizing, with each book separated by a comma.

If the input is:

chemistry,compsci A,bio A,english B

compsci B,compsci D,compsci C

The output should be:

170

compsci B,compsci A,compsci D,compsci C,chemistry,bio A,english B

But when I run my code, the output I get is this:

170

compsci A,compsci B,compsci D,compsci C,chemistry,bio A,english B,

I am not sure how to get rid of the extra comma at the end of the sentence and I think I am supposed to check queue 1 and 2 in sequence, queue1.front then queue2.front then back to queue1.front then queue2.front, but the way my code is its running queue1 first then moving on to queue2 which is why its putting it in the wrong order, but I am not sure how to rewrite the loops so it checks them in sequence.

My code:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include "ArgumentManager.h"

using namespace std;

queue<string> splitStringUsingComma(string text)
{
  queue<string> sol;
  stringstream ss(text);
  while(ss.good())
  {
    string x;
    getline(ss,x,',');
    sol.push(x);
  }
  return sol;
}

int main(int argc, char* argv[])
{
  ArgumentManager am(argc, argv);
  ifstream input;

  string infileName = am.get("input");
  string outfileName = am.get("output");
  input.open(infileName);

  string text;
  queue<string> queue1,queue2;
  getline(input,text);
  queue1 = splitStringUsingComma(text);
  text.clear();
  getline(input,text);
  queue2 = splitStringUsingComma(text);
  queue<string> queue3;
  int cost{0};
  vector<string> sol;

  while(!queue1.empty())
  {
    if(queue1.front().find("compsci")!=string::npos)
    {
      sol.push_back(queue1.front());
      cost =20;
    }
    else
    {
      queue3.push(queue1.front());
      cost =10;
    }
    queue1.pop();
  }

  while(!queue2.empty())
  {
    if(queue2.front().find("compsci")!=string::npos)
    {
      sol.push_back(queue2.front());
      cost =20;
    }
    else
    {
      queue3.push(queue2.front());
      cost =10;
    }
    queue2.pop();
  }

  while(!queue3.empty())
  {
    sol.push_back(queue3.front());
    queue3.pop();
    cost =20;
  }

  ofstream output;
  output.open(outfileName);
  output<<cost<<'\n';

  for(string x:sol)
  {
    output<<x<<",";
  }
  output<<endl;
  input.close();
  output.close();
  return 0;
}

The argument manager.h is only there so the code can run properly and can run on the server and be checked, it cannot be modified but I can include it in here just in case:

#include <map>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

class ArgumentManager {
private:
    map<string, string> m_argumentMap;
public:
    ArgumentManager() { }
    ArgumentManager(int argc, char *argv[], char delimiter=';');
    ArgumentManager(string rawArguments, char delimiter=';');
    void parse(int argc, char *argv[], char delimiter=';');
    void parse(string rawArguments, char delimiter=';');
    string get(string argumentName);
    string toString();
    friend ostream& operator << (ostream &out, ArgumentManager &am);
};

void ArgumentManager::parse(string rawArguments, char delimiter) {
    stringstream currentArgumentName;
    stringstream currentArgumentValue;
    bool argumentNameFinished = false;
    
    for (unsigned int i=0; i<=rawArguments.length(); i  ) {
        if (i == rawArguments.length() || rawArguments[i] == delimiter) {
            if (currentArgumentName.str() != "") {
                m_argumentMap[currentArgumentName.str()] = currentArgumentValue.str();
            }
            // reset
            currentArgumentName.str("");
            currentArgumentValue.str("");
            argumentNameFinished = false;
        }
        else if (rawArguments[i] == '=') {
            argumentNameFinished = true;
        }
        else {
            if (argumentNameFinished) {
                currentArgumentValue << rawArguments[i];
            }
            else {
                // ignore any spaces in argument names. 
                if (rawArguments[i] == ' ')
                    continue;
                currentArgumentName << rawArguments[i];
            }
        }
    }
}

void ArgumentManager::parse(int argc, char *argv[], char delimiter) {
    if (argc > 1) {
        for (int i=1; i<argc; i  ) {
            parse(argv[i], delimiter);
        }
    }
}

ArgumentManager::ArgumentManager(int argc, char *argv[], char delimiter) {
    parse(argc, argv, delimiter);
}

ArgumentManager::ArgumentManager(string rawArguments, char delimiter) {
    parse(rawArguments, delimiter);
}

string ArgumentManager::get(string argumentName) {
    map<string, string>::iterator iter = m_argumentMap.find(argumentName);

    //If the argument is not found, return a blank string.
    if (iter == m_argumentMap.end()) {
        return "";
    }
    else {
        return iter->second;
    }
}

string ArgumentManager::toString() {
    stringstream ss;
    for (map<string, string>::iterator iter = m_argumentMap.begin(); iter != m_argumentMap.end(); iter  ) {
        ss << "Argument name: " << iter->first << endl;
        ss << "Argument value: " << iter->second << endl;
    }
    return ss.str();
}

ostream& operator << (ostream &out, ArgumentManager &am) {
    out << am.toString();
    return out;
}

Also I know using namespace std is not proper but it makes it easier for someone like me who is still learning how to properly code and its the way my professor writes the code.

CodePudding user response:

Just use the following loop to output the individual string into the output file. See the working program here.

//   for(string x:sol)
//   {
//     output<<x<<",";
//   }
  for(int i = 0; i < sol.size() -1 ;   i)
  {
      
          output << sol.at(i)<<",";
     
      
  }
  output << sol.back();

The output of the above program is as follows:

170
compsci A,compsci B,compsci D,compsci C,chemistry,bio A,english B

Note there is no comma at the end as you wanted.

  • Related