Fast input for bool data type in c++
I have read in my text book that we should prefer scanf/printf instead of cin/cout for fast input because cin/cout use function overloading. So why can't i use scanf for bool data type? Is there any other way for fast input in bool?
See also questions close to this topic

c++ subclass access to base class
I am currently studying c++ and I have a question. Inside the concept of inheritance, I understand that the subclass has the access to base class's data and method. But if the Base class implements an interface, can the subclass access or use the methods defined in the interface that is implemented in the Base class?

c++  creating a thread for a class function referenced by a unique pointer
The original source is as below :
for (auto &core : cores) core>Run();
Member "cores" has the structure like below :
std::vector<std::unique_ptr<Core>> cores;
I tried to make the code above multithreaded by changing the "for" loop :
for(auto &core : cores){ threads[i] = std::thread(core>Run, this); i++; } i = 0; for (auto &core : cores){ threads[i].join(); i++; }
I get the following error :
error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, x86::Cpu* const)’ threads[i] = std::thread(core>Run, this);
(P.S. core>Run() is implemented in x86::Cpu::Run(), core>Run() is a method in a class x86::Core::Run())
I know what I did was kind of stupid, but I'm fairly new to using threads, so I'm having a hard time finding the right way to use these functions or understanding them at all.
I'm trying to make each core>Run() function run in a separate thread, but I'm getting confused of how to use the std::thread when the function is inside a vector of a unique_ptr, and I'm finding it hard to reference the method in the right way
I think my situation is kind of unique, I could not find anyone asking a question similar to mine

variable is not a type name in C++
Here is my
Triangle.h
:#pragma once #ifndef _TRIANGLE_H_ #define _TRIANGLE_H_ #include <iostream> #include "Point.h" #include "IDReporter.h" class Point; class Triangle : public IDReporter { public: class Segment { private: Point& _a; Point& _b; public: Segment(const Point&, const Point&); const Point& start(); const Point& end(); }; Triangle( const double& x1 = 0, const double& y1 = 0, const double& x2 = 1, const double& y2 = 0, const double& x3 = 0, const double& y3 = 1 ); Triangle(const Point&, const Point&, const Point&); Triangle(const Point& a, const Triangle::Segment& _bc); Triangle (const Triangle&); ~Triangle(); Triangle& operator=(const Triangle&); double perimeter() const; double area() const; const Point& apexA() const; const Point& apexB() const; const Point& apexC() const; const Triangle::Segment& side_a() const; const Triangle::Segment& side_b() const; const Triangle::Segment& side_c() const; double length_a() const; double length_b() const; double length_c() const; const Triangle::Segment height_a() const; const Triangle::Segment height_b() const; const Triangle::Segment height_c() const; /*const Segment& height_a() const; const Segment& height_b() const; const Segment& height_c() const;*/ private: static unsigned _quantity; static unsigned _freeId; int _id; Point _a, _b, _c; Triangle::Segment* _ab; Triangle::Segment* _bc; Triangle::Segment* _ca; }; std::ostream& operator<<(std::ostream&, const Triangle&); #endif
Here is my
Triangle.cpp
:#include "Triangle.h" #include "Segment.h" #include "Point.h" unsigned Triangle::_quantity = 0; unsigned Triangle::_freeId = 0; Triangle::Segment(const Point& a, const Point& b) : _a(Point(a)), _b(Point(b))) {} const Point& Triangle::Segment::start() { return _a; } const Point& Triangle::Segment::end() { return _b; } Triangle::Triangle( const double& x1, const double& y1, const double& x2, const double& y2, const double& x3, const double& y3) : _a(x1, y1), _b(x2, y2), _c(x3, y3), _id(++_freeId) {} Triangle::Triangle(const Point& a, const Point& b, const Point& c) : _a(a), _b(b), _c(c), _id(++_freeId) {} Triangle::Triangle(const Point& a, const Triangle::Segment& _bc) : _a(a), _b(Point(_bc.start())), _c(Point(_bc.end())) {} Triangle::~Triangle(void) { }
Here is my
Point.h
://Every point posesses its unique ID. //In debug mode both constructor and destructor //report the ID and the coordinates //of a point created or resp. deleted #pragma once #ifndef _POINT_H_ #define _POINT_H_ #include <iostream> class Point { private: static int _quantity; static int _freeID; const int _pointID; double _x; double _y; public: Point(double x = 0, double y = 0); Point(const Point &); ~Point(); Point& operator=(const Point&); Point& operator+=(const Point&); const bool operator!=(const Point&) const; const double& x() const; const double& y() const; double& x(); double& y(); const int getID() const; static int amount(); static int activeAmount(); }; std::ostream& operator<<(std::ostream&, const Point&); const Point operator+(const Point&, const Point&); const bool operator==(const Point&, const Point&); void reportPointIfDebug(const Point&, const char* const); #endif
Here is my
Point.cpp
:#include "Point.h" #include <typeinfo> int Point::_quantity = 0; int Point::_freeID = 0; Point::Point(double x, double y) : _pointID(++_freeID), _x(x), _y(y) { reportPointIfDebug(*this, "created: " ); ++_quantity; } Point::Point(const Point& p) : _pointID(++_freeID), _x(p.x()), _y(p.y()) { reportPointIfDebug(*this, "created: " ); ++_quantity; } Point::~Point(void) { reportPointIfDebug(*this, "deleted: " ); _quantity; } Point& Point::operator=(const Point& p) { _x = p.x(); _y = p.y(); return *this; } Point& Point::operator+=(const Point& v) { _x += v.x(); _y += v.y(); return *this; } const bool Point::operator!=(const Point& v) const { return (_x != v.x())  (_y != v.y()); } double& Point::x() { return _x; } double& Point::y() { return _y; } const double& Point::x() const { return _x; } const double& Point::y() const { return _y; } const int Point::getID() const { return _pointID; } int Point::amount() { return _freeID; } int Point::activeAmount() { return _quantity; } std::ostream& operator<<(std::ostream& out, const Point& p) { out << "(" << p.x() << ", " << p.y() << ")"; return out; } const Point operator+(const Point& u, const Point& v) { Point res(u); res += v; return res; } const bool operator==(const Point& u, const Point& v) { return !(u != v); } void reportPointIfDebug(const Point& p, const char* const message) { #ifndef NDEBUG std::cout << typeid(p).name() << " " << message << p << " id: " << p.getID() << std::endl; #endif }
Here is my 'IDReporter.h`:
#pragma once #ifndef _ID_REPORTER_H_ #define _ID_REPORTER_H_ #include <iostream> class IDReporter { public: virtual ~IDReporter() = 0; virtual const int& id() const = 0; virtual std::ostream& output(std::ostream&) const = 0; }; #endif
Here is my
IDReporter.cpp
:#include "IDReporter.h" IDReporter::~IDReporter(void) {}
Here is the error message when I try to compile
Triangle.cpp
:1> Build started: Project: 04Trofimov, Configuration: Debug Win32  1> Triangle.cpp 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(8): error C2226: syntax error : unexpected type 'Point' 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(9): error C4430: missing type specifier  int assumed. Note: C++ does not support defaultint 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(9): error C2059: syntax error : ')' 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(10): error C4430: missing type specifier  int assumed. Note: C++ does not support defaultint 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(10): error C2143: syntax error : missing ';' before '{' 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(10): error C2447: '{' : missing function header (oldstyle formal list?) 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(31): error C2662: 'Triangle::Segment::start' : cannot convert 'this' pointer from 'const Triangle::Segment' to 'Triangle::Segment &' 1> Conversion loses qualifiers 1>c:\users\oobarbazanoo\desktop\cpp\04trofimov\04trofimov\triangle.cpp(31): error C2662: 'Triangle::Segment::end' : cannot convert 'this' pointer from 'const Triangle::Segment' to 'Triangle::Segment &' 1> Conversion loses qualifiers ========== Build: 0 succeeded, 1 failed, 0 uptodate, 0 skipped ==========
When I hover over any
Point
class name used in theTriangle.h
orTriangle.cpp
or even in thePoint.h
outside of the class declaration I am getting the following message:Error: variable "Point" is not a type name.
How can I get rid from the error?

Sparse boolean matrix multiplication
Does anybody know the efficient implementation of sparse boolean matrix multiplication? I'm interested in both CPU and GPGPU implementations because it is necessary to multiply matrices of different sizes (from 8x8 to up to 10^8x10^8). Currently, I use cuSPARSE library, but it supports only numerical matrices (float, double etc) and this fact leads to huge overhead (by memory and time) which is critical in my task.

How do you code a boolean variable when using True and False values?
I am coding in Cloud 9, to keep in mind, you might have to code it a certain way for their software.

R  TRUE/FALSE calling function not the same as stepbystep evaluation
Working in R.
The goal is that by handing R a set of 8 numerical values that I get back TRUE or FALSE according to whether the line segment defined by the first 4 values intersects with the line segment defined by the final 4 values.
The input is in the form (x1,x2,y1,y2) for the first line segment and the same for the second line segment. This is a peculiarity of the storage system. The actual points of the line segment are (x1,y1) and (x2,y2)
So, for example:do_line_segments_intersect(0,2,0,2,0,2,2,0)
should return TRUE because the line segment from (0,0) to (2,2) intersects the line segment from (0,2) to (2,0)
anddo_line_segments_intersect(1,0,0,1,0,1,0,1)
should return FALSE because the line segment from (1,0) to (0,1) does not intersect with the line segment from (0,0) to (1,1)
Here's the code that I'm using.find_line_equation < function(x1,x2,y1,y2) { A < y1  y2 B < x2  x1 C = A*x1 + B*y1 return_list = list(A,B,C) return(return_list) } find_the_intersection_point < function(list_1, list_2) { coefficient_matrix = matrix(c(list_1[1], list_1[2], list_2[1],list_2[2]), nrow = 2, ncol = 2, byrow = TRUE) coefficient_matrix < as.numeric(coefficient_matrix) coefficient_matrix = matrix(coefficient_matrix, nrow =2, ncol = 2, byrow = FALSE) RHS_matrix = matrix(c(list_1[3], list_2[3]), nrow = 2, ncol = 1, byrow = FALSE) RHS_matrix < as.numeric(RHS_matrix) RHS_matrix = matrix(RHS_matrix, nrow =2, ncol = 1, byrow = FALSE) inverse_coefficient_matrix = solve(coefficient_matrix) solution = inverse_coefficient_matrix %*% RHS_matrix return(solution) } do_line_segments_intersect < function(ax1,ax2,ay1,ay2,bx1,bx2,by1,by2) { a_coefficients < find_line_equation(ax1,ax2,ay1,ay2) b_coefficients < find_line_equation(bx1,bx2,by1,by2) intersection_point < find_the_intersection_point(a_coefficients, b_coefficients) #find the boundaries of the line segments. This will let us check that our intersection is within the boundaries. max_x < max(ax1,ax2) min_x < min(ax1,ax2) max_y < max(ay1,ay2) min_y < min(ay1,ay2) #Is the intersection within the boundaries? if(findInterval(intersection_point[1],c(min_x,max_x)) && findInterval(intersection_point[2],c(min_y,max_y))) { print("true1") } else { print("false1") } }
I know the code is not complete. My comparison to the max and min values should include all of the points (hence, coordinate values) rather than just half of them. And ultimately I want to return the actual boolean value rather than just a text string indicating what the value is.
The issue I'm having is the following:
If I run the code step by step on the input coordinates (1,0,0,1) for a_coefficients and (0,1,0,1) for b_coefficients and then run each line onebyone then the final result is the "false" that I'm expecting.
However, if I call the function with the input like thisdo_line_segments_intersect(1,0,0,1,0,1,0,1)
then R returns "true".
Why is it doing this?
And, more importantly, how do I fix it?

Segmentation fault, scanf
We have an assignment which is to take characters from a file, shift it a given value to the right(it'll make sense in the code) and then store that new value in a new file, but I seem to be running into a segmentation fault, which as far as I know means I'm trying to access memory outside of the memory I have been allocated? I'm very new to C and I managed to debug this code up until this point and I honestly don't know where to go. I don't even quite understand what the issue is.
#include<stdio.h> //Get Shift amount //Get ifilename //Get ofilename //open them //Get characters one at a time from input //Process and shift by shift amount int main() { int i;//loop value char a[62]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//Variable with every value int sa = 0;//Store Shift value char ofile[30];//contain the name of output file char ifile[30];//contain name of input file char value;//Value to keep the current value in printf("How far in ascii values would you like to shift?\n"); scanf("%i", sa);//Get shift printf("What is the name of your input file located in this directory?"); scanf("%s", ifile);//get input name printf("What would you like to name your new file?\n Do note that it will overwrite the current file named this!"); scanf("%s", ofile);//Get output name FILE *oIfile = fopen(ifile, "r"), *oOfile = fopen(ofile, "w"); while(value = fscanf(oIfile, "%c", value) != EOF)//Check to ensure that you never reach the end of the file { for(i=0; i<62; i++)//loop through the list of all characters { if(value == a[i])//check to see if the value from the input file matches with which letter { value = a[i+sa] % 62;//incrase the value by the shift amount, check if its longer than 62, add remainder break;//break the for loop so we can restart the while loop with the next letter } } fprintf(oOfile, "%c");//print the new value to the output file } fclose(oIfile);//close input file fclose(oOfile);//close output file }
is this issue due to my approach to scanf?

Reading certain length string from file with fscanf C
How do you read a string of a length defined by a variable? I have the code:
int main (int argc, char **argv) { FILE *fp; if ((*fp = fopen(fileName, "r")) == NULL) { fprintf(stderr, "Unable to read file: %d: %s\n", errno, strerror(errno)); return 1; } char string[81]; int len = 12; if (fscanf(*fp, " %*s ", len, string) == 1 { /* do something */ } return 0; }
But C throws an error for the
fscanf()
line statingwarning: too many arguments for format [Wformatextraargs]
Is it not possible to use variable for length (using the * wildcard notation) with
fscanf()
or am I doing it wrong? 
c  Trouble with scanf
So I'm having some trouble formatting my scanf call to fit my input. Basically, I want to take an input of a string (maximum 32 characters) along with 2 integers (all separated by spaces). Currently, my code is:
scanf("%32s", str); scanf("%*s %d %d", &first, &last);
I added the %*s in order to ignore any possible characters after the 32 I'm reading in. For some reason this %*s ignores the first number and reads the 2nd number from the input into &last. I'm not sure exactly how to fix this, any help would be appreciated.