Clockowl 56 Posting Whiz

What do you think the code is doing? Add some comments.

lancevo3 commented: Very helpful in attempting to solve my issues. +1
Clockowl 56 Posting Whiz

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. ;)

Clockowl 56 Posting Whiz

Hey guys,

I was wondering, with this code:

main.cpp

#include <iostream>
using namespace std;

class car {
public:
    car (float speed) :
            speed(speed) {}

    car () :
            speed(0) {}

    void cruise(float speed) {
        this->speed = speed;
        cout << "New speed: " << getSpeed() << endl;
    }

    void brake(float power) {
        this->speed -= power*power/4;
    }

    float getSpeed() {
        return speed;
    }

private:
    float speed;
};

class racer : public car {
public:
    void boost(float power) {
        cout << "BOOST! ";
        cruise(getSpeed() + power*power/3);
    }
};

class tank : public car {
public:
    bool shoot(float aimTime) {
        cout << "Shot ";
        if (aimTime > 5.0) {
            cout << "hits!" << endl;
            return true; //hit!
        } else {
            cout << "misses!" << endl;
            return false; //miss!
        }
    }

};

class racetank : public racer, public tank {
public:
    bool boostShoot(float power, float aimTime) {
        boost(power*2);
        return shoot(aimTime*2);
    }
};

int main() {
    racetank mycar;
    mycar.car::cruise(50);
    mycar.boost(20);
    mycar.car::brake(5);
    mycar.boostShoot(35, 1.4);

    return 0;
}

MinGW's GCC gives me these errors:

D:\MyDocs\Code\InheritThis\main.cpp||In function `int main()':|
D:\MyDocs\Code\InheritThis\main.cpp|62|error: `car' is an ambiguous base of `racetank'|
D:\MyDocs\Code\InheritThis\main.cpp|64|error: `car' is an ambiguous base of `racetank'|

How come the compiler still complains about ambiguity?

It knows the function is not overloaded (?)
It knows the function is not virtual.

Can't the compiler be sure that when I call the function like that, it's the same function?

Thanks in advance,

PS:
I know the solution to this is to use virtual inheritance, …

NicAx64 commented: hmm nice and smart bug , I think I find something new +1
Clockowl 56 Posting Whiz

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 = …
Salem commented: Looks good. +26
Clockowl 56 Posting Whiz

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).

Clockowl 56 Posting Whiz

By the way blrjohn, you could also try and fire up a console and execute your program from there. It won't disappear then. I dunno on which OS you are, but guessing it's Windows (80% market share or something eh):

Go to:

Start -> Run

type in "cmd"

Hit enter.

Behold the almighty black screen with often blinking cursor! :D

Typing in "cd" and then a path will let you change your directory. Type in "help" if you're in need of more help. Mastering the console is pretty convenient for developers.

Salem commented: This is how it should be done. +20
Clockowl 56 Posting Whiz

That's a C++ program btw, not C.

To make it C:

#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <stdio.h>
#include <math.h>

int main()
{
    HWND console = GetConsoleWindow();
    HDC dc = GetDC(console);
    int pixel = 0;
    unsigned int loopcounter = 0;
    unsigned int loopmax = 126;
    COLORREF C1= RGB(255,0,0); //red
    double i = 0;

    for (loopcounter = 0; loopcounter < loopmax; loopcounter++)
    {
        SetPixel(dc,pixel,(int)(100+50*sin(i)),C1);
        pixel++;
        i += 0.05;
    }
    ReleaseDC(console, dc);
    getchar();
    return 0;
}

And link it with gdi32.

Nick Evan commented: Thanks for the heads up! +6
Clockowl 56 Posting Whiz

Not to be an asshole but poster says he wants to capture and store image data from a webcam. That's pretty specific, not?

jephthah commented: haha +2