After getting a segmentation fault error, the debugger gives me an error that says I cannot access memory at address 0x1. It also says cannot create a lazy string with address 0x0, and a non-zero length. Does anyone know how I can fix this?
Thanks!
#include <iostream>
#include <string>
#include "unorderedSet.h"
using namespace std;
int main()
{
int intArr[] = {12,23,4,7,12,9,10,6,23,11};
//Debugger points to here
string strArr[] = {"banana","apple","pear","grape",
"banana","fig","mango","orange","pear","guava"};
unorderedSet<int> intSet(20);
for (int i = 0; i < 10; i++)
{
intSet.insertEnd(intArr[i]);
}
unorderedSet<string> strSet(20);
for (int i = 0; i < 10; i++)
{
strSet.insertEnd(strArr[i]);
}
cout << "\nInteger Set: " << intSet << endl;
cout << "String Set: " << strSet << endl;
intSet.insertAt(5,30);
cout << "\nInsert At non-duplicate\nInteger Set: " << intSet << endl;
intSet.insertAt(5,11);
cout << "Insert At duplicate\nInteger Set: " << intSet << endl;
strSet.replaceAt(1,"pineapple");
cout << "\nReplace At non-duplicate\nString Set: " << strSet << endl;
strSet.replaceAt(3,"pear");
cout << "Replace At Duplicate\nString Set: " << strSet << endl;
int intArr1[] = {7,0,19,56,22,11,23,5};
//Debugger points to here
string strArr1[] = {"red","yellow","grape","banana","mango","orange","guava"};
unorderedSet<int> intSet1(20);
for (int i = 0; i < 8; i++)
{
intSet1.insertEnd(intArr1[i]);
}
unorderedSet<string> strSet1(20);
for (int i = 0; i < 7; i++)
{
strSet1.insertEnd(strArr1[i]);
}
Related
I am new to C++ and learning data structures. In the below code I am getting an "out of range warning", and do not understand what I am doing wrong.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers{100,-1,2,4,55,78,3};
int temp {};
int pass {};
pass = numbers.size();
for(int i {0} ;i<pass-1;i++){
for(int j {0} ; j<pass-1-i ; j++){
if(numbers.at(j) > numbers.at(j+1)){
temp = numbers.at(j);
numbers.at(j)=numbers.at(j+1);
numbers.at(j+1)=temp;
}
}
}
cout << numbers.at(0) << endl;
cout << numbers.at(1) << endl;
cout << numbers.at(2) << endl;
cout << numbers.at(3) << endl;
cout << numbers.at(4) << endl;
cout << numbers.at(5) << endl;
cout << numbers.at(6) << endl;
cout << numbers.at(7) << endl;
cout << numbers.at(8) << endl;
return 0;
}
It seems like you may not understand how std::vectors work.
You have only declared 7 elements in your vector which means you can only go up to the index 6. This is because std::vector's indices start at 0. This is true for std::array as well.
vector<int> numbers{100,-1,2,4,55,78,3};
However, in your code you have put these two statements:
cout << numbers.at(7) << endl;
cout << numbers.at(8) << endl;
which doesn't work because like I mentioned you can only go up to index 6.
You should also consider using a for loop like the comments mention above. It is more simple to use and is less work.
For example eith a for loop:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers{ 100,-1,2,4,55,78,3 };
int temp{};
int pass{};
pass = numbers.size();
for (int i{ 0 }; i < pass - 1; i++) {
for (int j{ 0 }; j < pass - 1 - i; j++) {
if (numbers.at(j) > numbers.at(j + 1)) {
temp = numbers.at(j);
numbers.at(j) = numbers.at(j + 1);
numbers.at(j + 1) = temp;
}
}
}
std::cout << "v = { ";
for (int i = 0; i < numbers.size(); i++) {
std::cout << numbers.at(i) << ", ";
}
std::cout << "}; \n";
return 0;
}
Output:
v = { -1, 2, 3, 4, 55, 78, 100, };
In the following code, when run using GNU GCC v8.2.0, code does not terminate:
int main(void)
{
/* code */
int myArray[] = {2, 4};
int otherArray[] = {777, 888};
for(int i = 0; i<4; i++)
{
myArray[i] = 0;
cout << "myArray[" << i << "]=";
cout << myArray[i] << endl;
cout << "add: " << &myArray[i] << endl;
}
for(int i = 0; i<2; i++)
{
cout << "otherArray[" << i << "]=";
cout << otherArray[i] << endl;
cout << "add: " << &otherArray[i] << endl;
}
return 0;
}
output:
add:0x28ff24
myarray[2]=0
add:0x28ff28
myarray[0]=0
add:0x28ff20
myarray[1]=0
add:0x28ff24
myarray[2]=0
add:0x28ff28
myarray[0]=0
add:0x28ff20
myarray[1]=0
add:0x28ff24
myarray[2]=0
add:0x28ff28
myarray[0]=0
add:0x28ff20
myarray[1]=0
add:0x28ff24
myarray[2]=0
add:0x28ff28
myarray[0]=0
add:0x28ff20
myarray[1]^C
for(int i = 0; i<4; i++)
Replace the 4 in the 'for loop' by 2 like this:
for(int i = 0; i<2; i++)
Since you're using a static array so it's better to specify the fixed size, but the most important is to be aware when you try to access the array by comparing the index being processed with the size of the array to avoid this behavior.
You are invoking undefined behaviour by writing the indexes 0-3 of the 2 element array myArray.
As this is undefined behaviour there are no guarantees as to what is happening or what will happen if you run the code again in the future. A likely explanation for your observed behaviour is that when you write myArray[2] that is actually overwriting the value of i causing your loop to restart back at 0.
The simple solution is to make myArray larger or change your for loop limit to 2.
To detect this sort of behaviour use std::array instead and call the at function which has bounds checking and will throw an exception when you go outside the bounds of the array. e.g.:
#include <array>
#include <iostream>
int main(void)
{
/* code */
std::array< int, 2 > myArray = { 2, 4 };
std::array< int, 2 > otherArray = { 777, 888 };
for(int i = 0; i<4; i++)
{
myArray.at(i) = 0;
std::cout << "myArray[" << i << "]=";
std::cout << myArray[i] << "\n";
std::cout << "add: " << &myArray.at(i) << "\n";
}
for(int i = 0; i<2; i++)
{
std::cout << "otherArray[" << i << "]=";
std::cout << otherArray.at(i) << "\n";
std::cout << "add: " << &otherArray.at(i) << "\n";
}
return 0;
}
std::array also has the bonus of a size() method which can make your code safer too:
for(int i = 0; i<myArray.size(); i++)
{
myArray.at(i) = 0;
std::cout << "myArray[" << i << "]=";
std::cout << myArray[i] << "\n";
std::cout << "add: " << &myArray.at(i) << "\n";
}
I am new in C++, I created a struct called Device with two fields
string MacAdress
vector<string> RSSI
Then, I created a vector of structure: vector<Device> Devices
I want to extract the vector<string> RSSI and display its contant.
Here is where I got stuck in my main.cpp:
cout << "display MAC and RSSI"<< endl;
Device CurrentDevice;
for(int j=0; j<Devices.size();j++)
{
CurrentDevice = Devices.at(j);
vector<string>::const_iterator begin = CurrentDevice.GetRSSIs().begin();
vector<string>::const_iterator last = CurrentDevice.GetRSSIs().begin() + CurrentDevice.GetRSSIs().size();
vector<string> intermed(begin+1, last);
cout << "Size: "<< intermed.size() << endl;
for (int i = 0 ; i < intermed.size(); i++)
{
cout << intermed[i] << endl;
cout << "device n°"<< j+1<<" " << "MAC "<< " "<< CurrentDevice.GetMacAdress()<< endl;
for(int k=0; k<intermed.size();k++)
{
cout << "device n°" << j;
cout << "\tRSSI " << k << " = " << intermed.at(k)<< endl;
}
}
return 0;
}
I end up with Size=0
here is some simplified code that does not use iterators but should still do the job:
#include <iostream>
#include <string>
#include <vector>
int main()
{
struct Device {
std::string MacAddress;
std::vector<std::string> RSSI;
};
std::vector<Device> Devices;
// add some stuff to first object
Device CurrentDevice1;
CurrentDevice1.MacAddress = "A-B-C";
CurrentDevice1.RSSI.push_back("rssi11");
CurrentDevice1.RSSI.push_back("rssi12");
CurrentDevice1.RSSI.push_back("rssi13");
Devices.push_back(CurrentDevice1);
// add some stuff to second object
Device CurrentDevice2;
CurrentDevice2.MacAddress = "D-E-F";
CurrentDevice2.RSSI.push_back("rssi21");
CurrentDevice2.RSSI.push_back("rssi22");
Devices.push_back(CurrentDevice2);
// see object MAC's
for (int i = 0; i < Devices.size(); i++){
std::cout << "device " << i+1 << " MAC: " << Devices[i].MacAddress << std::endl;
}
// see object RSSI's
for (int j = 0; j < Devices.size(); j++){
for (int k = 0; k < Devices[j].RSSI.size(); k++){
std::cout << "device " << j + 1 << " RSSI: " << k +1 << " : " << Devices[j].RSSI[k] << std::endl;
}
std::cout << "\n";
}
return 0;
}
I'm not sure about what do you want to obtain, but I suppose the problem is in the following lines
vector<string>::const_iterator last = CurrentDevice.GetRSSIs().begin() + CurrentDevice.GetRSSIs().size();
vector<string> intermed(begin+1, last);
Do you want to obtain a copy of CurrentDevice.GetRSSIs()?
In this case, you can use begin() and end()
vector<string> intermed(CurrentDevice().GetRSSIs().begin(),
CurrentDevice().GetRSSIs().end());
or, simpler, invoke the copy constructor,
vector<string> intermed(CurrentDevice().GetRSSIs());
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?