See here why: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=134
Boils down to C-style casts being static_casts or reinterpret_casts. But I never got reinterpret_cast to work anyway.
See here why: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=134
Boils down to C-style casts being static_casts or reinterpret_casts. But I never got reinterpret_cast to work anyway.
You can only do that when they are defined as constants, so MSVC++ can create the 2D arrays at compile time rather then at runtime.
I suggest reading how to make your own 2D arrays, or using vectors.
Is it a Windows handle, the one in windows.h? It's not an initializer, it's what int and char are: a type.
If it's a window handle, it's basically as specific as a pointer. A pointer used by windows to keep track of resources, varying from windows (window handles) to files (file handles).
Here's a bit more info on windows variables, including HANDLE: http://en.wikibooks.org/wiki/Windows_Programming/Handles_and_Data_Types
You can look all that info up in a STL List reference, as the one found here:
http://www.cplusplus.com/reference/stl/list/
See the "STL Algorithms" for sorting data inside STL containers.
what is this vector class you mentioned?
Short version: STL's resizable arrays.
Long version: I hope you realize I'm not superior to www.cplusplus.com or any reference on the STL when it comes to questions like this right? ;)
If you get stuck on vectors, create a new thread about vectors, but I don't think you'll get stuck on it. It's a great and easy to use class.
I know that's the problem, and MSVC++ is sometimes quite archaic when it comes to "C code". You can try and use the vector class? MSVC++ apparently can't create 2D "standard" arrays dynamically.
You can of course create the array yourself with new, see:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16
And bookmark that website. Lots of useful info.
What archaic compiler are you using? Hehe. Get GCC, it supports (practically) all the C++ features + a bit more.
... size_t is a standard type (~100% of the time it's an unsigned int)... that's weird. Could you copy and paste what your compiler is saying please? :)
size_t is:
http://www.cplusplus.com/reference/clibrary/cstring/size_t/
Does this code compile for you?
int main(){
size_t cols = 10;
cin >> cols;
size_t rows = 20;
cin >> rows;
char x[cols][rows];
for(int i = 0; i < rows; i++){
for(int n = 0; n < cols; n++){
cout << x[i][n];
}
cout << endl;
}
return 0;
}
Erm. I guess it can be compile in MSVC++. You can try to install MSVC++ Express and grab the platform SDK. Google is your friend. Can anybody else compile this code?
Example of inheritance using virtual functions:
#include <iostream>
using namespace std;
class base {
public:
virtual void func(){
cout << "First level: Base class" << endl;
}
};
class der1 : public base {
public:
virtual void func(){
cout << "Second level: Derived class" << endl;
}
};
class der2 : public der1 {
public:
virtual void func(){
cout << "Third level: Second derived class" << endl;
}
};
int main(){
base* all_your_base[3] = {new base, new der1, new der2};
for(int n = 0; n < 3; n++){
all_your_base[n]->func();
}
return 0;
}
class Client
{
public:
string name;
Menu order;
int table_number;
string type;
double bill;
Client(void)
{
name="";
table_number=0;
type="";
bill=0.0;
}
};
This code lacks the virtual addClient table wants to use. Another note: is a table a client? If not, you shouldn't have inheritance, but perhaps composition.
Errr, ignore that last part. Sometimes I'm unclear.
Student student;
for(int subject = 0; subject < 4; subject++){
string code;
int cu;
char pass_or_fail;
inFile >> code;
inFile >> cu;
inFile >> pass_or_fail;
//subject == loop variable
subjects[subject].setCode(code);
subjects[subject].setCU(cu);
subjects[subject].setPF(pass_or_fail);
}
I meant something like that. That's the "input" loop. Now, as you can see, this one is for subjects, but you should also create one for students for their variables. Same goes for output. If you don't know how, first write the code for reading in 1 student, then simply loop that. ;)
No, student already has 4 subjects: use those.
Student::Subjects::setVar();
That sucks. But still, just loop the student array then, one time for input, one time for output. So no more extra variables representing members in class Student! ;)
You could create a function that accepts a reference to a student and outputs it's data?
void output_student(Student &stud){
//put the output code here
}
//....
int main(){
//loop each of your students here, one loop for reading in the data, the other one for outputting the data
}
Totally wrong approach buddy. Create 4 students, read out their data.
Example code:
cout << students[0].getVar() << endl;
Kinda like that.
Good luck with the assignment, don't forget about the STL! (It has a queue, of course you can't use that, but it has plenty of other useful containers and algorithms).
If you're standing in queue line at the grocery shop, does the LINE look like a tree, with intersections and such, or more like an array of (possibly annoyed) customers?
I sure hope it doesn't look like a tree.
It doesn't compile in MinGW's GCC. Looks like MSVC code?
I'm getting these errors:
=|
sdvrp.h|18|warning: ignoring #pragma warning |
sdvrp.h|222|warning: no newline at end of file|
genius.cpp||In function `double genius(route*&, Nb&)':|
genius.cpp|732|warning: comparison between signed and unsigned integer expressions|
genius.cpp|733|warning: comparison between signed and unsigned integer expressions|
genius.cpp|736|error: no matching function for call to `std::vector<node*, std::allocator<node*> >::erase(node**)'|
vector.tcc|108|note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = node*, _Alloc = std::allocator<node*>]|
vector.tcc|120|note: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = node*, _Alloc = std::allocator<node*>]|
||=== Build finished: 1 errors, 4 warnings ===|
Maybe someone else can compile it tho.
Oh it does you no good, but you can't really check for !file, since file would be.. something. Although it "worked", maybe by accident.
Anyway, Could you hardcode the path, "C:\test.txt", and put the file up here? I'll test it.
Also, on what OS/compiler are you? Just to make sure.
Wait, doesn't the fstream constructor *always* create something? Check if file.good() is true.
If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).
Oh and, don't open it twice. In the constructor OR with fstream.open(), not both.
Hurr, it might be a really stupid "error".
One time (at band camp, xD) I was trying to open a file from an app I wrote in Code::Blocks on windows. So I put that file in project/bin/debug/, but code::blocks launched it in project/, so it couldn't find the file.
Try to open an absolute path like "C:\test.txt".
Yeah, but keep in mind he didn't design the functions himself, his prof did it for him. He needs to use these functions, unaltered, I think.
I accept all that you're saying there, and good point about the multi-core processors, though even they would not cause a problem in this case.
On my dualcore processor, 2 cores where in use and it indeed didn't give any errors as stated earlier. This is on Windows XP x64.
Okay, not 100% right. Darnit. xD
I've just created a single multithreaded program and it was (a dreaded task and) crashing constantly! So I ended up creating mutexes for every "group" of shared variables.
"Set pass/fail" I guess? Maybe the int is the subject indicator? Ugh, the variable names are horribly chosen. Just use that int as subject index and the char as.. P or F or something? Or grades? I dunno.
MrSpigot was 100% right, my bad. You don't need mutexes to write and read from one variable at the same time.
Tested with:
#include <iostream>
#include <windows.h>
using namespace std;
int victim = 10;
void thread()
{
for(int n = 0; n < 99999999; n++){
victim = n;
}
}
int main()
{
HANDLE thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL, 0, NULL);
HANDLE thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL, 0, NULL);
HANDLE thread3 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, NULL, 0, NULL);
WaitForSingleObject(thread1, INFINITE);
WaitForSingleObject(thread2, INFINITE);
WaitForSingleObject(thread3, INFINITE);
return 0;
}
main() is just reading it, so no protection is necessary in this case.
That's wrong, almost 100% sure, couldn't make sure with google, but it's just 2 threads trying to access the same memory: doesn't matter if they read or write, both will lead to errors.
Agreed on not using Sleep(). It's a useless function: wait for events to happen, don't guess how long such and such is going to take, and put that time in Sleep().
Sure, it looks fine, but you need to write it yourself if you want to learn C++, not somebody else.
I mean, do you know what this &*(array + k)
does? (It looks rather redundant...)
What doesn't work?
new doesn't set the allocated memory to 0, could that be a problem?
Be a bit more specific please.
Give it a shot here: http://www.cplusplus.com/doc/tutorial/
When you're stuck on a tutorial, you can ask for help of course.
Haha, no, that function of course needs to know what subject to set. Change it a bit, add a comment for your prof? The other functions do have such a parameter, so maybe he forgot?
// set the subject grade (pass or fail)
void Student::setSpf(char score,int grade)
{
}
How does that function know what subject it is supposed to work on?
xD Okay, sorry. I'd suggest sqrt() nonetheless, but it doesn't really matter.
To find the square root, you need to #include <cmath> and use the pow() function.http://www.cppreference.com/wiki/c/math/pow
He means the sqrt() function.
And, not related to programming, you're squaring it, not square rooting it.
A small addition:
Sometimes set functions do return something, and most of the time that's whether they succeeded or not.
Anyway, seeing the format, it's like this:
<name>, list(<subject>, <number>, <character>)
Make a function that separates those variables, so you can output them individually. If you can do that, then you can set them individually with those set functions handed to you.
while(!inFile.eof())
{
// use getline to read entire line
getline(inFile, s);
cout << s << endl;
}
That's wrong code. eof gets set when it has read eof, so and in that case getline() won't do a thing, so you'll output the last output twice (possibly).
Correct code:
while(getline(inFile, s) // use getline to read entire line
{
cout << s << endl;
}
From there, again, use the set functions. You do know what your set functions do right? :D Some of the classes functions are empty though, either get rid of them or make them useful.
An example
Student someStudent;
someStudent.setName("Anne");
cout << someStudent.getName() << endl;
while(firstTry == true)
{
{
cout << "It Is Your First Try, We Will Create My Array First:\n";
updateArr(arr, size);
}
return firstTry = false; //Don't return here, hehe.
}
Should be something along the lines of..
if(firstTry == true)
{
cout << "It Is Your First Try, We Will Create My Array First:\n";
updateArr(arr, size);
firstTry = false;
}
"... The unicode field is only used when UNICODE translation is enabled with SDL_EnableUNICODE. ... "
I suppose you did that, but below there is code on how to translate that code back to ASCII.
Buuuuttt as he also notes, if you're not using unicode, don't enable it. Why not grab the .sym instead and convert that? Hope it helps anyway.
Maybe make sure that conversion is going alright?
Output (char) (event.key.keysym.unicode) or something.
ASCII chars in UTF-8 might be something like, {\x00\x<ASCII-code>}, in that case, you're appending \x00 ;). It's my best guess.
newnode = new Node;
newnode = NULL;
Eerrrr? That's a bit of weird code. You're trying to derefence that pointer on the next line! ;)
Here are two examples I created and use as a reference, maybe they are of help:
/*
** Playing around with WinSockets
*/
#include <stdio.h>
#include <winsock.h>
int main (int argc, char **argv){
WORD wsaVersion;
wsaVersion = MAKEWORD(2,2);
WSADATA wsaData;
SOCKET sock1;
int wsaerr;
wsaerr = WSAStartup(wsaVersion, &wsaData);
if (wsaerr != 0){
printf("\nWinsock not found");
return -1;
}
printf("Winsock found");
sock1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock1 == INVALID_SOCKET){
printf("Initializing socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("\nSocket() works");
WSACleanup();
return 0;
}
#include <stdio.h>
#include <winsock2.h>
int main(int argc, char **argv){
WORD wsaVersion = MAKEWORD(2,2);
WSADATA wsaData;
int wsaerr;
wsaerr = WSAStartup(wsaVersion, &wsaData);
if (wsaerr){
printf("WSAStartup failed, exiting.");
return -1;
}
SOCKET somesock;
somesock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (somesock == INVALID_SOCKET){
printf("Socket() failed, exiting.");
WSACleanup();
return -1;
}
struct sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(12344);
if (bind(somesock, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR){
printf("Bind() failed, exiting.");
closesocket(somesock);
WSACleanup();
return -1;
}
closesocket(somesock);
WSACleanup();
return 0;
}
Here's are tiny server/client applications. I don't guarrantee they work or are the holy grail of winsock programming, but if they still work then they are good references. ;-)
basicServer.c
/*BasicServer.c*/
#include <stdio.h>
#include <winsock.h>
#include <string.h>
#define BUFFERSIZE 511
int main(int argc, char **argv){
if (argc < 3){
printf("Usage: %s <message to send to client> <ip to listen to>\n", argv[0]);
return -1;
}
SOCKET sock, accepsock = SOCKET_ERROR;
WSADATA wsaData;
WORD wsaVersion = MAKEWORD(2,2);
int sizeOf, wsaerr;
struct sockaddr_in sockinfo, clientinfo;
sockinfo.sin_family = …
First post!
Read the sticky at the top of the forum?
Nah I just meant to say that if you're sure that you'll only use it on your system, why not use those compiler specific functions? I think it's overkill to make basically everything you create cross platform, but if you're coding for your job then you should I guess... I'm a hobby coder. :)
I'm more of a newb but feeling cocky as always:
Don't AVOID compiler specific functions, but be sure to find a cross-compiler way to do it as well, as in: find it, use it a couple of time, memorize it.
However, I think it's a definite plus if you also know compiler specific tweaks.
Not in any imaginable way if you ask me. Like I said, a pointer to an array of pointers (an array of "arrays") resembles a 2D array. Unless you want that, there's no use for a double pointer.
You can always create the double pointer at any time by &students, so if the need would arise it has a simple solution.
It is only a pointer, don't worry. It's a pointer that points to pointers.
The struct declaration indeed only reserves one pointer, not an array or anything.
However, I guess you want to have *students in your struct. An "array" of students, right? That makes a bit more sense than what you have now (which resembles a 2D array of students).
hehe, you should access sc.students then, not students. ;)