I did all of them but idk why my get product count fails the test This is what i
ID: 3712159 • Letter: I
Question
I did all of them but idk why my get product count fails the test
This is what i have code is in c++
//getLine.h
// DO NOT EDIT THIS FILE
#ifndef GetLine_h
#define GetLine_h
#include <iostream>
#include <string>
using namespace std;
bool GetLine(istream& stream, string& text);
bool GetLine(istream& stream, string& text, const string& delimiter);
#endif
//getLine.cpp
// DO NOT EDIT THIS FILE
#include <iostream>
#include <string>
#include "GetLine.h"
using namespace std;
bool GetLine(istream& stream, string& text)
int c;
int p;
bool success;
text.erase();
success = false;
while (true)
{
c = stream.get();
if (stream.good())
{
success = true;
if (c == ' ')
{
p = stream.peek();
if (p == ' ')
{
stream.ignore();
}
break;
}
else if (c == ' ')
{
p = stream.peek();
if (p == ' ')
{
stream.ignore();
}
break;
}
else
{
text += c;
}
}
else
{
break;
}
}
if (success)
{
stream.clear();
}
return(success);
}
bool GetLine(istream& stream, string& text, const string& delimiter)
{
const uint32_t initialMask = 0x80000000;
const uint32_t columnMask = 0x1F;
const uint32_t rowMask = 0x07;
const uint32_t rowShift = 5;
char c;
uint32_t flag[8];
uint32_t i;
uint32_t mask;
uint32_t row;
uint32_t shift;
bool success;
text.erase();
for (row = 0; row < (sizeof(flag) / sizeof(flag[0])); ++row)
{
flag[row] = 0;
}
for (i = 0; i < delimiter.size(); ++i)
{
c = delimiter[i];
row = (c >> rowShift) & rowMask;
shift = c & columnMask;
mask = initialMask >> shift;
flag[row] |= mask;
}
success = false;
while (true)
{
stream.get(c);
if (stream.good())
{
success = true;
row = (c >> rowShift) & rowMask;
shift = c & columnMask;
mask = initialMask >> shift;
if ((flag[row] & mask) == 0)
{
text += c;
}
else
{
break;
}
}
else
{
break;
}
}
if (success)
{
stream.clear();
}
return(success);
}
//catalog.h
#ifndef Catalog_h
#define Catalog_h
#include <iostream>
#include <string>
#include <map>
#include<list>
#include"GetLine.h"
#include <stdexcept>
using namespace std;
class Catalog
{
public:
//Given a category number and name, add it to the catalog. It will have an empty product list.
//Return false if the category number already exists in the catalog, true otherwise.
bool AddCategory(uint64_t categoryNumber, const string& name);
//Return the number of categories in the catalog
uint64_t GetCategoryCount();
//Given a category number, a product number, and a product name, add the product to the catalog.
//Return false if the category number doesn?t exist in the catalog or if the product number already exists within the category, true otherwise.
bool AddProduct(uint64_t categoryNumber, uint64_t productNumber, const string& name);
//Given a category number, return the number of products in the category; return -1 if the category doesn?t exist.
int64_t GetProductCount(uint64_t categoryNumber);
private:
int categoryCount = 0;
int productCount = 0;
std::map<uint64_t, string> catalogMap;
std::map<uint64_t, string>::iterator it;
};
#endif
//catalog.cpp
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include "Catalog.h"
#include "GetLine.h"
using namespace std;
bool Catalog::AddCategory(uint64_t categoryNumber, const string& name)
{
if (catalogMap.find(categoryNumber) != catalogMap.end())
return false;
catalogMap.insert(std::pair<uint64_t, string>(categoryNumber, name));
categoryCount++;
return true;
}
bool Catalog::AddProduct(uint64_t categoryNumber, uint64_t productNumber, const string& name)
{
if (catalogMap.find(categoryNumber) != catalogMap.end())
return false;
catalogMap.insert(std::pair<uint64_t, string>(productNumber, name));
productCount++;
return true;
}
uint64_t Catalog::GetCategoryCount()
{
//to be completed
return categoryCount;
//return 0;
}
int64_t Catalog::GetProductCount(uint64_t categoryNumber)
{
int productCount = 0;
if (catalogMap.find(categoryNumber) == catalogMap.end())
return -1;
for (std::map<uint64_t, string>::iterator it = catalogMap.begin(); it != catalogMap.end(); it++)
{
productCount++;
}
productCount = (catalogMap.count(categoryNumber) > 0);
return productCount;
}
Explanation / Answer
As it seems, the issue might be where you return - 1 after checking if the categoryNumber is valid or not.
if (catalogMap.find(categoryNumber) == catalogMap.end())
return -1;
Here the condition should be '=='. I think you need to change it to '!=', because if the catergory no is not present in catalogMap it should return -1 while as per your code it will do the opposite. So i think the correction in your GetProductCount() will be
if (catalogMap.find(categoryNumber) != catalogMap.end())
return -1;
Rest seems to be ok. Hope this helps!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.