I don't now what i missed but Visual Studio hits for on 12 row and says "Expected a declaration" and "Expected a ";"" at the end of code.
There's a code:
#include <string>
#include <math.h>
using namespace std;
class DigPow
{
public:
static int digPow(int n, int p)
{
int tmpnumb = 0;
string temp = to_string(n);
for (int i = 0; i<temp.length();i++)
{
tmpnumb += pow (temp[i],p);
p++;
}
for (int i = 1; ;i++)
{
if (tmpnumb*i == n)
{
return i;
}
if (tmpnumb*i > n)
{
return -1;
}
}
}
};
Related
I have written a code, but there’s one Problem. When I put cout<<""<<endl; out, I have Bus error: 10. How can I solve this Problem.
This is my code:
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <assert.h>
#include <ctime>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;
char* rotate(char const* a, int b)
{
char* r;
for (int i = 0; i < int(strlen(a)); i++) {
if (i >= int(strlen(a)) - b) {
r[i] = a[i - int(int(strlen(a)) - b)];
}
else if (i + b < 0) {
r[i] = a[int(strlen(a) + b)];
}
else {
r[i] = a[i + b];
}
}
r[int(strlen(a))] = '\0';
return r;
}
char* Crypt(char* Input, bool Encrypt, int InitialRotation, int Rotation)
{
char const* Table;
try {
Table = new char[size_t(strlen(Table))];
}
catch (...) {
throw "No Memory";
};
char* a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
try {
a = new char[size_t(strlen(a))];
}
catch (...) {
throw "No Memory";
};
char* ActualTable;
try {
ActualTable = new char[size_t(strlen(ActualTable))];
}
catch (...) {
throw "No Memory";
};
int Offset = InitialRotation;
char* Result;
try {
Result = new char[size_t(strlen(Result))];
}
catch (...) {
throw "No Memory";
};
int b;
Table = "JPGVOUMFYQBENHZRDKASXLICTW";
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < int(strlen(Input)); i++) {
for (int c = 0; i < int(strlen(a)); c++) {
if (a[c] == Input[i]) {
b = c;
break;
}
}
cout << "" << endl;
ActualTable = rotate(Table, Offset);
Result[i] = rotate(Table, Offset)[b];
Offset = Offset + Rotation;
}
return Result;
}
int main()
{
char* A = "ZZZZ";
cout << Crypt(A, 1, 1, 4) << endl;
return 0;
}
Visual studio said I got no issue but, every time I try to run my code I get an assertion failure error, and the error says that vector subscript is out of range, what should I do to fix this, I dont really know what I am doing wrong.
#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
class Human
{
private:
int ap;
int hp;
public:
Human()
{
srand(time(NULL));
ap = rand() % 3 + 8;
hp = rand() % 2 + 9;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class Skeleton
{
private:
int ap;
int hp;
public:
Skeleton()
{
srand(time(NULL));
ap = rand() % 3 + 3;
hp = rand() % 2 + 4;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class game
{
private:
std::vector<Human*> hum;
std::vector<Skeleton*> ske;
bool adv;
int sC;
int hC;
public:
game(int h, int s)
{
srand(time(NULL));
adv = rand() % 2;
sC = s;
hC = h;
for (int i = 0; i < h; i++) { hum.push_back( new Human()); }
for (int i = 0; i < h; i++) { ske.push_back(new Skeleton()); }
}
~game()
{
for (int i = 0; i < hum.size(); i++)
{
Human* current = hum.back();
hum.pop_back();
delete current;
}
for (int i = 0; i < ske.size(); i++)
{
Skeleton* current = ske.back();
ske.pop_back();
delete current;
}
}
void start()
{
int x = hC-1;
int y = sC-1;
bool quit = false;
while (!quit)
{
if (adv)
{
ske[y]->dmg(hum[x]->getAp());
if (ske[y]->getHp() <= 0) { y--; ske.pop_back();}
adv = 0;
}
if (!adv)
{
hum[x]->dmg(ske[y]->getAp());
if (hum[x]->getHp() <= 0) { x--; hum.pop_back(); }
adv = 1;
}
if (hum.size() == 0 || ske.size() == 0)
{
cout << "human left : " << hum.size() << "skeleton left : " << ske.size();
quit = true;
}
}
}
};
int main()
{
game g1(10, 5);
g1.start();
return 0;
}
I always get this error box.
this is the error message i got no idea what are they talkin about.
I also get this, what is this?
this
ive fixed it thx to all of ur comments,
this is the completed code, it may look like garbage but it runs
#include <iostream>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
class Human
{
private:
int ap;
int hp;
public:
Human()
{
ap = rand() % 3 + 8;
hp = rand() % 2 + 9;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class Skeleton
{
private:
int ap;
int hp;
public:
Skeleton()
{
ap = rand() % 3 + 3;
hp = rand() % 2 + 4;
}
int getAp() { return ap; }
int getHp() { return hp; }
void dmg(int x) { hp -= x; }
};
class game
{
private:
std::vector<Human*> hum;
std::vector<Skeleton*> ske;
bool adv;
int sC;
int hC;
public:
game(int h, int s)
{
srand(time(NULL));
adv = rand() % 2;
sC = s;
hC = h;
for (int i = 0; i < h; i++) { hum.push_back( new Human()); }
for (int i = 0; i < s; i++) { ske.push_back(new Skeleton()); }
}
~game()
{
for (int i = 0; i < hum.size(); i++)
{
Human* current = hum.back();
hum.pop_back();
delete current;
}
for (int i = 0; i < ske.size(); i++)
{
Skeleton* current = ske.back();
ske.pop_back();
delete current;
}
}
void start()
{
int x = hum.size()-1;
int y = ske.size()-1;
bool quit = false;
while (!quit)
{
if (hum.size() == 0 || ske.size() == 0)
{
cout << "human left : " << hum.size() << "skeleton left : " << ske.size();
quit = true;
break;
}
if (adv)
{
ske.at(y)->dmg(hum.at(x)->getAp());
if (ske.at(y)->getHp() <= 0) { y--; ske.pop_back();}
adv = 0;
}
else if (!adv)
{
hum.at(x)->dmg(ske.at(y)->getAp());
if (hum.at(x)->getHp() <= 0) { x--; hum.pop_back(); }
adv = 1;
}
}
}
};
int main()
{
game g1(8, 20);
g1.start();
return 0;
}
Your final code is somewhat complex than it should be, I have simplified it by:
Remove the windows related header files, they are not used
Use member initializer lists
Modify dmg to return the latest hp
Remove the unnecessary index in start, to use back
Remove the vector of pointers, it's Ok to use objects here
Move the srand call into main(Actually we can use std::random here)
Simplify the while loop in start
#include <ctime>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
class Human {
private:
int ap;
int hp;
public:
Human() : ap(rand() % 3 + 8), hp(rand() % 2 + 9) {}
int getAp() { return ap; }
int getHp() { return hp; }
int dmg(int x) { return hp -= x; }
};
class Skeleton {
private:
int ap;
int hp;
public:
Skeleton() : ap(rand() % 3 + 3), hp(rand() % 2 + 4) {}
int getAp() { return ap; }
int getHp() { return hp; }
int dmg(int x) { return hp -= x; }
};
class game {
private:
bool adv;
int sC;
int hC;
std::vector<Human> hum;
std::vector<Skeleton> ske;
public:
game(int h, int s) : adv(rand() % 2), sC(s), hC(h), hum(h), ske(s) {}
void start() {
while (!hum.empty() && !ske.empty()) {
if (adv) {
if (ske.back().dmg(hum.back().getAp()) <= 0) {
ske.pop_back();
}
} else {
if (hum.back().dmg(ske.back().getAp()) <= 0) {
hum.pop_back();
}
}
adv = !adv;
}
cout << "human left : " << hum.size() << ", skeleton left : " << ske.size();
}
};
int main() {
srand(time(NULL));
game g1(8, 20);
g1.start();
return 0;
}
Online demo
In function start, ske[y] is accessed with a negative index. std::vector does not like negative indexes, this needs to be fixed.
Hey I'm having trouble figuring out what is causing my program to trigger a breakpoint. For school I had to write a class using templates that mimics a java array list, but in c++. My code seems to work when I only use the push method that I have written more than twice in a test. If I try pushing more than two values onto the arraylist, it starts triggering breakpoints.
Here's my code:
HeaderFile:
Arraylist.h
#include <string>
using namespace std;
template<class t>
class ArrayList {
public:
ArrayList();
void push_back(t m);
void erase(t m);
int size();
string toString();
t& operator[](unsigned int i);
private:
int currentIndex;
int sizeOfArray;
t *p;
int currentindex;
};
ArrayList.cpp
#include "ArrayList.h"
#include <iostream>
using namespace std;
template<class t>
ArrayList<t>::ArrayList() {
p = new t[1];
sizeOfArray = 1;
currentIndex = 0;
p[currentIndex] = 0;
currentIndex++;
}
template<class t>
int ArrayList<t>::size() {
return sizeOfArray;
}
template<class t>
void ArrayList<t>::erase(t m) {
int location = 0;
for (int i = 0; i < currentIndex; i++) {
if (p[i] == m) {
location = i;
}
}
for (int i = location; i < currentIndex - 1; i++) {
p[i] = p[i + 1];
}
currentIndex--;
}
template<class t>
void ArrayList<t>::push_back(t m) {
if (currentIndex < sizeOfArray-1) {
t *temp = new t[sizeOfArray + 1];
sizeOfArray++;
for(int i=0;i<sizeOfArray-1;i++){
temp[i] = p[i];
}
temp[currentIndex] = m;
currentIndex++;
sizeOfArray++;
delete [] p;
t *p = new t[sizeOfArray];
p = temp;
delete [] temp;
}
else {
p[currentIndex] = m;
currentIndex++;
}
}
template<class t>
t& ArrayList<t>::operator[](unsigned int i) {
return p[i];
}
template<class t>
string ArrayList<t>::toString() {
string output = "";
for (int i = 0; i < sizeof(p); i++) {
output += to_string(p[i]);
}
return output;
}
Source.cpp (just using it as a scratchpad for testing)
#include "ArrayList.cpp"
#include <iostream>
using namespace std;
int main()
{
ArrayList<double> list = ArrayList<double>();
list.push_back(1.0);
list.push_back(2.0);
list.push_back(3.0);
//list.push_back(4.0);
cout << list.toString();
cin.get();
return 0;
}
I believe that the problem has something to do with me somehow going out of bounds with one of my array's, but I can't seem to figure it out (perhaps I've just been staring at this assignment for to long). Any help, or suggestions would be much appreciated as always.
this my Library.h file, before the Library used to do All the dirty work: in term of manipulating the arrays and stuff, but now I am trying to make the Library the middle man that just invoke the call that has to do with any array manipulations. My problem is I am trying to have one instance of Patron array, that would hold all the patrons in the Library.
#ifndef LIBRARY_H
#define LIBRARY_H
#include <string>
#include "types.h"
#include "Book.h"
#include "Patron.h"
#include "PatronArray.h"
//class PatronArray
class Library
{
public:
Library();
~Library();
void init();
int addBook(Book*);
int addPatron(Patron*);
int remPatron(int);
int findBook(int, Book**, int*);
int findPatron(string, string, Patron**, int*);
int getMaxCollIndex();
int getMaxPatronsIndex();
Book* getBook(int);
Patron* getPatron(int);
private:
Book* collection[MAX_COLL_SIZE];
PatronArray* patrons;
int maxCollIndex;
int maxPatronsIndex;
};
#endif
This is my Library.cc file
#include "Library.h"
Library::Library()
: maxCollIndex(0)
{
patrons = new PatronArray;
for (int i=0; i<MAX_COLL_SIZE; ++i) {
collection[i] = 0;
}
}
Library::~Library()
{
delete patrons;
for (int i=0; i<maxCollIndex; ++i)
delete collection[i];
}
int Library::getMaxCollIndex() { return maxCollIndex; }
int Library::getMaxPatronsIndex()
{
return patrons->getMaxPatronsIndex();
}
Book* Library::getBook(int index)
{
if (index < 0 || index >= maxCollIndex)
return 0;
return collection[index];
}
Patron* Library::getPatron(int index)
{
return patrons->getPatron(index);
}
void Library::init()
{
Book* newBook;
Patron* newPatron;
newBook = new Book("Ender's Game", "Orson Scott Card", 1985);
addBook(newBook);
newBook = new Book("Dune", "Frank Herbert", 1965);
newBook->setStatus(LOST);
addBook(newBook);
newBook = new Book("Foundation", "Isaac Asimov", 1951);
addBook(newBook);
newBook = new Book("Hitch Hiker's Guide to the Galaxy", "Douglas Adams", 1979);
addBook(newBook);
newPatron = new Patron("Jack", "Shephard");
addPatron(newPatron);
}
int Library::addBook(Book* book)
{
if (maxCollIndex >= MAX_COLL_SIZE - 1) {
return C_NOK;
}
collection[maxCollIndex++] = book;
return C_OK;
}
int Library::addPatron(Patron* patron)
{
// the lbrary is the middle ma that invokesthe calls
//return patronArray->addPatron(patron);
return patrons->addPatron(patron);
}
int Library::remPatron(int index)
{
return patrons->remPatron(index);
}
int Library::findPatron(string fn, string ln, Patron** patron, int* index)
{
return patrons->findPatron(fn,ln,patron,index);
}
int Library::findBook(int id, Book** book, int* index)
{
for (int i=0; i<maxCollIndex; ++i) {
if (collection[i] == 0)
continue;
if (collection[i]->getId() == id) {
*book = collection[i];
*index = i;
return C_OK;
}
}
*book = 0;
*index = -1;
return C_NOK;
}
This is my PatronArray.h that holds all the Patrons registered within the Library.
#ifndef PATRONARRAY_H
#define PATRONARRAY_H
#include "Patron.h"
#include "Book.h"
#include "types.h"
//class Patron;
class PatronArray
{
public:
PatronArray();
~PatronArray();
int addPatron(Patron*);
int remPatron(int);
int findPatron(string, string, Patron**, int*);
int getMaxPatronsIndex();
Patron* getPatron(int);
private:
Patron* patrons[MAX_COLL_SIZE];
int maxPatronsIndex;
};
#endif
and this is the PatronArray.cc file and please, I know there is better way of doing this by doing templating which I currently dont understand yet plus this way helps me understand the whole object oriented style.
#include<iostream>
#include<string>
#include "PatronArray.h"
/*
* Default constructor: recheck this later
*/
PatronArray::PatronArray()
:maxPatronsIndex(0)
{
for (int i = 0; i < MAX_COLL_SIZE; ++i) {
patrons[i] = 0;
}
}
/*
* Destructor: recheck this later
*/
PatronArray::~PatronArray()
{
for (int i = 0; i < maxPatronsIndex; ++i)
delete patrons[i];
}
//get the maxindex
int PatronArray::getMaxPatronsIndex() { return maxPatronsIndex; }
/*
* Adds the given Patron to the given patrons Array
*/
int PatronArray::addPatron(Patron* patron)
{
if (maxPatronsIndex >= MAX_COLL_SIZE - 1) {
return C_NOK;
}
patrons[maxPatronsIndex++] = patron;
return C_OK;
}
/*
* Used for removing a patron in the patrons array
*/
int PatronArray::remPatron(int index)
{
if (index < 0 || index >= maxPatronsIndex)
return C_NOK;
delete patrons[index];
patrons[index] = 0;
return C_OK;
}
/*
* Searches for the patron; if found, sets the contents of the second
* parameter to that patron pointer, sets the contents of the third parameter to
* its index in the collection, and returns C_OK; if not found, sets the
* contents of the second parameter to zero, the theird to -1, and returns C_NOK
*/
int PatronArray::findPatron( string fn, string ln, Patron** patron, int* index)
{
for (int i = 0; i < maxPatronsIndex; ++i) {
if (patrons[i] == 0)
continue;
if (patrons[i]->getFname() == fn && patrons[i]->getLname() == ln) {
*patron = patrons[i];
*index = i;
return C_OK;
}
}
*patron = 0;
*index = -1;
return C_NOK;
}
Patron* PatronArray::getPatron(int index)
{
if (index < 0 || index >= maxPatronsIndex)
return 0;
return patrons[index];
}
I forgot to link my PatronArray.cc to Library.cc in my makefile, thank you Jack!
I found one solution or nqueen problem and tried to compile, but it compiles and stuck after running. I couldn't find what is the problem.
Here are the codes
main.cpp
#include <iostream>
#include <cmath>
#include <stack>
#include <vector>
#include "Queen.h"
using namespace std;
int main()
{
int n;
int row;
int col;
cout << "Enter the num of queens: " << endl;
cin >> n;
Queen one(1,1);
stack<Queen> result;
result.push(one);
for (int i=0; i<n; i++) {
Queen z(i,i);
int c=z.getcol();
if (one.hit(z,n)==true) {
while(one.hit(z,n)==true) {
int b=z.getcol();
z.setcol((z.getcol()+1));
one.hit(z,n);
}
result.push(z);
} else {
result.push(z);
}
for (int i=1; i<n; i++) {
cout << "Number of Solution is "<< result.size() <<endl;
}
return 0;
}
}
Queen.h
#ifndef QUEEN_H
#define QUEEN_H
#include <iostream>
#include <cmath>
#include <stack>
#include <vector>
using namespace std;
class Queen
{
public:
Queen();
Queen(int row,int col);
bool hit(Queen b, int size);
int getrow();
int getcol();
int setcol(int x);
private:
double row;
double col;
};
#endif // QUEEN_H
Queen.cpp
#include "Queen.h"
Queen::Queen()
{
this->row=1;
this->col=1;
}
Queen::Queen(int row, int col)
{
this->row=1;
this->col=1;
}
bool Queen::hit(Queen b, int size)
{
int r1=row;
double r2=b.getrow();
int c1=col;
double c2=b.getcol();
int rmod=abs(r2-this->row);
int cmod=abs(c2-this->col);
//compare row
if (row==b.getrow()) {
return true;
}
//compare col
if (col==b.getcol()) {
return true;
}
//compare diag
if (rmod==cmod) {
return true;
}
else {
return false;
}
}
int Queen::getrow()
{
return row;
}
int Queen::getcol()
{
return col;
}
int Queen::setcol(int x)
{
//int x;
col=x;
return x;
}
I think all syntax looks fine. Debugger says no error. What should I fix?