I want to show a message before an input :
cout<<"Enter a char:";
ch = getche();
But when running program it does not show the message and getche() works and after that the message is showed !!!
The whole program :
#include <iostream.h>
#include <conio.h>
int main(){
int ch;
cout<<"Enter a char:";
//cin.ignore();
ch = getche();
return 0;
}
The whole problem that, i want to do is this:
1- show a message
2- enter ONLY one char
3- do something immediately after entering the char
Adding endl soleved the problem :
cout<<"Enter a char:"<<endl;
ch = getche();
I have a code that depends on many external libraries that works fine in debug mode but it crashes in release mode.
When checking the root cause I found that there is a true condition that's evaluated to false
My code looks like
#include <iostream>
enum fruits
{
a, b, c
}
...
int main()
{
auto condition (fruits::a == 99);
std::cout << condition;
if (fruits::a == 99) std::cout << " FATAL ERROR ";
...
}
The progam outputs :
0 FATAL ERROR
The program is using c++20 with O2 optimisation flag
The issue is not present if I execute the code in a separated program.
I am testing running tasks through the HPC Pack Job Manager but it always fails:
task failed with exit code 1073741515
and the output is empty so no details are available.
Then I tried running simple codes. I started with this:
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
int y, x = 10;;
fin.open("number.txt");
fout.open("output.txt");
fout << "x * y = " << x*y << endl;
return 0;
}
and I thought maybe reading from files is the issue so let me try this:
int main()
{
int x;
x = 9;
return 0;
}
In my cluster I can only connect to the head node.
Please note that running any of my applications through cmd works perfectly in the head node and on the compute node (by running cmd in the network shared folder)
I tried putting my program file in a folder in c drive that I shared with HPCJobAdministrators, HPCJobOperators, HPCUsers and HPCAdminMirror. I also tried putting it in a compute node network shared folder. Still getting the same error.
I have tried to install CGAL, maybe I installed it correctly but I don't know how to create a project Hello World in VS.
When I pasted some lines of code to file .cpp CGAL project:
#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
int main()
{
Point_2 points[5] = { Point_2(0, 0), Point_2(10, 0), Point_2(10, 10), Point_2(6, 5), Point_2(4, 1) };
Point_2 result[5];
Point_2 *ptr = CGAL::convex_hull_2(points, points + 5, result);
std::cout << ptr - result << " points on the convex hull:" << std::endl;
for (int i = 0; i < ptr - result; i++){
std::cout << result[i] << std::endl;
}
return 0;
}
I built this code with no error, But when I ran this code in Release mode here the error: Unable to start the program :'C:\CGAL\lib\Release\libCGAL-vc120-mt-45.lib'
In Debug mode: Unable to start the program :'C:\CGAL\lib\Debug\libCGAL-vc120-mt-gd-45.lib'
Please tell me how to fix it, thanks.
Did you successfully build the library?
It is explained here in Building CGAL section. It then generates the lib files such as C:\CGAL\lib\Release\libCGAL-vc120-mt-45.lib. If it generates the .lib files with a different name (libCGAL-vc-120-mt-45.lib instead of vc120 for instance) try renaming the .lib file with the libCGAL-vc120-mt-45.lib (it worked for me).
While testing some functionality with std::thread, a friend encountered a problem with GCC and we thought it's worth asking if this is a GCC bug or perhaps there's something wrong with this code (the code prints (for example) "7 8 9 10 1 2 3", but we expect every integer in [1,10] to be printed):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <thread>
int main() {
int arr[10];
std::iota(std::begin(arr), std::end(arr), 1);
using itr_t = decltype(std::begin(arr));
// the function that will display each element
auto f = [] (itr_t first, itr_t last) {
while (first != last) std::cout<<*(first++)<<' ';};
// we have 3 threads so we need to figure out the ranges for each thread to show
int increment = std::distance(std::begin(arr), std::end(arr)) / 3;
auto first = std::begin(arr);
auto to = first + increment;
auto last = std::end(arr);
std::thread threads[3] = {
std::thread{f, first, to},
std::thread{f, (first = to), (to += increment)},
std::thread{f, (first = to), last} // go to last here to account for odd array sizes
};
for (auto&& t : threads) t.join();
}
The following alternate code works:
int main()
{
std::array<int, 10> a;
std::iota(a.begin(), a.end(), 1);
using iter_t = std::array<int, 10>::iterator;
auto dist = std::distance( a.begin(), a.end() )/3;
auto first = a.begin(), to = first + dist, last = a.end();
std::function<void(iter_t, iter_t)> f =
[]( iter_t first, iter_t last ) {
while ( first != last ) { std::cout << *(first++) << ' '; }
};
std::thread threads[] {
std::thread { f, first, to },
std::thread { f, to, to + dist },
std::thread { f, to + dist, last }
};
std::for_each(
std::begin(threads),std::end(threads),
std::mem_fn(&std::thread::join));
return 0;
}
We thought maybe its got something to do with the unsequenced evaluation of function's arity or its just the way std::thread is supposed to work when copying non-std::ref-qualified arguments. We then tested the first code with Clang and it works (and so started to suspect a GCC bug).
Compiler used: GCC 4.7, Clang 3.2.1
EDIT: The GCC code gives the wrong output with the first version of the code, but with the second version it gives the correct output.
From this modified program:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <thread>
#include <sstream>
int main()
{
int arr[10];
std::iota(std::begin(arr), std::end(arr), 1);
using itr_t = decltype(std::begin(arr));
// the function that will display each element
auto f = [] (itr_t first, itr_t last) {
std::stringstream ss;
ss << "**Pointer:" << first << " | " << last << std::endl;
std::cout << ss.str();
while (first != last) std::cout<<*(first++)<<' ';};
// we have 3 threads so we need to figure out the ranges for each thread to show
int increment = std::distance(std::begin(arr), std::end(arr)) / 3;
auto first = std::begin(arr);
auto to = first + increment;
auto last = std::end(arr);
std::thread threads[3] = {
std::thread{f, first, to},
#ifndef FIX
std::thread{f, (first = to), (to += increment)},
std::thread{f, (first = to), last} // go to last here to account for odd array sizes
#else
std::thread{f, to, to+increment},
std::thread{f, to+increment, last} // go to last here to account for odd array sizes
#endif
};
for (auto&& t : threads) {
t.join();
}
}
I add the prints of the first and last pointer for lambda function f, and find this interesting results (when FIX is undefined):
**Pointer:0x28abd8 | 0x28abe4
1 2 3 **Pointer:0x28abf0 | 0x28abf0
**Pointer:0x28abf0 | 0x28ac00
7 8 9 10
Then I add some code for the #ELSE case for the #ifndef FIX. It works well.
- Update: This conclusion, the original post below, is wrong. My fault. See Josh's comment below -
I believe the 2nd line std::thread{f, (first = to), (to +=
increment)}, of threads[] contains a bug: The assignment inside the
two pairs of parenthesis, can be evaluated in any order, by the
parser. Yet the assignment order of 1st, 2nd and 3rd argument of the
constructor needs to keep the order as given.
--- Update: corrected ---
Thus the above debug printing results suggest that GCC4.8.2 (my version)
is still buggy (not to say GCC4.7), but GCC 4.9.2 fixes this bug, as
reported by Maxim Yegorushkin (see comment above).