Tuesday, February 28, 2017

MidTerm Review Spring 2017

9 t/f
20 multiple choice
char  extraction operator (>>) how it behaves.  
flag control while loop do you use bool value? yes
while loop ( ) <----- div="">
how many times does a for loop execute/iterate. for(int i = 1; i<5 4="" div="" false="" i="" is="" so="" times.="" whenever="">
know pre and post  ++x(pre)/x++(post)
o ex.1
o x = 5;
o x++ = 5 //x = 6
o ++x=  7
o ex.2 a = 1;
o b = 0;
o b = ++a;
o a = 2;
o b = 2 ;
o -------------
o b = a++;
o b = 1;
prototyping a function:
#include
using namespace std;
void protoTypedFunction(int,int);   the prototype
know what an infinite loop is:
o ex: while(true)
A while loop tell the output of it.
Part of dev environment combines our pro./prog. from - assembler linker, decoder, complier.
Know this something:
o cin >> x >> y;
o stdin: 5, 6;  x = 5 y = 6
interger division
19 / 7 =  2
length of a string.
o string name = "Billy" 
o length = 5

cout << pow(pow(num, num), number) << endl;
o pow = power operator from cmath/math.h.
o pow(9,3) = 9^3 =  9 * 9 * 9
Pre increment operation example. Break it down into 2 steps.
Ifstream <--- 160.="" a="" div="" file.="" opening="" page="">
Ascii has 128 characters
o Other one has 255 characters.
o Unicode has 65535 characters

Tuesday, November 15, 2016

WinSock Server

// WinSockServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#undef UNICODE

#define WIN32_LEAN_AND_MEAN

#include 
#include 
#include 
#include 
#include 

// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(void)
{
 WSADATA wsaData;
 int iResult;

 SOCKET ListenSocket = INVALID_SOCKET;
 SOCKET ClientSocket = INVALID_SOCKET;

 struct addrinfo *result = NULL;
 struct addrinfo hints;

 int iSendResult;
 char recvbuf[DEFAULT_BUFLEN];
 int recvbuflen = DEFAULT_BUFLEN;

 // Initialize Winsock
 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
 if (iResult != 0) {
  printf("WSAStartup failed with error: %d\n", iResult);
  return 1;
 }

 ZeroMemory(&hints, sizeof(hints));
 hints.ai_family = AF_INET;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;
 hints.ai_flags = AI_PASSIVE;

 // Resolve the server address and port
 iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
 if (iResult != 0) {
  printf("getaddrinfo failed with error: %d\n", iResult);
  WSACleanup();
  return 1;
 }

 // Create a SOCKET for connecting to server
 ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
 if (ListenSocket == INVALID_SOCKET) {
  printf("socket failed with error: %ld\n", WSAGetLastError());
  freeaddrinfo(result);
  WSACleanup();
  return 1;
 }

 // Setup the TCP listening socket
 iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
 if (iResult == SOCKET_ERROR) {
  printf("bind failed with error: %d\n", WSAGetLastError());
  freeaddrinfo(result);
  closesocket(ListenSocket);
  WSACleanup();
  return 1;
 }

 freeaddrinfo(result);

 iResult = listen(ListenSocket, SOMAXCONN);
 if (iResult == SOCKET_ERROR) {
  printf("listen failed with error: %d\n", WSAGetLastError());
  closesocket(ListenSocket);
  WSACleanup();
  return 1;
 }

 // Accept a client socket
 ClientSocket = accept(ListenSocket, NULL, NULL);
 if (ClientSocket == INVALID_SOCKET) {
  printf("accept failed with error: %d\n", WSAGetLastError());
  closesocket(ListenSocket);
  WSACleanup();
  return 1;
 }

 // No longer need server socket
 closesocket(ListenSocket);
 
 // Receive until the peer shuts down the connection
 do {

  iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
  if (iResult > 0) {
   printf("Bytes received: %d\n", iResult);

   // Echo the buffer back to the sender
   iSendResult = send(ClientSocket, recvbuf, iResult, 0);
   iSendResult = send(ClientSocket, recvbuf, iResult, 0);
   if (iSendResult == SOCKET_ERROR) {
    printf("send failed with error: %d\n", WSAGetLastError());
    closesocket(ClientSocket);
    WSACleanup();
    return 1;
   }
   printf("Bytes sent: %d\n", iSendResult);
  }
  else if (iResult == 0)
   printf("Connection closing...\n");
  else {
   printf("recv failed with error: %d\n", WSAGetLastError());
   closesocket(ClientSocket);
   WSACleanup();
   return 1;
  }

 } while (iResult > 0);

 // shutdown the connection since we're done
 iResult = shutdown(ClientSocket, SD_SEND);
 if (iResult == SOCKET_ERROR) {
  printf("shutdown failed with error: %d\n", WSAGetLastError());
  closesocket(ClientSocket);
  WSACleanup();
  return 1;
 }

 // cleanup
 closesocket(ClientSocket);
 WSACleanup();

 return 0;
}

WinSock Client

// WinSockClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN

#include 
#include 
#include 
#include 
#include 


// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")


#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(int argc, char **argv)
{
 WSADATA wsaData;
 SOCKET ConnectSocket = INVALID_SOCKET;
 struct addrinfo *result = NULL,
  *ptr = NULL,
  hints;
 char *sendbuf = "this is a test";
 char recvbuf[DEFAULT_BUFLEN];
 int iResult;
 int recvbuflen = DEFAULT_BUFLEN;

 // Validate the parameters
 if (argc != 2) {
  printf("usage: %s server-name\n", argv[0]);
  return 1;
 }

 // Initialize Winsock
 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
 if (iResult != 0) {
  printf("WSAStartup failed with error: %d\n", iResult);
  return 1;
 }

 ZeroMemory(&hints, sizeof(hints));
 hints.ai_family = AF_UNSPEC;
 hints.ai_socktype = SOCK_STREAM;
 hints.ai_protocol = IPPROTO_TCP;

 // Resolve the server address and port
 iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
 if (iResult != 0) {
  printf("getaddrinfo failed with error: %d\n", iResult);
  WSACleanup();
  return 1;
 }

 // Attempt to connect to an address until one succeeds
 for (ptr = result; ptr != NULL;ptr = ptr->ai_next) {

  // Create a SOCKET for connecting to server
  ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
   ptr->ai_protocol);
  if (ConnectSocket == INVALID_SOCKET) {
   printf("socket failed with error: %ld\n", WSAGetLastError());
   WSACleanup();
   return 1;
  }

  // Connect to server.
  iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  if (iResult == SOCKET_ERROR) {
   closesocket(ConnectSocket);
   ConnectSocket = INVALID_SOCKET;
   continue;
  }
  break;
 }

 freeaddrinfo(result);

 if (ConnectSocket == INVALID_SOCKET) {
  printf("Unable to connect to server!\n");
  WSACleanup();
  return 1;
 }

 // Send an initial buffer
 iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
 if (iResult == SOCKET_ERROR) {
  printf("send failed with error: %d\n", WSAGetLastError());
  closesocket(ConnectSocket);
  WSACleanup();
  return 1;
 }

 printf("Bytes Sent: %ld\n", iResult);

 // shutdown the connection since no more data will be sent
 iResult = shutdown(ConnectSocket, SD_SEND);
 if (iResult == SOCKET_ERROR) {
  printf("shutdown failed with error: %d\n", WSAGetLastError());
  closesocket(ConnectSocket);
  WSACleanup();
  return 1;
 }

 // Receive until the peer closes the connection
 do {

  iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
  if (iResult > 0)
  {
   recvbuf[iResult] = NULL;
   printf("Msg: %s\nBytes received: %d\n", recvbuf, iResult);
  }
  else if (iResult == 0)
   printf("Connection closed\n");
  else
   printf("recv failed with error: %d\n", WSAGetLastError());

 } while (iResult > 0);

 // cleanup
 closesocket(ConnectSocket);
 WSACleanup();

 return 0;
}


Tuesday, July 21, 2015

76 89 150 135 200 76 12 100 150 28 178 189 167 200 175 150 87 99 129
149 176 200 87 35 157 189

Wednesday, July 15, 2015

#include  

using namespace std;
//cin demo with exception detection and quit
int main(void)
{
int n = 0;
char c;
   
while(cin)
{
cout << "Enter a int(q to quit): ";
cin >> n;

if(!cin)
{
cin.clear();
c=cin.peek();
if(c=='q')
break;
else
{
cout << "Oops try again\n";
cin.ignore(5000,'\n');
}
}
}
return 0;
}

Thursday, November 01, 2007

Friend function example

// friend_functions.cpp
// compile with: /EHsc
// http://msdn2.microsoft.com/en-us/library/h2x4fzdz(VS.80).aspx
#include

using namespace std;
class Point
{
friend void ChangePrivate( Point & );
public:
Point( void ) : m_i(0) {}
void PrintPrivate( void ){cout << m_i << endl; }

private:
int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
Point sPoint;
sPoint.PrintPrivate();
ChangePrivate(sPoint);
sPoint.PrintPrivate();
}

Wednesday, September 26, 2007

Advice from Ben Stein to his son

Found on Yahoo Finance

Finance is an endlessly fascinating topic. It involves history, intuition, logic, mathematics, and hope. But for most of us, our finances come from work and not from investments.

In turn, how we do at work is vitally affected by the kind of education we get -- and most especially what we get out of our education. And what we get out of our education is very largely dependent on what we put into our education.

The Son Also Rises

This comes to mind because classes are starting soon in many colleges. (How did it get to be so early? When I was a lad, we started in late September.)

My one and only son, the light of my life, is starting at Presbyterian College, a liberal arts school in South Carolina. My wife and I want him to have the best possible experience there.

So, what follows is some advice for our son and for all college students, and maybe for all students of any age, about how to maximize the value of time spent in higher education.

Teachers Are People, Too

First, make friends with your teachers. They're human beings, not machines, and they want to have friends. They want to be liked and admired. They have exactly the same wishes about human relations that the rest of us have.

To make friends with your teachers, try the following:

• Read your assignments and be ready to discuss them.

I can tell you, based on my years of teaching at glorious American University, stupendously beautiful University of California, Santa Cruz, and spiritual and good-hearted Pepperdine, that not a lot of your fellow students will have read the assigned work.

If you're among the ones who have read it, and can raise your hand to discuss it, you'll place yourself at the top of the teacher's mind right away. He or she will be conscious of you, will appreciate you, and will remember you.

• Be polite but firm in class.

If you and your teacher disagree on something, you shouldn't be afraid to challenge him or her. Never do so rudely or cruelly (although you'll be tempted), but teachers want you to challenge them if it's based on facts and data and sound reasoning. They consider it a job well done when their students do that.

• If there's something you need to have clarified or an additional point you want to make, stay after class to talk to your teacher and walk around the campus with him or her.

Teachers are there to teach. If you show that you're there to learn, they'll admire you and thank you. Not as many students are in school to learn as there should be. If you're one of them, you're way ahead of the game.

Time Is of the Essence

Next, do your papers neatly, according to the assignment, and on time. Don't cheat yourself by not handing in your work or by doing it late.

College is largely about learning to budget your time and effort. If you give yourself plenty of time and don't wait until the last minute, you can get it all done, and done easily. College isn't boot camp -- your teachers want to make the work possible for you, not impossible. You can do it all if you give yourself enough time.

Also, spell-check everything you do and read it over to make sure there are no mistakes. When I was a teacher, nothing infuriated me more than a paper with a lot of misspelled words. Don't misspell anything and you'll be ahead of the curve.

Finally, make sure you write at least to the length suggested. Don't write too briefly or way too long. Do what your teacher asks you to do.

Be Well-Rounded

Take courses that will be of genuine use to your mind. It's vital that every young person know U.S. history, European history, and geography. It's just as vital that you know Shakespeare, the English and American poets, and the classics of Greek and Roman literature.

These are the common currency of educated humans all over the western world. You mark yourself as civilized or uncivilized depending on how much you know of Wordsworth and Keats and Gibbon as much as by what you wear.

Science and I have long been uneasy bedfellows, but some knowledge of biology, botany, and physics is basic. Mathematics is the queen of science. You should take as much of it as you can.

You probably won't call upon these subjects in your daily life when you enter the workforce, but they're vitally important in teaching you how to think. And learning how to think is, above all, the main challenge you face in school. It's true that you have to know certain basic facts, but you should also know how to approach a problem, break it down, solve it, and write about it. That's why it's important to take English composition, and take it seriously.

Affability and Neatness Count

Make friends, and preferably join a fraternity or sorority. It's lonely spending your hours by yourself in the library. You need to have a group you can hang with and joke with and eat with. This group will support you, cheer you, divert you, and energize you. Having friends in college is not a trivial matter -- it's life and death in terms of getting through successfully.

Also, don't allow yourself to look like a slob. Always be well dressed, cleanly showered, clean shaven, and look as if you mean business. Teachers don't like sloppy students. They like students who look neat.

I know you'll be sorely tempted to look like a hippie; I used to look like one, and it was fun. But if you wear sloppy clothes, be clean inside them and have your thoughts especially well-ordered to offset your appearance. You'll need to work twice as hard so your teachers know you're smarter on the inside than on the outside.

Some Final Tips

Don't smoke or drink to excess.

Play a sport. This keeps you in good physical condition, gives you a readymade set of friends, and allows you to express your tensions and anxiety on the field. Even if you're not perfect at the sport, just play it to get some air into your lungs.

If you're not happy with your roommate, switch. Having a good roommate makes all the difference in the world. Don't let yourself be sidetracked by having a disturbing person sharing your world. Go to the housing office and make an official switch, or just do it informally. But do it, and keep switching until you find a roommate you get along with.

Try to have a significant other. At your age, this is a huge part of life -- as it is at every age. Treat her or him with respect and dignity, and you'll soon find that you have a reason to get up every morning.

Above all, develop habits of work. You'll spend most of your life working, I hope. College is where you learn to allocate your time, get your assignments done, and develop a good rapport with your fellow workers (students) and your bosses (teachers), and make them all your friends.

If you can learn to work, think, and make friends, you're way ahead of the pack.

Good luck.

Sunday, March 19, 2006

Welcome to CIS282

This is the sandbox for the Augusta Tech CIS 282 Introduction to C++ Programming course. The syllabus and students download links of over there on the right.
Info:
I found a great old ARPAnet documentary that evangelizes networking computers in order to share resources: Computer Networks: The Heralds of Resource Sharing