void ca_time(int *arr,int &len_) //the variable len_'s value always is 0
{
cout << "You only got five seconds to type the number 1~5,,ready go,,\n";
_sleep(5000);
cout << "Sorry time out!!\n";
cout << "Ok, here is your greads:\n";
for(int i = 0; i < len_; i ++)
{
cout << arr[i] << " ";
}
cout << endl;
cout <<"-->" << len_ << endl;
return;
}
void fun_type(int *arr,int &len_)
{
memset(arr,'\0',sizeof(arr));
for(; len_ < 5; len_ ++)
{
cin >> arr[len_];
}
}
int main()
{
int arr[100];
int len = 0;
thread time(ca_time,arr,len);
time.detach();
fun_type(arr,len);
system("pause");
return 0;
}
But it work when changed the quote to the address(point variable).Why?
Somebody say that's a IED's bug?But I don't think so .So What the hell?
Related
Ok so i'am writting this code for an exercise we have to write a class for matrices and then to check it we use a code that the teacher wrote to check if it performes the normal operations.
And i don't get exactly where i am wrong it seems to bother the compiler that i return a type Matrix but really i dont get what is a lvalue or rvalue .
But i get this when i try to compile
anthony#anthony-linux:~/Documents/CS/Ex6$ g++ -Wall simplematrixtest.cc Matrix.cc -o simplematrixtest
simplematrixtest.cc: In function ‘int main()’:
simplematrixtest.cc:70:24: error: cannot bind non-const lvalue reference of type ‘Matrix&’ to an rvalue of type ‘Matrix’
70 | Matrix g = f.multiply(a);
| ~~~~~~~~~~^~~
In file included from simplematrixtest.cc:4:
Matrix.hh:13:18: note: initializing argument 1 of ‘Matrix::Matrix(Matrix&)’
13 | Matrix(Matrix &m);
| ~~~~~~~~^
this is my class Matrix:
#include "Matrix.hh"
#include <cmath>
#include <iostream>
using namespace std;
// Default constructor: initializes the Matrix.
Matrix::Matrix() {
nrows=0;
ncols=0;
values=0;
}
// Initializes the Matrix with rows and cols.
Matrix::Matrix(int numberOfRows,int numberOfCols) {
nrows=numberOfRows;
ncols=numberOfCols;
values = new double[nrows*ncols] ;
for (int i = 0; i < nrows*ncols; i++) {
values[i] = 0;
}
}
// Copy constructor
Matrix::Matrix(Matrix &m)
{
nrows=m.getRows();
ncols=m.getCols();
int sizeOfM=m.getRows()*m.getCols();
values = new double[sizeOfM];
for (int i = 0; i < sizeOfM; ++i) {
values[i] = m.values[i];
}
}
// Destructor - Matrix allocates no dynamic resources.
Matrix::~Matrix() {
delete[] values;
// no-op
}
// Mutators:
void Matrix::setElem(int rowIndex,int colIndex,double val){
values[rowIndex*ncols+colIndex]=val; /*See Jasper i listen to what you say ! No return for a void function :) */
}
void Matrix::subtract(Matrix &m) {
for (int i = 0; i < nrows*ncols; i++) {
values[i] -=m.values[i];
}
}
void Matrix::add(Matrix &m) {
for (int i = 0; i < nrows*ncols; i++) {
values[i] +=m.values[i];
}
}
Matrix Matrix::multiply(Matrix &m) {
if(ncols!=m.getRows()){
cout << " Error :invalid matrix multiplication"<< endl;
Matrix ret(0,0);
return ret;
}
else{
Matrix ret(nrows,m.getCols());
for (int rowIndex = 0; rowIndex < ret.nrows; rowIndex++) {
for (int colIndex = 0; colIndex < ret.ncols; colIndex++) {
for(int count=0; count <= ncols; count++) {
ret.values[rowIndex*ncols+colIndex] +=(values[rowIndex*ncols+(count)]*m.values[count*ncols+colIndex]);
}
}
}
return ret;
}
}
// Accessors:
int Matrix::getRows() {
return nrows;
}
int Matrix::getCols() {
return ncols;
}
double Matrix::getElem(int rowIndex,int colIndex){
return values[rowIndex*ncols+colIndex];
}
bool Matrix::equals(Matrix &m) {
if(m.getRows()!=nrows||m.getCols()!=ncols){return false;}
for (int i = 0; i < nrows*ncols; i++) {
if(values[i] !=m.values[i]){return false;}
}
return true;
}
Apparently the problem is in multiply here is the test code
#include <iostream>
using namespace std;
#include "Matrix.hh"
// Have some fun playing with a Matrix!
int main(void) {
// Create a new matrix with two rows and three columns
Matrix a(2, 3);
// Print out some information about the size of this matrix:
cout << "Matrix a is " << a.getRows() << " rows tall." << endl;
cout << "Matrix a is " << a.getCols() << " columns wide." << endl;
// Print out the contents of this matrix (should be all zeroes!):
for(int r = 0; r < a.getRows(); r++) {
for(int c = 0; c < a.getCols(); c++) {
cout << "Element (" << r << ", " << c << ") = " << a.getElem(r,c) << endl;
}
}
// Fill in a few values
a.setElem(1, 2, -5280); // bottom right element is now -5280
a.setElem(0, 1, 123); // top center element is now 123
// Create an identical copy of this matrix
Matrix b = a;
// Change the original matrix some more
a.setElem(1, 2, 555); // bottom right element is now 555
// Examine some elements of each matrix
cout << "(1,2) of a = " << a.getElem(1,2) << " [should be 555]" << endl;
cout << "(1,2) of b = " << b.getElem(1,2) << " [should be -5280]" << endl;
// So if a and b are different, let's copy a into a new matrix and add b to it:
Matrix c = a;
c.add(b);
// Now let's copy c into another new matrix, d, and subtract a from it:
Matrix d = c;
d.subtract(a);
// Hmm... that means d should be b, no?
if (d.equals(b)) {
cout << "Yay! d = b!" << endl;
} else {
cout << "Uh-oh! Something went wrong, d isn't b!" << endl;
}
// Let's create a tiny 0 by 0 matrix using the default constructor:
Matrix e;
cout << "0x0 matrix e is " << e.getRows() << " by " << e.getCols() << endl;
// Of course, e and d are different, since they have different sizes!
if(!e.equals(d)) {
cout << "e and d are indeed different!" << endl;
} else {
cout << "Oh well, back to the drawing board...." << endl;
}
Matrix f(2,2);
f.setElem(0, 0, 2.0);
f.setElem(1, 0, 3.0);
f.setElem(0, 1, 5.0);
f.setElem(1, 1, 7.0);
Matrix g = f.multiply(a);
if (g.getElem(1, 2) == 3885.0) {
cout << "Multiply seems to work!" << endl;
} else {
cout << "Error in multiply() !" << endl;
}
// Okay, enough of this; destroy all those matrices and end the program:
return 0;
}
If i write
Matrix g = a ;
f.multiply(a);
The code compiles without any problem so it is a problem with this line: Matrix g =f.multiply(a);
Hello Everyone! I have a Project due tomorrow and I accidently chosen a hard one and now im regretting it. Im trying to make digital voting system on a small scale just to represent my idea as I'm a 2nd semester student of BSCS. I made the entire code by myself. The first code I made was working code but the output was not as I desired. I wanted to change the Vote after casting the vote. But it wasnt changing it. This is the first code Below:
#include<iostream>
#include<conio.h>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
double nic[4] = { {42401335},{1},{2},{3} };
int knum[4] = { {123},{1},{2},{3} };
int vote[4] = { {0},{0},{0},{0} };
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice <<endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Now I tried to remake the code and added file stream. taking text from external source instead of making array inside the visual studio and assigning values. but now Im facing an error which says This declaration has no storage class or type specifier The code is below:
#include<iostream>
#include<conio.h>
#include <fstream>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
fstream Text;
int nic[2];
int knum[2];
int vote[2];
int count = 0;
int ccnic = 0;
int knumm = 0;
int votee = 0;
Text.open("Nadra.txt")
{
while (!Text.eof())
{
Nadra >> ccnic >> knumm >> votee;
nic[count] = ccnic;
knum[count] = knumm;
vote[count] = votee;
count++;
}
}
Text.close();
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice << endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Can Someone please help??? Tomorrow is my last date! I just want my program to take data from text, chech the data with user entered data, IF correct, it will proceed and show for vote menu. if not, It will give error etc.
I know I'm a very pathetic learner. But You guys are professional! I will learn alot from you. Please help me!
I am using zybook, and it requires for me to create a program that maintains the inventory of a car rental agency.
The three files for this program are:
car.h - Class declaration
car.cpp - Class definition
main.cpp - main() function and other functions as described
When I wrote the program, I am getting an error:
main.cpp:4:10: fatal error: Car.h: No such file or directory
4 | #include "Car.h"
| ^~~~~~~
compilation terminated.
car.cpp:3:10: fatal error: Car.h: No such file or directory
3 | #include "Car.h"
| ^~~~~~~
compilation terminated.
Here is my code:
#include <iostream>
#include <string.h>
#include <vector>
#include "Car.h"
#include <algorithm>
using namespace std;
void addCar(vector<Car>& cars);
//bool deleteCar(vector<Car>& cars);
bool updateCarCondition(vector<Car>& cars);
void displayCar(vector<Car>& cars);
void displayAllCars(vector<Car>& cars);
bool rentCar(vector<Car>& cars);
bool returnCar(vector<Car>& cars);
int main(){
char option;
vector<Car> cars;
while (true){
cout << "CAR RENTAL AGENCY MENU" << endl;
cout << "a - Add car to inventory" << endl;
cout << "d - Delete car by id from inventory" << endl;
cout << "u - Update car by id condition in inventory" << endl;
cout << "s - Display one car by id from inventory" << endl;
cout << "i - Display list of all cars in inventory" << endl;
cout << "c - Rent a car by id in inventory" << endl;
cout << "r - Return a car by id in inventory" << endl;
cout << "q - Quit" << endl;
cout << "Choose an option: " << endl;
cin >> option;
cin.ignore();
cout << endl;
switch(option){
case 'a': addCar(cars);
break;
//case 'd': deleteCar(cars);
//break;
case 'u': updateCarCondition(cars);
break;
case 's': displayCar(cars);
break;
case 'i': displayAllCars(cars);
break;
case 'c': rentCar(cars);
break;
case 'r': returnCar(cars);
break;
case 'q': break;
default: cout << "Please enter a valid option" << endl;
}
}
return 0;
}
void addCar(vector<Car>& cars){
int id, year;
string model, make, condition;
cout << "ADD CAR TO INVENTORY" << endl;
cout << "Enter an ID: " << endl;
cin >> id;
cout << "Enter the make: " << endl;
cin >> make;
cout << "Enter the model: " << endl;
cin >> model;
cout << "Enter the year: " << endl;
cin >> year;
cout << "Enter the condition (new, slighty_used, used): " << endl;
cin >> condition;
Car car(id, make, model, year, condition);
cars.push_back(car);
}
/*bool deleteCar(vector<Car>& cars){
cout << "REMOVE CAR FROM INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to delete: " << endl;
cin >> id;
vector<Car>::iterator it;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
//Car const& const_car = cars.at(i);
//std::vector<Car>::iterator itr = std::find(cars.begin(), cars.end(), const_car);
it = cars.at(i);
cars.erase(it);
return true;
}
}
return false;
}*/
bool updateCarCondition(vector<Car>& cars){
cout << "UPDARE CAR CONDITION IN INVENTORY" << endl;
int id;
string condition;
cout << "Enter the ID of the car to update condition: " << endl;
cin >> id;
cout << "Enter the condition (new, slighty_used, used): " << endl;
cin >> condition;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
cars.at(i).Setcondition(condition);
return true;
}
}
return false;
}
void displayCar(vector<Car>& cars){
cout << "DISPLAY CAR IN INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to display: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
cars.at(i).displayCar();
break;
}
}
}
void displayAllCars(vector<Car>& cars){
cout << "DISPLAY ALL CARS IN INVENTORY" << endl;
for (int i = 0; i < cars.size(); i++){
cars.at(i).displayCar();
}
}
bool rentCar(vector<Car>& cars){
cout << "RENT CAR IN INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to rent: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
if (cars.at(i).Getrented()){
cout << "Car is already rented" << endl;
return false;
}
else if (cars.at(i).Getrented() == false){
cout << "Car has been successfully rented to you" << endl;
cars.at(i).toggleRented();
return true;
}
}
}
cout << "Car " << id << " not found in inventory" << endl;
return false;
}
bool returnCar(vector<Car>& cars){
cout << "RENT CAR TO INVENTORY" << endl;
int id;
cout << "Enter the ID of the car to return: " << endl;
cin >> id;
for (int i = 0; i < cars.size(); i++){
if (cars.at(i).Getid() == id){
if (cars.at(i).Getrented()){
cars.at(i).toggleRented();
cout << "Car returned successfully!!" << endl;
return true;
}
}
}
cout << "Car " << id << " not found in inventory" << endl;
return false;
}
i have to find the max and min value in array of random numbers... here is my code:
#include<iostream>
using namespace std;
void populateArray();
int findMin();
int findMax();
int computeTotal();
int arr[50];
int small, big;
void main()
{
big = small = arr[0];
populateArray();
findMin();
findMax();
computeTotal();
system("pause");
}
void populateArray()
{
for (int i = 0; i < 48; i++)
{
arr[i] = rand() % 1000;
cout << arr[i] << " ";
}
cout << endl;
}
int findMin()
{
for (int i = 0; i < 48; i++)
{
if (arr[i] < small)
{
small = arr[i];
}
cout << "The smallest number is " << small << endl;
return 0;
}
}
int findMax()
{
for (int i = 0; i < 48; i++)
{
if (arr[i] > big)
{
big = arr[i];
}
}
cout << "The biggest number is " << big << endl;
return 0;
}
int computeTotal()
{
int sum = 0;
sum = big + small;
cout << "Total= " << sum << endl;
return 0;
}
But the problem is that it does not display the minimum and maximum value from the array... it displays minimum value 0 and max 995 which are not included even in the array..
any help??
I have written a small bluetooth server and client progrem using winsock
I am not able to figure out why the client is not getting connected to the server. Both are running in different pcs and
both are paired through bluetooth.
The server code is
void server()
{
SOCKET server_socket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM), new_socket;
if (server_socket == INVALID_SOCKET)
{
cout << "socket creation failed...Error code : " << WSAGetLastError() << endl;
Sleep(2000);
return;
}
cout << "socket created" << endl;
SOCKADDR_BTH sa, sa2;
int channel = 0, len=sizeof(sa2);
memset(&sa, 0, sizeof(SOCKADDR_BTH));
sa.addressFamily = AF_BTH;
sa.port = channel & 0xff;
//bind
if (bind(server_socket, (SOCKADDR *)&sa, sizeof(sa)))
{
cout << "Binding failed...Error code : " << WSAGetLastError() << endl;
closesocket(server_socket);
Sleep(2000);
return;
}
cout << "binding done" << endl;
cout << "\nWaiting for client" << endl;
listen(server_socket, 3);
new_socket = accept(server_socket, (sockaddr *)&sa2, &len);
cout<<"connection accepted";
}
The client code is
void client()
{
SOCKET client_socket = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
int channel = 0;
BTH_ADDR bt_addr;
char* server_address = "34:02:86:26:c1:62";
if (client_socket == INVALID_SOCKET)
{
cout << "socket creation failed...Error code : " << WSAGetLastError() << endl;
Sleep(2000);
return;
}
cout << "socket created" << endl;
if (str2ba(server_address, &bt_addr) == 1)
{
cout << "address conversion error..." << endl;
Sleep(2000);
return;
}
SOCKADDR_BTH sa;
sa.addressFamily = AF_BTH;
sa.port = channel & 0xff;
sa.btAddr = bt_addr;
cout << "\nconnecting..." << endl;
if (connect(client_socket, (sockaddr *)&sa, sizeof(sockaddr)))
{
cout << "Error in connecting...Error code : " << WSAGetLastError() << endl;
closesocket(client_socket);
Sleep(2000);
return;
}
cout << "\nConnected" << endl;
Sleep(2000);
}
int str2ba(char *str_bt_addr, BTH_ADDR *bt_addr)//for converting string to bluetooth address
{
unsigned int addr[6];
if (sscanf_s(str_bt_addr, "%02x:%02x:%02x:%02x:%02x:%02x",
&addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6)
{
return 1;
}
*bt_addr = 0;
BTH_ADDR tmpaddr;
int i;
for (i = 0;i < 6;++i)
{
tmpaddr = (BTH_ADDR)(addr[i] & 0xff);
*bt_addr = ((*bt_addr) << 8) + tmpaddr;
}
return 0;
}
Why are these not getting connected? What am I missing?
Please help me.
Thanks in advance for any help.
In my short Bluetooth experience, the problem is normally somewhere in the SOCKADDR_BTH declarations.
I hard coded the MAC Address of each Endpoint: "38:2D:E8:B9:FA:EB" in Hex
RemoteEndPoint.btAddr = BTH_ADDR(0x382DE8B9FAEB);
Also make sure your Ports are the same on each Endpoint, I used:
RemoteEndPoint.port = 0;
and
LocalEndpoint.port = 0;
I have some code here: C++ WinSock Bluetooth Connection - AT Command - Error Received where I have an issue also.
Bluetooth is not as easy as some may think, thus the lack of answers received by the OP's