C++ char vector addition - c++

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;
}

Related

Cannot Create a Lazy String and cannot access memory

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]);
}

returning multiple values in value returning function

I want to write a program that counts the number of times a certain vowel appears in a string but each value must be returned.
int vowel_count(string);
int main()
{
string str;
cout << "Enter a string";
cin >> str;
// Calling funtion:
vowel_count(str);
}
int vowel_count(string var)
{
int sum_a = 0;
int sum_e = 0;
int sum_i = 0;
int sum_o = 0;
int sum_u = 0;
for (int j = 0; j < var.length(); j++)
{
if (Var.at(j) == 'a')
sum_a++;
if (Var.at(j) == 'e')
sum_e++;
if (Var.at(j) == 'i')
sum_i++;
if (Var.at(j) == 'o')
sum_o++;
if (Var.at(j) == 'u')
sum_u++;
}
return sum_a;
return sum_e;
return sum_i;
return sum_o;
return sum_u;
}
Everytime I return , the program terminates, I would like to know how to I overrun the function of return.
You can use std::tuple to return multiple values from a function. Then you can either use std::get or std::tie to get at the values in the tuple. This avoids having to define a struct/class simply for the purpose of returning a result from your function vowel_count().
Since you tagged your question with C++14, I assume you haven't got C++17. However, with C++17 you can use structured bindings to extract the values instead.
For C++11/C++14:
#include<iostream>
#include<tuple>
using TupleVowel = std::tuple<int, int, int, int, int>;
TupleVowel vowel_count(std::string var)
{
int sum_a = 0;
int sum_e = 0;
int sum_i = 0;
int sum_o = 0;
int sum_u = 0;
for (int j = 0; j < var.length(); j++)
{
if (var.at(j) == 'a')
sum_a++;
if (var.at(j) == 'e')
sum_e++;
if (var.at(j) == 'i')
sum_i++;
if (var.at(j) == 'o')
sum_o++;
if (var.at(j) == 'u')
sum_u++;
}
return std::make_tuple(sum_a, sum_e, sum_i, sum_o, sum_u);
}
int main(void)
{
int sum_a = 0;
int sum_e = 0;
int sum_i = 0;
int sum_o = 0;
int sum_u = 0;
std::tie(sum_a, sum_e, sum_i, sum_o, sum_u) = vowel_count("hello world");
std::cout << "sum_a = " << sum_a << ", sum_e = " << sum_e << ", sum_i = " << sum_i << ", sum_o = " << sum_o << ", sum_u = " << sum_u << std::endl;
}
Live demo.
You can also use std::get() with the index of the element like this without having to 'tie' the tuple elements to variables:
auto tup = vowel_count("hello world");
std::cout << "sum_a = " << std::get<0>(tup) << ", sum_e = " << std::get<1>(tup) << ", sum_i = " << std::get<2>(tup) << ", sum_o = " << std::get<3>(tup) << ", sum_u = " << std::get<4>(tup) << std::endl;
In C++17, you can just do:
auto [sum_a, sum_e, sum_i, sum_o, sum_u] = vowel_count("hello world");
Live demo.
When the type of the returned variable is the same, you can use a std::array (also std::vector or std::deque, but, given that the number of the returned variable is fixed, I suppose std::array is preferable)
I mean, something as follows
#include <array>
#include <iostream>
auto vowel_count (std::string var)
{
int sum_a = 0;
int sum_e = 0;
int sum_i = 0;
int sum_o = 0;
int sum_u = 0;
for ( auto const & ch : var )
if ( ch == 'a') ++sum_a;
else if ( ch == 'e') ++sum_e;
else if ( ch == 'i') ++sum_i;
else if ( ch == 'o') ++sum_o;
else if ( ch == 'u') ++sum_u;
return std::array<int, 5u>{ sum_a, sum_e, sum_i, sum_o, sum_u };
}
int main()
{
std::string str;
std::cout << "Enter a string";
std::cin >> str;
auto vc = vowel_count(str);
std::cout << "a: " << vc[0] << std::endl;
std::cout << "e: " << vc[1] << std::endl;
std::cout << "i: " << vc[2] << std::endl;
std::cout << "o: " << vc[3] << std::endl;
std::cout << "u: " << vc[4] << std::endl;
}
Observe that, starting from C++17, you can use structured bindings, exactly as suggested from jignatius in the std::tuple based solution.
auto [sum_a, sum_e, sum_i, sum_o, sum_u] = vowel_count(str);
std::cout << "a: " << sum_a << std::endl;
std::cout << "e: " << sum_e << std::endl;
std::cout << "i: " << sum_i << std::endl;
std::cout << "o: " << sum_o << std::endl;
std::cout << "u: " << sum_u << std::endl;
The answers above are way too complicated.
The easiest solution is a simple struct.
struct VowelResult {
int a;
int e;
int i;
int o;
int u;
};
VowelResult func () {
int sumA = 0;
int sumE = 0;
int sumI = 0;
int sumO = 0;
int sumU = 0;
//your code goes here...
return {sumA,sumE,sumI,sumO,sumU}; // order matters here
// alternatively:
// VowelResult res{};
// res.a = sumA;
// res.e = sumE;
// and so on...
// return res;
// alternatively alternativey you can simply use the struct directly to store the sums
}

Can't pass array values to a vector from within a constructor

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;
}

segmentation fault for string function argument

I have a simple main code that gives me segmentation fault when calling a function. In the following code, I have two functions, the first one works correctly but the program doesn't enter the second one and gives me segmentation fault error. Is there any reason for that? I have made sure about the following:
The variables o and c are not out of bound.
cn is initialized correctly.
I have a read-only access to cm and argv. Plus it does not even enter the function evaluate
Here is the code:
void print_cm(vector<vector<int> > *cm, char* gtf);
void evaluate(vector<vector<int> > *cm, char* gtf);
int main(int argc, char** argv)
{
int o = 2; // It is initialized
int c = 4; // It is initialized
vector<vector<int> > cm; // It is initialized
if (argc>4)
print_cm(&cm, argv[o]);
if (argc>4)
{
cout << argv[c] << endl; // Works
// The following also works
for (int i=0; i<cm.size(); i++)
for (int j=0; j<cm[i].size(); j++)
cout << cm[i][j] << " ";
// The following causes segmentation fault;
evaluate(&cm, argv[c]);
}
return 0;
}
void evaluate(vector<vector<int> > *cm, char* gtf)
{
// Read-only access to cm and gtf
}
void print_cm(vector<vector<int> > *cm, char* gtf)
{
// Read-only access to cm and gtf
}
Here is the complete code:
#include "includes/Utility.h"
#include "includes/Graph.h"
void print_cm(vector<vector<int> > *cores, char* output);
void evaluate(vector<vector<int> > const *cm, char* gtf);
int main(int argc, char** argv)
{
int g = -1, c = -1, o = -1;
for (int i=1; i<argc-1; i++)
if (argv[i][0]=='-')
{
if (argv[i][1]=='g')
g = i + 1;
else if (argv[i][1]=='c')
c = i + 1;
else if (argv[i][1]=='k')
ki = i + 1;
else if (argv[i][1]=='s')
si = i + 1;
else if (argv[i][1]=='o')
o = i + 1;
}
Graph G;
if (c>0) G.read_input(argv[g], argv[c]);
else G.read_input(argv[g]);
if (ki > 0)
{
int k = atoi(argv[ki]);
cout << k << endl;
}
if (si > 0)
{
int s = atoi(argv[si]);
cout << s << endl;
}
// Find communities
vector<vector<int> > cores;
G.partitioning(&cores);
if (o>0)
print_cm(&cores, argv[o]);
if (c>0)
{
cout << "here" << endl;
for (size_t i=0; i<cores.size(); i++)
for (size_t j=0; j<cores[i].size(); j++)
if (cores.at(i).at(j)<0) cout << "here";
cout << "here" << endl;
evaluate(&cores, argv[c]);
}
}
return 0;
}
void print_cm(vector<vector<int> > *cores, char* output)
{
ofstream out;
out.open(output);
for(size_t i=0; i<(*cores).size(); i++)
{
for(size_t j=0; j<(*cores)[i].size(); j++)
out << (*cores)[i][j] << " ";
out << endl;
}
out.close();
return ;
}
void evaluate(vector<vector<int> > const *cm, char* gtf)
{
// we evaluate precision, recall, F1 and F2
vector<vector<int> > gt;
ifstream in;
char str[100000000];
in.open(gtf);
while(in.getline(str, 100000000))
{
stringstream s;
s << str;
int a;
gt.resize(gt.size()+1);
while (s >> a) gt[gt.size()-1].push_back(a);
}
in.close();
cout << "==================== Evaluation Results ====================" << endl;
int imax = 0;
for(size_t i=0; i<(*cm).size(); i++)
imax = max(imax, *max_element((*cm)[i].begin(), (*cm)[i].end()));
for(size_t i=0; i<gt.size(); i++)
imax = max(imax, *max_element(gt[i].begin(), gt[i].end()));
vector<bool> flag(imax, false);
vector<double> recall((*cm).size(), 0), precision((*cm).size(), 0), f1((*cm).size(), 0), f2((*cm).size(), 0);
int overlap;
double size = 0;
for(size_t i=0; i<(*cm).size(); i++)
{
// evaluate
size += (double) (*cm)[i].size();
for(size_t j=0; j<(*cm)[i].size(); j++)
flag[(*cm)[i][j]] = true;
double p, r, ff1, ff2;
for(size_t j=0; j<gt.size(); j++)
{
overlap = 0;
for(size_t k=0; k<gt[j].size(); k++)
if (flag[gt[j][k]]) overlap++;
p = (double) overlap / (double) (*cm)[i].size();
if (p > precision[i])
precision[i] = p;
r = (double) overlap / (double) gt[j].size();
if (r > recall[i])
recall[i] = r;
ff1 = (double) 2*(p*r)/(p+r);
if (ff1 > f1[i])
f1[i] = ff1;
ff2 = (double) 5*(p*r)/(4*p + r);
if (ff2 > f2[i])
f2[i] = ff2;
}
for(size_t j=0; j<(*cm)[i].size(); j++)
flag[(*cm)[i][j]] = false;
}
double Recall = 0, Precision = 0, F1 = 0, F2 = 0;
for(size_t i=0; i<(*cm).size(); i++)
{
Recall += recall[i];
Precision += precision[i];
F1 += f1[i];
F2 += f2[i];
}
cout << "+--------------+--------------+--------------+--------------+" << endl;
cout << "| " << setiosflags( ios::left ) << setw(10) << "Precision";
cout << " | " << setiosflags( ios::left ) << setw(10) << "Recall";
cout << " | " << setiosflags( ios::left ) << setw(10) << "F1-measure";
cout << " | " << setiosflags( ios::left ) << setw(10) << "F2-measure";
cout << " |" << endl;
cout << "| " << setiosflags( ios::left ) << setw(10) << Precision/(*cm).size() ;
cout << " | " << setiosflags( ios::left ) << setw(10) << Recall/(*cm).size();
cout << " | " << setiosflags( ios::left ) << setw(10) << F1/(*cm).size();
cout << " | " << setiosflags( ios::left ) << setw(10) << F2/(*cm).size();
cout << " |" << endl;
cout << "+--------------+--------------+--------------+--------------+" << endl;
cout << "Number of communities: " << (*cm).size() << endl;
cout << "Average community size: " << size/(*cm).size() << endl;
return ;
}
char str[100000000];
This is in your evaluate function. This are 100 million bytes, or about 95 MB that you're allocating on the stack.
Typical stack sizes are far less than that, around 1 MB.
So apart from possible other problems this is most likely causing a stack overflow.
When entering the function, the stack frame gets extended to be large enough to hold the local variables. As soon as the stack is used then (to write a default value) you're accessing invalid (non stack, thankfully protected) memory.

War Card Game Shuffle Function Returning Negative #'s

#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?