|
As this is supposed to be a C++ class, the code should be using nullptr rather than the old Windows NULL definition. And what happens if numStudents is only 1?
|
|
|
|
|
Colour me not impressed! As long as you take the free option, I guess you didn't loose much, except maybe some time. Also there is the question of getting bad programming habits, but those can be fixed over time.
These days there are many free online course from very reputable sources like MIT OpenCourseWare | Free Online Course Materials[^]. You might want to take a look at some of them.
Mircea
|
|
|
|
|
|
And also by binary-file I mean the files with the weird characters and all.
Next, Here is a example of what it should look like:
read_4_bytes("file", 0);
Not much else to say...
|
|
|
|
|
1. Use fopen function to open the file
2. Use fseek function to go to specific offset
3. Use fread function to read the 4 bytes
4. Use fclose function to close the file
Mircea
|
|
|
|
|
One more detail - on Windows, you must open the file in "binary" mode using the O_BINARY flag (for open) or "rb" (for fopen).
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I am trying to catch a divide-by-zero error with the following code:
#include <iostream>
using namespace std;
int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}
}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception?
Thank you.
|
|
|
|
|
AFAIK, there's no way to do it.
You'll have to catch it manually and throw.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
I've not used it, but maybe take a look at Handling the Divide by Zero Exception in C++[^], similar to what Maximilien said.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
modified 5-Jan-24 13:08pm.
|
|
|
|
|
The article mentioned in the previous post does it by doing divisions in a function that checks for a zero divisor and throws an exception if it detects one. If you don't want to use such a function, it gets rather complicated.
If you're running on Windows, you need to handle the structured exception STATUS_FLOAT_DIVIDE_BY_ZERO or STATUS_INT_DIVIDE_BY_ZERO . If you're running on Linux, you need to handle the POSIX signal SIGFPE . You can also handle the use of a bad pointer with these techniques. The handler that receives the structured exception or POSIX signal can throw a C++ exception, which you can then catch in the usual way.
The following article goes into the details. But because it also describes a thread framework, you'll have to sift through it to extract the specific code that you need. In particular, see the sections "Receiving a Windows Structured Exception" and "Receiving a POSIX Signal".
Robust C++: Safety Net[^]
|
|
|
|
|
To me that was always the 'fun' part of C++ versus Java and C# - figuring out exactly what was being thrown in the first place.
|
|
|
|
|
In Windows, there are additional, O/S-level exceptions, that provide this sort of information. Look for "Structured Exception Handling". If you use MSVC, you may use the _set_se_handler function in main() and in the main function of each thread to convert Structured Exceptions to C++ exceptions.
There may be similar O/S- and compiler-specific methods in other environments.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I have the following file code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string path = "student/text/readpractice.txt";
try {
ifstream file;
file.open(path); if (!file) {
throw runtime_error("File failed to open.");
}
cout << file.rdbuf(); file.close();
}
catch (exception& e) {
cerr << e.what() << endl;
}
return 0;
}
Can someone help me understand how this line works:
if (!file) {
How can you use the exclamation point operator on what is probably a class?
Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded.
Any ideas?
Thanks.
|
|
|
|
|
|
Interesting.
I tried doing a "Step Into" on that line in the debugger, but it won't go to it.
If I do a "Go to Definition", it goes to this in xiosbase:
_NODISCARD bool __CLR_OR_THIS_CALL operator!() const noexcept {
return fail();
}
However, I put a breakpoint there, and it doesn't break.
|
|
|
|
|
I wrote the following code to output the Greek word "Δέλτα":
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
cout << "\u0394\u03AD\u03BB\u03C4\u03B1" << endl;
return 0;
}
For some reason, I get this error:
---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!
Program: ...sers\Owner\source\repos\assert_test\x64\Debug\assert_test.exe
File: minkernel\crts\ucrt\src\appcrt\lowio\write.cpp
Line: 659
Expression: buffer_size % 2 == 0
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Any ideas why the code is producing that error?
Thank you.
|
|
|
|
|
First things first, you're ignoring the return value of _setmode. If it's -1, the call failed, but you'll never know.
Second, after reading the documentation on _setmode and associated error links, it appears you cannot use cout with the mode of stdout switched to Unicode. You have to use wprintf instead:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
int r;
r =_setmode(_fileno(stdout), _O_U16TEXT);
if (r == -1)
perror("Cannot set mode");
wprintf(L"\u0394\u03AD\u03BB\u03C4\u03B1\n");
return 0;
}
CORRECTION: You CAN use COUT, but you must use the wide version of it, but note the L in front of the string being output. It MUST be there:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
int r;
r =_setmode(_fileno(stdout), _O_U16TEXT);
if (r == -1)
perror("Cannot set mode");
wcout << L"\u0394\u03AD\u03BB\u03C4\u03B1" << endl;
return 0;
}
|
|
|
|
|
I wrote this code:
class ChessPiece
{
};
int main()
{
cout << "size:" << sizeof(ChessPiece) << endl;
}
For some reason, it prints out 1 instead of 0. Anyone know why?
Thanks.
|
|
|
|
|
Probably because there has to be some minimum size of a class. You could study the C++ standard to see why.
|
|
|
|
|
Perhaps take a look at this FAQ.
Stroustrup: C++ Style and Technique FAQ[^]
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
That is interesting.
That page references a 'better' link. Same answer is there.
Standard C++[^]
Myself though if Stroustrop is the name then I would consider that better.
modified 2-Jan-24 12:43pm.
|
|
|
|
|
jschell wrote: if Stroustrop is the name then I would consider that better. Agreed.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Voluntarily removed - incorrect forum.
modified 1-Jan-24 23:53pm.
|
|
|
|
|
The << in this is not shift left, it is operator<<() operating on whatever QStringList() returns. It might be concatenating them, or appending them to an array, or whatever.
It can handle both char* and QString because it probably has overloads for both types, or possibly because QString has an operator for returning a char* .
I'm guessing this from the context because the C++ standard library also uses << this way - you would have to consult the QT documentation to be sure.
|
|
|
|
|
Thank you.
Appreciate your help.
Am I understanding it correctly - "<<" is pretty much like passing a parameter to a function ?
I am still not clear about "where -c " is passed to - is it an option to QStringList or "/bin/sh"?
I believe it is "passed to QStringList" since that is a second parameter to QProcess.
Perhaps now I can make more sense from QT docs, both - QProcess and QStringList.
I also need to look at the source of "sh"...
|
|
|
|