Adding in a HTML Repository in C++ problem - html

I have implemented a HTML Repository for my project and I have a problem that when I add something, there will be only the last element here. Also I have to do this without cached memory. Here's how my code looks:
HTMLRepository::HTMLRepository(const std::string& fileName)
{
this->fileName = fileName;
}
bool HTMLRepository::addDog(const Dog& dog)
{
//if (this->findDog(dog.getName()) != -1)
// throw RepositoryException("Dog already exists");
vector<Dog> dogList = this->getAllDogs();
dogList.push_back(dog);
this->writeVectorToFile(dogList);
return true;
}
This is how I write them:
void HTMLRepository::writeVectorToFile(std::vector<Dog> vectorOfDogs)
{
fstream fileOut(this->fileName);
fileOut << "<!DOCTYPE html>" << endl;
fileOut << "<html>" << endl;
fileOut << "<head>" << endl;
fileOut << "<title>myDogs</title>" << endl;
fileOut << "</head>" << endl;
fileOut << "<body>" << endl;
fileOut << "<table border = '1'>" << endl;
fileOut << "<tr>" << endl;
fileOut << "<td>Name</td>" << endl;
fileOut << "<td>Breed</td>" << endl;
fileOut << "<td>Birth Date</td>" << endl;
fileOut << "<td>Number of shots</td>" << endl;
fileOut << "<td>Photo</td>" << endl;
fileOut << "</tr>" << endl;
for (auto dog : vectorOfDogs) {
fileOut << "<tr>" << endl;
fileOut << "<td>" << dog.getName() << "</td>" << endl;
fileOut << "<td>" << dog.getBreed() << "</td>" << endl;
fileOut << "<td>" << dog.getBirthDate() << "</td>" << endl;
fileOut << "<td>" << dog.getNumberOfShots() << "</td>" << endl;
fileOut << "<td>" << dog.getPhoto() << "</td>" << endl;
fileOut << "</tr>" << endl;
}
fileOut << "</table>" << endl;
fileOut << "</body>" << endl;
fileOut << "</html>" << endl;
fileOut.close();
}
And this is how I get all the objects:
std::vector<Dog> HTMLRepository::getAllDogs() const
{
fstream fileInput(this->fileName);
int numberOfUselessLines = 14, position;
string line, name, breed, birth, photo;
int numberOfShots;
while (numberOfUselessLines != 0)
{
numberOfUselessLines--;
getline(fileInput, line);
}
Dog dog;
vector<Dog> vectorOfDogs;
while (getline(fileInput, line))
{
if (line.find("table>"))
break;
getline(fileInput, line);
position = line.find(">");
line.erase(0, position + 1);
position = line.find("<");
name = line.substr(0, position);
getline(fileInput, line);
position = line.find(">");
line.erase(0, position + 1);
position = line.find("<");
breed = line.substr(0, position);
getline(fileInput, line);
position = line.find(">");
line.erase(0, position + 1);
position = line.find("<");
birth = line.substr(0, position);
getline(fileInput, line);
position = line.find(">");
line.erase(0, position + 1);
position = line.find("<");
breed = line.substr(0, position);
getline(fileInput, line);
position = line.find(">");
line.erase(0, position + 1);
position = line.find("<");
numberOfShots = stoi(line.substr(0, position));
getline(fileInput, line);
position = line.find(">");
line.erase(0, position + 1);
position = line.find("<");
photo = line.substr(0, position);
dog = Dog(name, breed, birth, numberOfShots, photo);
vectorOfDogs.push_back(dog);
getline(fileInput, line);
}
fileInput.close();
return vectorOfDogs;
}
I know those functions are pretty long because it works on HTML, but I really need some help here, please.

Related

What is the behavior of the vector here?

I am unable to comprehend why in the test shown below, iterator p never reaches the end and therefore the loop breaks only when k = 20? What exactly is the push_back doing to cause undefined behavior? Is it because the vector dynamically allocated a bunch of additional storage for the new elements I want to use, and the amount is not necessarily the amount I will use?
#include <iostream>
#include <vector>
#include <list>
using namespace std;
const int MAGIC = 11223344;
void test()
{
bool allValid = true;
int k = 0;
vector<int> v2(5, MAGIC);
k = 0;
for (vector<int>::iterator p = v2.begin(); p != v2.end(); p++, k++)
{
if (k >= 20) // prevent infinite loop
break;
if (*p != MAGIC)
{
cout << "Item# " << k << " is " << *p << ", not " << MAGIC <<"!" << endl;
allValid = false;
}
if (k == 2)
{
for (int i = 0; i < 5; i++)
v2.push_back(MAGIC);
}
}
if (allValid && k == 10)
cout << "Passed test 3" << endl;
else
cout << "Failed test 3" << "\n" << k << endl;
}
int main()
{
test();
}
Insertion to a vector while iterating over it is really a bad idea. Data insertion may cause memory reallocation that invalidates iterators. In this case, the capacity was not enough to insert additional elements, which caused memory allocation with a different address. You can check it yourself:
void test()
{
bool allValid = true;
int k = 0;
vector<int> v2(5, MAGIC);
k = 0;
for (vector<int>::iterator p = v2.begin(); p != v2.end(); p++, k++)
{
cout << v2.capacity() << endl; // Print the vector capacity
if (k >= 20) // prevent infinite loop
break;
if (*p != MAGIC) {
//cout << "Item# " << k << " is " << *p << ", not " << MAGIC <<"!" << endl;
allValid = false;
}
if (k == 2) {
for (int i = 0; i < 5; i++)
v2.push_back(MAGIC);
}
}
if (allValid && k == 10)
cout << "Passed test 3" << endl;
else
cout << "Failed test 3" << "\n" << k << endl;
}
This code will output something like the following:
5
5
5
10 <-- the capacity has changed
10
... skipped ...
10
10
Failed test 3
20
We can see that where k is equal to 2 (third line), the capacity of the vector doubled (fourth line) because we are adding new elements. The memory is redistributed, and the vector elements are most likely now located elsewhere. You can also check it by printing vector base address with data member function instead of capacity:
Address: 0x136dc20 k: 0
Address: 0x136dc20 k: 1
Address: 0x136dc20 k: 2
Address: 0x136e050 k: 3 <-- the address has changed
Address: 0x136e050 k: 4
... skipped ...
Address: 0x136e050 k: 19
Address: 0x136e050 k: 20
Failed test 3
20
The code is poorly written, you can make it more robust by using indices instead of iterators.

namespace::function cannot be used as a function

main.cpp
#include "Primes.h"
#include <iostream>
int main(){
std::string choose;
int num1, num2;
while(1 == 1){
std::cout << "INSTRUCTIONS" << std::endl << "Enter:" << std::endl
<< "'c' to check whether a number is a prime," << std::endl
<< "'u' to view all the prime numbers between two numbers "
<< "that you want," << std::endl << "'x' to exit,"
<< std::endl << "Enter what you would like to do: ";
std::cin >> choose;
std::cout << std::endl;
if(choose == "c"){
std::cout << "Enter number: ";
std::cin >> num1;
Primes::checkPrimeness(num1) == 1 ?
std::cout << num1 << " is a prime." << std::endl << std::endl :
std::cout << num1 << " isn't a prime." << std::endl << std::endl;
}else if(choose == "u"){
std::cout << "Enter the number you want to start seeing primes "
<< "from: ";
std::cin >> num1;
std::cout << "\nEnter the number you want to stop seeing primes "
<< "till: ";
std::cin >> num2;
std::cout << std::endl;
for(num1; num1 <= num2; num1++){
Primes::checkPrimeness(num1) == 1 ?
std::cout << num1 << " is a prime." << std::endl :
std::cout << num1 << " isn't a prime." << std::endl;
}
}else if(choose == "x"){
return 0;
}
std::cout << std::endl;
}
}
Primes.h
#ifndef PRIMES_H
#define PRIMES_H
namespace Primes{
extern int num, count;
extern bool testPrime;
// Returns true if the number is a prime and false if it isn't.
int checkPrimeness(num);
}
#endif
Primes.cpp
#include "Primes.h"
#include <iostream>
int Primes::checkPrimeness(num){
if(num < 2){
return(0);
}else if(num == 2){
return(1);
}else{
for(count = 0; count < num; count++){
for(count = 2; count < num; count++){
if(num % count == 0){
return(0);
}else{
testPrime = true;
if(count == --num && testPrime == true){
return(1);
}
}
}
}
}
}
I get the following 3 errors: Errors from terminal
I've spent hours for days and still can't seem to fix the errors.
I've tried using extern and pretty much everything I can imagine.
Here is an error in function declaration:
int checkPrimeness(num);
defines a global integer variable checkPrimeness initialized with num! To declare a function you just should change it like:
int checkPrimeness(int);
Can't understand why you declare parameters as external variables. To split declarations and realization you should declare all functions and classes inside header file, and define them inside source file.

Error: 2.5e-1 cannot be used as a function

i wrote a simple program and i'm getting this error which i never encountered yet. Can you help me out?
line 13: error: 2.5e-1 cannot be used as a function
#include <iostream>
#include <iomanip>
using namespace std;
int dirac(int);
int main()
{
float y;
for(int k = 0; k <= 4; k++){
y = 2*dirac(k)-0.5*dirac(k-1)*0.25(2*dirac(k-2)-0.5*dirac(k-3));
cout << "k = " << k << ": ";
cout << setw(8) << setfill(' ');
cout << setprecision(3) << fixed << y << endl;
}
return 0;
}
int dirac(int x){
if(x == 0){
x = 1;
return x;
}else{
x = 0;
return x;
}
}
y = 2*dirac(k)-0.5*dirac(k-1)*0.25(2*dirac(k-2)-0.5*dirac(k-3));
^---
You probably forgot a * at the indicated spot.

How to validate player input in tic tac toe?

Hi i am trying to make a tic tac toe game where the player plays against the pc. The only problem is that i cannot validate the move of the player or pc, as in, if the pc places its move on the board the player cannot place their move in that same spot as it will be occupied.The same goes for the pc. My code is down below so have a look at it and let me know how to rectify it.
for(row=0;row<3;row++){
for(col=0;col<3;col++){
board[row][col]=' ';
}
}
cout << endl << endl;
for(row=0;row<3;row++){
for(col=0;col<3;col++){
cout << "|" << board[row][col] << "|" ;
}
cout << endl << "|-||-||-|" ;
cout << endl;
}
cout << endl << endl << endl;
cout << "Are you player 1 or 2?: ";
cin >> player;
cout << endl << endl << endl;
if(player==1){
cout << "Your symbol is 'O'.The computer's is 'X' " << endl;
}else if(player==2){
cout << "Your symbol is 'X'.The computer's is 'O' " << endl;
}
cout << endl << endl;
while(Gameover==false){
while(win==false){
if(player==1){
cout << "Enter row: ";
cin >> r;
cout << "Enter column: ";
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
cout << endl << endl << endl;
if(board[row][col]!=' '){
cout << "Invalid move.That spot is already occupied.";
cout << endl << endl << endl;
cout << "Enter row: ";
cin >> r;
cout << "Enter column: ";
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
}
board[r][c]='O';
board[x][y]='X';
}else if(player==2){
cout << "Enter row: ";
cin >> r;
cout << "Enter column: ";
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
if(board[row][col]!=' '){
cout << "Invalid move.That spot is already occupied.";
cout << endl << endl << endl;
cout << "Enter row: ";
cin >> r;
cout << "Enter column: ";
cin >> c;
srand(time(0));
x=rand()%3;
y=rand()%3;
}
board[r][c]='X';
board[x][y]='O';
}
cout << endl << endl << endl;
for(row=0;row<3;row++){
for(col=0;col<3;col++){
cout << "|" << board[row][col] << "|" ;
}
cout << endl << "|-||-||-|" ;
cout << endl;
}
cout << endl << endl << endl;
}
}
return 0;
}
you have a logic problem in the loop ,if(board[row][col]!=' ') sentence should be While(board[r][c]!=' ' then after this loop you have to test the computer player values like While (board[y][x]!=' ') after these to loops, you will put the values, the if clause will test once if the user enter the second time occupied place you will not know.

thrust::device_vector not working

I have written a code using Thrust. I am pasting the code and its output below. Strangely, when the device_vector line is reached during exectution the screen just hangs and no more output comes. It was working in the morning. Please help me.
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <iostream>
int main(void)
{
// H has storage for 4 integers
thrust::host_vector<int> H(4);
// initialize individual elements
H[0] = 14;
H[1] = 20;
H[2] = 38;
H[3] = 46;
// H.size() returns the size of vector H
std::cout << "H has size " << H.size() << std::endl;
// print contents of H
for(size_t i = 0; i < H.size(); i++)
std::cout << "H[" << i << "] = " << H[i] << std::endl;
// resize H
H.resize(2);
std::cout << "H now has size " << H.size() << std::endl;
// Copy host_vector H to device_vector D
thrust::device_vector<int> D = H;
// elements of D can be modified
D[0] = 99;
D[1] = 88;
// print contents of D
for(size_t i = 0; i < D.size(); i++)
std::cout << "D[" << i << "] = " << D[i] << std::endl;
// H and D are automatically deleted when the function returns
return 0;
}
The output is :
H has size 4
H[0] = 14
H[1] = 20
H[2] = 38
H[3] = 46
H now has size 2
* After this nothing happens
Run Device Query. I am confident that if the code was working in the morning, the problem is due to the graphics card.