It is a homework problem. It compiles fine, but linker gives undefined reference to my function getSalesData - line 20 just after my first for loop. Here is the code. I cannot find where I have done anything incorrectly; I have my prototype defined and it matches my function header, and I am simply calling the function.
// chips and salsa
#include <iostream>
#include <iomanip>
#include <string>
int getJarsSold(std::string type);
void getSalesData(int jarsSold[],int size,int &totalJars,int &highSeller,int &lowSeller);
int main() {
const int SIZE = 5;
const std::string salsaTypes[] {"Mild","Medium","Sweet","Hot","Zesty"};
int jarsSold[SIZE] = {};
int highIndex,lowIndex,totalJarsSold;
for (int i = 0; i < SIZE; i++) {
jarsSold[i] = getJarsSold(salsaTypes[i]);
}
getSalesData(jarsSold,SIZE,totalJarsSold,highIndex,lowIndex);
std::cout << " Type Jars Sold\n";
std::cout << "---------------------\n";
for (int i = 0; i < SIZE; i++) {
std::cout << std::setw(8) << salsaTypes[i] << std::setw(15) << jarsSold[i] << std::endl;
}
std::cout << "*********************\n";
std::cout << "Total Sales = " << totalJarsSold << std::endl;
std::cout << "Highest Seller = " << salsaTypes[highIndex] << std::endl;
std::cout << "Lowest Seller = " << salsaTypes[lowIndex] << std::endl;
}
int getJarsSold(std::string type) {
bool validData = false;
int numJars;
while (!validData) {
std::cout << "Enter jars sold this month for " << type << " salsa type: ";
std::cin >> numJars;
if (numJars < 0) {
std::cout << "Number of jars sold must be 0 or positive number.\n";
}
else {
validData = true;
}
}
validData = false;
return numJars;
}
void getSalesData(int jarsSold[],int size,int totalJars,int highIndex,int lowIndex) {
bool firstRun = true;
int highVal,lowVal;
totalJars = 0;
for (int i = 0; i < size; i++) {
if (firstRun) {
highIndex = i;
highVal = jarsSold[highIndex];
lowIndex = i;
lowVal = jarsSold[lowIndex];
totalJars += jarsSold[i];
firstRun = false;
}
else {
totalJars += jarsSold[i];
if (jarsSold[i] > highVal) {
highVal = jarsSold[i];
highIndex = i;
}
if (jarsSold[i] < lowVal) {
lowVal = jarsSold[i];
lowIndex = i;
}
}
}
}
Any help is appreciated. I am just using -c for compiling and -o for linking, nothing fancy as I know no better.
Related
I want to display a progress bar however putting the printing code inside a separate function seems to invoke an std::flush as each time the progress bar is printing in a new line. This did not happen when the code was used inline
The code:
#include <iostream>
#include <unistd.h>
void load(int curr, int total) {
std::cout << "\n[";
int pos = 50 * curr/total;
for (int i = 0; i < 50; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "]" << int(float(curr)/(float)total * 100.0) << " %\r";
std::cout.flush();
}
int main(){
for( int i = 0; i <= 5; i++ ){
load(i,5);
}
std::cout << std::endl;
return 0;
}
What it does:
[> ]0 %
[==========> ]20 %
[====================> ]40 %
[==============================> ]60 %
[========================================> ]80 %
[==================================================]100 %
What it's supposed to do: print all on the same line
The first line in your function outputs \n, which is what makes it print on a new line every iteration.
Fix:
#include <iostream>
void load(int curr, int total) {
std::cout << '[';
int pos = 50 * curr/total;
for (int i = 0; i < 50; ++i) {
if (i < pos) std::cout << '=';
else if (i == pos) std::cout << '>';
else std::cout << ' ';
}
std::cout << ']' << int(float(curr)/(float)total * 100.0) << " %\r" << std::flush;
}
int main(){
for( int i = 0; i <= 5; i++ ){
load(i, 5);
}
std::cout << '\n';
}
This is the main Test File and cannot be changed and there are 3 files in total not including Header files. main.cpp, engine.cpp, ship.cpp. I have included a screenshot of my error as well but its the invalid read of size 8 error (memory leak) at the T5 part of the test in the ship.cpp file. It outputs the correct answer but obviously has a memory leak.My error pic
#include <iostream>
#include "Ship.h"
#include "Ship.h"
#include "Engine.h"
#include "Engine.h"
using namespace std;
using namespace sdds;
void printHeader(const char* title)
{
char oldFill = cout.fill('-');
cout.width(40);
cout << "" << endl;
cout << "|> " << title << endl;
cout.fill('-');
cout.width(40);
cout << "" << endl;
cout.fill(oldFill);
}
int main()
{
{
printHeader("T1: Testing Constants");
cout << "TYPE_MAX_SIZE: " << sdds::TYPE_MAX_SIZE << endl;
cout << "MIN_STD_POWER: " << sdds::MIN_STD_POWER << endl;
cout << "MAX_STD_POWER: " << sdds::MAX_STD_POWER << endl;
cout << endl;
}
{
printHeader("T2: Testing Default Constructor");
Ship invalid;
invalid.display();
invalid += Engine("D2", 2.1);
cout << endl;
}
Engine engines[] = {
Engine("V8", 4.4),
Engine("V8", 5.0),
Engine("Inline", 4.1),
Engine("D3", 7.0),
Engine("D0", 2.0),
Engine("D1", 3.2),
};
{
printHeader("T3: Testing Custom Constructor");
Ship titanic("cruiser", engines, 6);
titanic.display();
cout << endl;
}
{
printHeader("T4: Testing Conversion to Bool Operator");
Ship invalid;
Ship titanic("liner", engines, 1);
if (invalid)
cout << "1. Test Failed! Object should be invalid.\n";
else
cout << "1. Test succeeded!\n";
if (titanic)
cout << "2. Test succeeded!\n";
else
cout << "3. Test Failed! Object should be valid.\n";
cout << endl;
}
{
printHeader("T5: Testing += and < Operators");
Ship titanic("liner", engines, 3);
char type[]{ "D0" };
while (titanic < sdds::MIN_STD_POWER)
{
type[1]++;
cout << "Ship not up to standard. Required power: "
<< sdds::MIN_STD_POWER << endl;
titanic += Engine(type, 2.1);
}
titanic.display();
if (sdds::MAX_STD_POWER < titanic)
cout << "Too much power." << endl;
else
cout << "Ship doesn't exceed power regulation of: "
<< sdds::MAX_STD_POWER << endl;
}
return 0;
}
This is my engine.cpp file
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include "Engine.h"
using namespace sdds;
using namespace std;
namespace sdds {
Engine::Engine(){ // Default Empty Engine
m_type[0] = '\0';
m_size = 0.0;
}
Engine::Engine(const char* type, double size){ // Custom Engine
strncpy(m_type, type, TYPE_MAX_SIZE);
m_size = size;
}
double Engine::get() const{ // Getter for the size of the engine
return m_size;
}
void Engine::display() const{ // Basic Display Function
cout << m_size << " liters - " << m_type << endl;
}
}
This is my ship.cpp where the error is in the += overloaded operator function.
#include <iostream>
#include <cstring>
#include "Ship.h"
using namespace std;
namespace sdds {
Ship::Ship(){ // Default Ship (empty)
m_type = nullptr;
m_engines = nullptr;
m_engCnt = 0;
}
Ship::Ship(const char* type, const Engine* engines, int engCnt){ // Custom Ship
if (type != nullptr && engines != nullptr && engCnt > 0) {
// creating a Valid Ship
int len = (unsigned)strlen(type);
m_type = new char[len + 1];
strcpy(m_type, type);
m_engines = new Engine[engCnt];
for (int i = 0; i < engCnt; i++) {
m_engines[i] = engines[i];
}
m_engCnt = engCnt;
}else{
m_type = nullptr; // Setting Ship to Empty State
m_engines = nullptr;
m_engCnt = 0;
}
}
Ship::~Ship(){
delete[] m_engines;
delete[] m_type;
}
Ship::operator bool() const {
// returning true if the ship is valid (not empty)
return m_type != nullptr;
}
Ship& Ship::operator+=(Engine e){ // THIS IS WHERE THE ERROR IS <-----------------
if (!*this) {
cout << "The object is not valid! Engine cannot be added!" << endl;
}else{
Engine* tmp = new Engine[m_engCnt + 1];
for (int i = 0; i <= m_engCnt; i++) {
tmp[i] = m_engines[i];
}
tmp[m_engCnt++] = e;
for (int i = 0; i <= m_engCnt; i++) {
m_engines[i] = tmp[i];
}
}
return *this;
}
double Ship::calculatePower() const { // Multiplying the Engines Size * 5 to get the power(sum of all)
double power = 0;
for (int i = 0; i < m_engCnt; i++) {
power += m_engines[i].get() * 5;
}
return power;
}
void Ship::display()const{ // Displaying the Ship
if (*this) {
streamsize dp = cout.precision(); // save default precision
cout.precision(2); // change default precision
cout << fixed; // enable fixed
cout << m_type << " - " << calculatePower() << endl;
for (int i = 0; i < m_engCnt; i++) {
m_engines[i].display(); // Engines Display function
}
cout.unsetf(ios::fixed); // disable fixed
cout.precision(dp); // restore default precision
}else{
cout << "No available data" << endl;
}
}
bool Ship::operator<(double power) const{ // Comparing the passed in power with the power of the ship
if (calculatePower() < power) {
return true;
}else{
return false;
}
}
bool operator<(double power, const Ship& theShip){ // Global < overloaded operator
if (power < theShip.calculatePower()) {
return true;
}else{
return false;
}
}
}
I'm at a bit of a roadblock and I just can't seem to figure out where I went wrong. Essentially, I just want to pass the values from the array in the test code to a vector via constructor and then print the contents of the vector. For whatever reason, I can't even hit the for loop that starts to add the array values to the vector.
header code:
#pragma once
#ifndef BoxOfProduce_H
#define BoxOfProduce_H
#include <vector>
#include <iostream>
#include <cstdlib> //for exit
using namespace std;
class BoxOfProduce
{
public:
BoxOfProduce();
BoxOfProduce(string customerId, int size, bool doRandom, bool okDuplicates, std::string productList[], int listSize);
void addBundle(string productList);
void displayBox();
private:
string customerID;
vector<string> test;
vector<string> bundles;
bool allowDuplicates;
bool buildRandom;
int size;
};
#endif
// End of dayOfYear.h
BocOfProduce.cpp
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
#include <cstdlib> //for exit
using namespace std;
#include "BoxOfProduce.h"
vector<string> tempbundles;
bool isInVect = false;
BoxOfProduce::BoxOfProduce()
{
customerID = "No Name";
allowDuplicates = true;
buildRandom = false;
}
BoxOfProduce::BoxOfProduce(string customerId, int size, bool doRandom, bool okDuplicates, string productList[], int listSize)
{
customerID = customerId;
buildRandom = doRandom;
allowDuplicates = okDuplicates;
size = size;
for (int k = 0; k > listSize; k++)
{
tempbundles.push_back(productList[k]);
cout << "added to temp" << endl;
}
if (allowDuplicates == false)
{
for (int k = 0; k > listSize; k++)
{
tempbundles.push_back(productList[k]);
cout << "added to temp" << endl;
}
for (int i = 0; i < listSize; i++)
{
for (int j = 0 + 1; j < listSize; j++)
{
if (tempbundles[i] == bundles[j])
{
isInVect = true;
}
if (isInVect == false)
{
bundles.push_back(tempbundles[i]);
cout << "added to isinvect bundle" << endl;
}
}
}
}
else if (allowDuplicates == true)
{
for (int k = 1; k > listSize; k++)
{
bundles.push_back(productList[k]);
cout << "added to normal bundle" << endl;
}
}
}
void BoxOfProduce::addBundle(string productList)
{
for (int k = 1; k > 100; k++)
{
bundles.push_back(productList);
}
}
void BoxOfProduce::displayBox()
{
cout << "custome ID is: " << customerID << "\n" << endl;
std::cout << std::boolalpha;
cout << "-buildRandom set to " << buildRandom << endl;
cout << "-allowDuplicates set to " << allowDuplicates << endl;
cout << "Contents of box: " << customerID << " with size: " << size << endl;
test.push_back("test");
for (int i = 0; i < bundles.size(); i++)
{
cout << bundles[i] << "\n";
}
cout << "\n" << endl;
}
Test code:
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
#include <cstdlib> //for exit
#include "BoxOfProduce.h"
using namespace std;
int main()
{
srand(1234); // Seed random generator for random additions of products
const int LISTSIZE = 12;
string produceList[] = { "Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo",
"Mango", "Spinach", "Cucumber", "Radish", "Chard", "Spinach", "Mango" };
cout << "Original list of produce:" << endl;
for (int i = 0; i < LISTSIZE; i++)
{
cout << "item[" << i << "] = " << produceList[i] << endl;
}
// Test BoxOfProduce class
cout << endl << "Start with empty box0" << endl;
BoxOfProduce box0; // Default constructor creates empty box
cout << "Display box0:" << endl;
box0.displayBox(); // Display the empty box
cout << endl; // Skip a line
cout << "Add all products from the produceList[] to box0 allowing duplicates:"
<< endl;
for (int i = 0; i < LISTSIZE; i++)
box0.addBundle(produceList[i]); // Duplicates allowed in box
cout << "Display box0 again after loading with products:" << endl;
box0.displayBox();
cout << endl;
BoxOfProduce box1("Box-1", 4, false, true, produceList, LISTSIZE);
box1.displayBox();
BoxOfProduce box2("Box-2", 4, true, false, produceList, LISTSIZE);
box2.displayBox();
BoxOfProduce box3("Box-3", 8, true, true, produceList, LISTSIZE);
box3.displayBox();
BoxOfProduce box4("Box-4", 12, true, true, produceList, LISTSIZE);
box4.displayBox();
BoxOfProduce box5("Box-5", 12, true, false, produceList, LISTSIZE);
box5.displayBox(); // This box produces an error message
}
Thank you for any and all help!
your problem is that in case of duplicate you use the list size for bundle and tempbundle which is not the case this should fix check the code for BocOfProduce.cpp
vector<string> tempbundles;
bool isInVect = false;
BoxOfProduce::BoxOfProduce()
{
customerID = "No Name";
allowDuplicates = true;
buildRandom = false;
}
BoxOfProduce::BoxOfProduce(string customerId, int size, bool doRandom, bool okDuplicates, string productList[], int listSize)
{
customerID = customerId;
buildRandom = doRandom;
allowDuplicates = okDuplicates;
size = size;
tempbundles = vector<int>() ; // set temp bundles to empty vector
for (int k = 0; k < listSize; k++) //k was >listSize it should be <
{
tempbundles.push_back(productList[k]);
cout << "added to temp" << endl;
}
if (allowDuplicates == false)
{
for (int k = 0; k < listSize; k++)
{
tempbundles.push_back(productList[k]);
cout << "added to temp" << endl;
}
for (int i = 0; i < listSize; i++)
{
isInVect = false;
for (int j = 0 + 1; j < bundles.size(); j++)
{
if (tempbundles[i] == bundles[j])
{
isInVect = true;
}
}
if (isInVect == false)
{
bundles.push_back(tempbundles[i]);
cout << "added to isinvect bundle" << endl;
}
}
}
else if (allowDuplicates == true)
{
for (int k = 1; k < listSize; k++)
{
bundles.push_back(productList[k]);
cout << "added to normal bundle" << endl;
}
}
}
void BoxOfProduce::addBundle(string productList)
{
for (int k = 1; k > 100; k++)
{
bundles.push_back(productList);
}
}
void BoxOfProduce::displayBox()
{
cout << "custome ID is: " << customerID << "\n" << endl;
std::cout << std::boolalpha;
cout << "-buildRandom set to " << buildRandom << endl;
cout << "-allowDuplicates set to " << allowDuplicates << endl;
cout << "Contents of box: " << customerID << " with size: " << bundles.size() << endl;
test.push_back("test");
for (int i = 0; i < bundles.size(); i++)
{
cout << bundles[i] << "\n";
}
cout << "\n" << endl;
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
//Class for a card deck:
class CardDeck
{
public:
CardDeck(int theValue, string theSuit);
CardDeck(){}
// Setters--Don't think we will need
void setValue(int theValue);
void setSuit(string theSuit);
// Getters
int getValue();
string getSuit();
private:
int value;
string suit;
};// end CardDeck class
int main()
{
int i = 0;
int gameInPlay = 1;
const string DR = "Dragons";
const string MG = "Mages";
const string WR = "Warriors";
const string CF = "Confessors";
vector<CardDeck> startDeck(52);
vector<CardDeck> tempCards(1);
// Dragons Suit
for (i = 0; i < 13; i++)
{
startDeck[i].setValue(i - 12);
startDeck[i].setSuit("Dragons");
//startDeck[i].setValue(i+1);
// startDeck[i].setSuit("Dragons");
}
// Mages Suit
for (i = 13; i < 26; i++)
{
startDeck[i].setValue(i - 12);
startDeck[i].setSuit("Mages");
}
for (i = 26; i < 39; i++)
{
startDeck[i].setValue(i - 25);
startDeck[i].setSuit("Warriors");
}
for (i = 39; i < 52; i++)
{
startDeck[i].setValue(i - 38);
startDeck[i].setSuit("Confessors");
}
// Output for de-bug
cout << "The first card is " << startDeck[0].getValue() << " of " << startDeck[0].getSuit() << endl;
cout << "The second card is " << startDeck[1].getValue() << " of " << startDeck[1].getSuit() << "\n\n";
//****************************************************************************
// Shuffle the deck
int shuffleTimes = (rand() % 120) + 1;
// Need to shuffle a random # of times, else deck is
// "shuffled" in same order every time
for (int i = 0; i < shuffleTimes; i++)
{
srand(time(0));
for (i = 0; i < startDeck.size(); i++)
{
int second = rand() % startDeck.size();
CardDeck temp = startDeck[i];
startDeck[i] = startDeck[second];
startDeck[second] = temp;
}
}
//*******************************************************************************
// Verify cards are shuffled for de-bug
cout << "After shuffling:\n Value \t Suit\n";
// Output for de-bug
cout << "The first card is " << startDeck[0].getValue() << " of " << startDeck[0].getSuit() << endl;
cout << "The second card is " << startDeck[1].getValue() << " of " << startDeck[1].getSuit() << endl;
// Creat human deck
vector<CardDeck> humanDeck(26);
for (i = 0; i< 26; i++)
{
humanDeck[i] = startDeck[i];
}
// Creat computer deck
vector<CardDeck> computerDeck(26);
for (i = 0; i< 26; i++)
{
computerDeck[i] = startDeck[i + 26];
}
// Output for de-bug
cout << "The first human card is " << humanDeck[0].getValue() << " of " << humanDeck[0].getSuit() << endl;
cout << "The second human card is " << humanDeck[1].getValue() << " of " << humanDeck[1].getSuit() << "\n\n";
cout << "The first computer card is " << computerDeck[0].getValue() << " of " << computerDeck[0].getSuit() << endl;
cout << "The second computer card is " << computerDeck[1].getValue() << " of " << computerDeck[1].getSuit() << "\n\n";
getchar();
return 0;
} // end main
// Functions for CardDeck class
CardDeck::CardDeck(int theValue, string theSuit)
{
value = theValue;
suit = theSuit;
}
void CardDeck::setValue(int theValue)
{
value = theValue;
}
void CardDeck::setSuit(string theSuit)
{
suit = theSuit;
}
int CardDeck::getValue()
{
return value;
}
string CardDeck::getSuit()
{
return suit;
}
Obviously not done with the game, and I am new to C++ and programming so any help will do
I would like some help trying to figure out how to get only positive numbers instead of negative. Also would like to figure out why they return values of the first two outputs are always the same.
Thank you
You probably meant to do this:
for (i = 0; i < 13; i++)
{
startDeck[i].setValue(i+1);
startDeck[i].setSuit("Dragons");
//startDeck[i].setValue(i+1);
// startDeck[i].setSuit("Dragons");
}
Otherwise, startDeck[i].setValue(i-12); will set negative values for i < 12, which is most of that loop.
I'm wondering why you have the correct code there and commented out...what was the issue with it?
This is a part of a program that I am writing to compute the addition of two integers as strings. (Writing my own bigInt class).
There appears to be a problem when I am adding the two integers together. Because they are both in vectors of char type, I had to add a '0' to each element of the vector before concatenating it into a string.
However, the results are still not what I am expecting:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string const Number = "1000";
string const Number2 = "1000";
vector<char> reverse;
vector<char> reverse2;
//cout << (rostrNumber[1] - '0') << endl;
cout << "Original Number: " << Number << endl;
reverse.clear();
for (int i = Number.size() - 1; i >= 0; i--)
{
reverse.push_back(Number[i]);
}
cout << "Reversed: " << endl;
cout << reverse[0] << reverse[1] << reverse[2] << reverse[3] << endl;
cout << endl << endl;
reverse2.clear();
{
for (int i = Number2.size() - 1; i >= 0; i--)
{
reverse2.push_back(Number[i]);
}
}
cout << "Adding these two integers" << endl;
vector<char> const rcvN1 = reverse;
vector<char> const rcvN2 = reverse2;
vector<char> Results;
Results.clear();
//Local copies
vector<char> vN1 = rcvN1;
vector<char> vN2 = rcvN2;
int iSize1 = vN1.size();
int iSize2 = vN2.size();
int i, iSize = iSize2;
int iC = 0, iR;
for (i = 0; i<iSize; i++)
{
iR = vN1[i] + vN2[i] + iC;
if (iR > 9)
{
iR -= 10;
iC = 1;
}
else
iC = 0;
Results.push_back(iR);
cout << Results[0] << endl;
}
if (iC > 0)
Results.push_back(iC);
string ostr;
vector<char>::const_reverse_iterator rIter = Results.rbegin();
for (; rIter != Results.rend(); rIter++)
ostr += *rIter +'0';
cout << "Results: " << ostr << endl;
system("PAUSE");
return 0;
}