c++ nqueen backtracking with stack with non-error, but program stuck - c++

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?

Related

Struct doesn't print with cout

I'm trying to print the structure in an array whose int prejetih member is highest.
#include <iostream>
using namespace std;
struct Tekma {
int prejetih;
};
Tekma najvec_kosev(Tekma tekme[]) {
int maksi = 0, index = 0;
for (int i = 0;i < 2;i++) {
if (maksi < tekme[i].prejetih) {
index = i;
maksi = tekme[i].prejetih;
}
}
return tekme[index];
}
void vpis(Tekma tekme[]) {
for (int i = 0;i < 2;i++)
cin >> tekme[i].prejetih;
}
int main() {
Tekma tekme[2];
vpis(tekme);
cout << najvec_kosev(tekme);
}
The compiler reports
C++ no operator matches these operands
operand types are: std::ostream << Tekma
over cout << najvec_kosev(tekme);
Here using a solution with std::vector and fixing your cout problem:
#include <iostream>
#include <vector>
using namespace std;
struct Tekma {
int prejetih;
};
Tekma najvec_kosev(vector<Tekma>& tekme) {
Tekma lowest = tekme[0]
for(auto& t : tekme) {
if(lowest.prejetih < t.prejetih) {
lowest = t;
}
}
return lowest ;
}
void vpis(vector<Tekma>& tekme) {
int input;
while(true) {
cin >> input;
// Check if the input is valid else quit the loop.
if(input == valid) {
Tekma newStruct = {input};
tekme.push(newStruct);
}
else {
// Stop the loop
break;
}
}
}
int main() {
vector<Tekma> tekme;
vpis(tekme);
cout << najvec_kosev(tekme).prejetih; // This fixes your error.
}

Expected a Declaration(C++)

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

Passing array values using pointer

I have two functions one is for file reading and another one for little sorting of numbers. With function read() I am reading file's each line and put each line into array. File looks like:
1
2
3
With function sort() I want to print only numbers with value greater than 1.
What is wrong: I got printed two arrays, but my sort array still printing all values, not only greater than 1.
My code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
class UZD
{
private:
int numLines;
int *value;
public:
UZD();
int * read();
int sort();
};
// =========================================================
UZD::UZD()
{
}
// =========================================================
int * UZD::read(){
ifstream myfile("stu.txt");
int value[20];
string line[20];
int i=0;
while(!myfile.eof() && i < 20) {
getline(myfile,line[i]);
++i;
}
numLines = i;
for (i=0; i < numLines; ++i) {
value[i] = atoi(line[i].c_str());
cout << i << ". " << value[i]<<'\n';
}
return value;
}
// =========================================================
int UZD::sort(){
int *p;
p = read();
int i;
if(p[i] > 1) {
cout << p <<'\n';
}
}
// =========================================================
int main() {
UZD wow;
wow.read();
wow.sort();
}
There are many issues in your code, the most obvious one is "return value" in the read() method. Value is a local array and will be gone once out of scope of read(). Also the design seems faulty. You are calling read() twice, once from the main() and again internally from sort().
I have written a working code, using vectors. Probably this is what you are expecting:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
class UZD
{
private:
int numLines;
vector<int> value;
public:
UZD();
vector<int> & read();
void sort();
};
// =========================================================
UZD::UZD()
{
}
// =========================================================
vector<int> & UZD::read(){
ifstream myfile("stu.txt");
vector<string> line(20);
int i=0;
while(!myfile.eof() && i < 20) {
getline(myfile,line[i]);
++i;
}
numLines = i;
cout << "After reading file: " << endl;
for (i=0; i < numLines; ++i) {
value.push_back(atoi(line[i].c_str()));
cout << i << ". " << value[i]<<'\n';
}
return value;
}
// =========================================================
void UZD::sort(){
cout << "Inside sort()" << endl;
for(int i=0; i<value.size(); ++i){
if(value[i] > 1)
cout << value[i] << endl;
}
}
// =========================================================
int main() {
UZD wow;
wow.read();
wow.sort();
return 0;
}
I have kept the variable names same for clarity. Let me know if you don't get anything.
There are a lot of issues in your program; just to mention some of them:
In sort, you use variable i uninitialized, which is undefined behaviour (probably crashes); You write while(!myfile.eof()..., which is usually considered wrong (see this SO answer; The use of atoi is not recommended, as it is not safe if the parameter does not represent a number; You return a pointer to a local variable, which is destroyed one going out of scope; You declare member variables, but pass through local ones (e.g. value)...
See the following code which demonstrates the usage of int* and a vector<int>; hope it helps.
class UZD
{
private:
int numLines;
int *value = nullptr;
public:
~UZD() {
if (value)
delete value;
};
void read();
void print();
};
// =========================================================
void UZD::read(){
ifstream myfile("stu.txt");
value = new int[20];
int val;
numLines = 0;
while(numLines < 20 && myfile >> val) {
value[numLines] = val;
numLines++;
}
}
// =========================================================
void UZD::print(){
for (int i=0; i<numLines; i++)
cout << value[i] << endl;
}
class UZD_vector
{
private:
vector<int> values;
public:
void read();
void print();
};
// =========================================================
void UZD_vector::read(){
ifstream myfile("stu.txt");
int val;
while(myfile >> val) {
values.push_back(val);
}
}
// =========================================================
void UZD_vector::print(){
for (auto val : values)
cout << val << endl;
}
// =========================================================
int main() {
cout << "with int[]:" << endl;
UZD wow;
wow.read();
wow.print();
cout << "with vector:" << endl;
UZD wow_vector;
wow_vector.read();
wow_vector.print();
}
Here is your own code rectified, just in case you find vectors too difficult to learn (which should not be true, though)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
class UZD
{
private:
int numLines;
int *value;
int num;
public:
UZD();
void read();
void sort();
};
// =========================================================
UZD::UZD():num(20)
{}
// =========================================================
void UZD::read(){
ifstream myfile("stu.txt");
value = new int[num];
string line[num];
int i=0;
while(!myfile.eof() && i < num) {
getline(myfile,line[i]);
++i;
}
numLines = i;
for (i=0; i < numLines; ++i) {
value[i] = atoi(line[i].c_str());
cout << i << ". " << value[i]<<'\n';
}
}
// =========================================================
void UZD::sort(){
cout << "After sorting: " << endl;
for (int i = 0; i < num; ++i) {
if(value[i] > 1)
cout << value[i] << endl;
}
}
// =========================================================
int main() {
UZD wow;
wow.read();
wow.sort();
return 0;
}

C++ Creating Lottery Guessing Program

Okay so the project is to create a lottery number composed of 10 random positive integers and the user is suppose to guess it until the user guesses the correct number. All of my code looks good but when I run the program and enter in a number it gives me this MSVS Runtime Library error? I dont even know what it means as I am fairly new to programming. Help would be very appreciated!
Main.cpp
#include <iostream>
#include <cmath>
#include <ctime>
#include "Lottery.h"
using namespace std;
int main() {
const int size = 9; //declare variables
int win[size];
int g;
srand(time(NULL));
assign(win, size);
draw(win, size);
g = entry();
if (check(win,size,g) == true) {
cout << "Congradulations! You have won the lottery!" << endl;
}
else {
cout << "Try again!" << endl;
}
printOut(g);
}
Lottery.cpp
#include <iostream>
#include <cmath>
#include "Lottery.h"
using namespace std;
int entry() {
int guess;
cout << "Enter a number from 0 to 99." << endl;
cin >> guess;
return guess;
}
void assign(int w[], int s) {
for (int i = 0; i < s; i++) {
w[s] = -1;
}
}
bool check(int w[], int s, int g) {
for (int i = 0; i < s; i++) {
if (g == w[i]) {
return true;
}
}
return false;
}
void draw(int w[], int s) {
for (int i = 0; i < s; i++) {
int tmp = rand() % 100;
if (check(w, s, tmp)) {
i--;
}
else
w[i] = tmp;
}
}
void printOut(int g) {
cout << "Numbers you have chosen:" << " " << g << endl;
}
Lottery.h
#ifndef LOTTERY_INCLUDED
#define LOTTERY_INCLUDED
void assign(int[], int);
bool check(int[], int, int);
void draw(int[], int);
int entry();
void printOut(int);
#endif //LOTTERY
Debugging tutorials are available elsewhere. But if something bad happens, don't panic and look for instructions.
First, your runtime error:
This has a link "Break and open exception settings" link or a "Break" button. Break which will take you to the end of main if you click it.
The details say we did something bad near win.
Look at this:
void assign(int w[], int s) {
for (int i = 0; i < s; i++) {
w[s] = -1; //<------Oh oops!
}
}
We know the length of the array is s i.e. 9, and are using w[s] where we clearly meant w[i].
The extra details in the error are telling you a possible place to look.

Boolean function not giving right answer

I have a problem with my boolean function check_gift. It gives the value false while the gift is in the store...
What am I doing wrong?
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <string>
#include <cassert>
using namespace std;
typedef vector<string> Wishes;
int size(Wishes& w){ return static_cast<int>(w.size()); }
struct Wishlist
{
double budget;
Wishes wishes;
};
struct Gift
{
double price;
string name;
};
typedef vector<Gift> Giftstore;
int size(Giftstore& g) { return static_cast<int>(g.size()); }
void read_wishlist_into_struct(ifstream& infile, Wishlist& wishlist)
{
double b;
infile>>b;
wishlist.budget=b;
int i=0;
string name;
getline(infile,name);
while(infile)
{
wishlist.wishes.push_back(name);
i++;
getline(infile,name);
}
infile.close();
}
void show_wishlist(Wishlist wishlist)
{
cout<<"Budget: "<<wishlist.budget<<endl<<endl;
cout<<"Wishes: "<<endl;
for(int i=0; i<size(wishlist.wishes); i++)
{
cout<<wishlist.wishes[i]<<endl;
}
cout<<endl;
}
void read_giftstore_into_vector(ifstream& infile, Gift& gift, Giftstore& giftstore)
{
double p;
string name;
int i=0;
infile>>p;
while(infile)
{
gift.price=p;
getline(infile,name);
gift.name=name;
giftstore.push_back(gift);
i++;
infile>>p;
}
infile.close();
}
void show_giftstore(Giftstore giftstore)
{
cout<<"All possible gifts in giftstore: "<<endl<<endl;
for(int i=0; i<size(giftstore); i++)
{
cout<<giftstore[i].price<<"\t"<<giftstore[i].name<<endl;
}
cout<<endl;
}
bool check_gift(Giftstore giftstore, string giftname)
{
int i=0;
while(i<size(giftstore))
{
if(giftstore[i].name==giftname)
{
return true;
}
else
{
i++;
}
}
return false;
}
void clear(Wishlist& b)
{
b.budget=0;
while(!b.wishes.empty())
{
b.wishes.pop_back();
}
}
void copy(Wishlist a, Wishlist& b)
{
b.budget=a.budget;
for (int i=0; i<size(b.wishes); i++)
{
b.wishes.push_back(a.wishes[i]);
}
}
int main ()
{
ifstream infile2("giftstore.txt");
Gift gift;
Giftstore giftstore;
read_giftstore_into_vector(infile2, gift, giftstore);
show_giftstore(giftstore);
string giftname;
giftname=("dvd Up van Pixar");
bool x;
x=check_gift(giftstore, giftname);
cout<<"in store?: "<<x<<endl;
return 0;
}
You forgot to return false at the end of your check_gift function
bool check_gift(Giftstore giftstore, string giftname)
{
int i=0;
while(i<size(giftstore))
{
if(giftstore[i].name==giftname)
{
return true;
}
else
{
i++;
}
}
return false;
}
There is no return false; at the end of the function; so if the gift is not found, the program will fall off the end and give undefined behaviour.
The compiler should warn you about this, if you enable warnings.
You need to add return false at the end of check_gift().