Reputation: 67
The user enters 2 numbers and either +,-,/,* and should be getting an answer back. Problem is, it's not displaying what they entered properly and it's not calculating. Why is this? I have provided a screenshot to show what I am getting. How do I fix this? http://imageshack.us/photo/my-images/404/mathfn.jpg/
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <winsock.h>
// Function prototype
void StreamClient(char *szServer, short nPort);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%s: %d\n", s, WSAGetLastError())
////////////////////////////////////////////////////////////
void main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
short nPort;
//
// Check for the host and port arguments
//
if (argc != 3)
{
fprintf(stderr,"\nSyntax: TCPTimeClient ServerName PortNumber\n");
return;
}
nPort = atoi(argv[2]);
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return;
}
//
// Go do all the stuff a datagram client does
//
StreamClient(argv[1], nPort);
//
// Release WinSock resources
//
WSACleanup();
printf("\nPlease enter a floating point number: ");
float number1;
scanf("%f", &number1);
printf("\nPlease enter a mathematical operator (+,-,*,/): ");
char mathOperator;
scanf("%f", &mathOperator);
printf("\nPlease enter a floating point number: ");
float number2;
scanf("%f", &number2);
printf("\nCalculating %f %f %f...", &number1, &mathOperator, &number2);
printf("\nPlease enter a floating point number: ");
float number3;
scanf("%f", &number2);
}
////////////////////////////////////////////////////////////
void StreamClient(char *szServer, short nPort)
{
int nRet; // return code
char szBuf[256]; // client buffer area
char szSvr[256]; // server name
LPHOSTENT lpHostEntry; // host data structure
SOCKET theSocket; // client socket
SOCKADDR_IN saClient; // socket address structure
//
// Get local machine name
//
nRet = gethostname(szSvr, sizeof(szSvr));
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("gethostname()");
return;
}
//
// Display an informational message
//
printf("Datagram Client [%s] sending to server [%s] on port %d...\n",
szSvr, szServer, nPort);
//
// Find the server
//
lpHostEntry = gethostbyname(szServer);
if (lpHostEntry == NULL)
{
PRINTERROR("gethostbyname()");
return;
}
//
// Create a TCP/IP datagram socket
//
theSocket = socket(AF_INET, // Address family
SOCK_STREAM, // Socket type
0); // Protocol
//
// Check for errors
//
if (theSocket == INVALID_SOCKET)
{
PRINTERROR("socket()");
return;
}
//
// Fill in the address structure of the server
//
saClient.sin_family = AF_INET;
saClient.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Client's address
saClient.sin_port = htons(nPort); // Port number from command line
//
// Connect to the server
//
nRet = connect(theSocket,
(struct sockaddr *)&saClient,
sizeof(saClient));
//
// Check for errors
//
if(nRet == SOCKET_ERROR)
{
PRINTERROR("Connect()");
return;
}
//
// Prepare some data to send to the server
//
sprintf(szBuf, "From the Client [%s]", szSvr);
//
// Send data to the server
//
nRet = send(theSocket, // Socket
szBuf, // Data buffer
(int)strlen(szBuf), // Length of data
0); // Flags
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("send()");
closesocket(theSocket);
return;
}
//
// Zero out the incoming data buffer
//
memset(szBuf, 0, sizeof(szBuf));
//
// Wait for the reply
//
nRet = recv(theSocket, // Socket
szBuf, // Receive buffer
sizeof(szBuf), // Length of receive buffer
0); // Flags
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recv()");
closesocket(theSocket);
return;
}
//
// Display the data that was received
//
printf("\n%s", szBuf);
//
// Close the socket
//
closesocket(theSocket);
return;
}
Upvotes: 0
Views: 752
Reputation: 2366
I couldn't see why you were not able to input the char so I created a test program - sure enough same behavior! I don't scanf() alot, but I guessed that the scanf("%c") was grabbing whatever character was in the buffer - if any - and continuing on before we had a chance to enter anything. So I made it totally explicit - specifying 'stdin' for the fscanf/fgetchar stream and doing a fflush() to make sure it's empty before reading. Also, I stopped using scanf() at all just to get a single character. That was it! Here's the complete program:
printf("Please enter a floating point number: ");
float number1;
fscanf(stdin,"%f", &number1);
printf("Please enter a mathematical operator (+,-,*,/): ");
char mathOperator;
//fscanf(stdin,"%f", &mathOperator);
fflush(stdin);
mathOperator = (char)fgetc(stdin);
printf("Please enter a floating point number: ");
float number2;
fscanf(stdin,"%f", &number2);
printf("\nCalculating %f %c %f...", number1, mathOperator, number2);
float result = 0.0F;
switch (mathOperator) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '/':
result = number1 / number2;
break;
case '*':
result = number1 * number2;
break;
default:
printf("\nInvalid operator entered!");
}
printf( "\n\nThe answer is %0.1f\n", result );
fflush(stdin);
char ch = getc(stdin); // wait for keypress
And here's the output of a test run:
Please enter a floating point number: 3.4
Please enter a mathematical operator (+,-,*,/): *
Please enter a floating point number: 2
Calculating 3.400000 * 2.000000...
The answer is 6.8
[End Update]
The specific problem you're having with the printf() is that you're passing the addresses of the floating point values - not the values themselves.
printf("\nCalculating %f %f %f...", &number1, &mathOperator, &number2);
should be:
printf("\nCalculating %f %f %f...", number1, mathOperator, number2);
The next problem is 'mathOperator' - it looks like you're asking the user to input a char, but the data type specified int the printf() is float. That's not going to work.
So the printf really should be more like:
printf("\nCalculating %f %c %f...", number1, mathOperator, number2);
assuming 'mathOperator' has a '+', '-', etc. in it.
As far as why it's not calculating - I don't see (maybe it's just omitted for brevity) any code to actually do the calculation.
[Calculation Update]
Since the only allowed operators are '+', '-', '/', '*' and the operands are always floats, the calculation logic is pretty simple:
float result = 0.0F;
switch (mathOperator) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '/':
result = number1 / number2;
break;
case '*':
result = number1 * number2;
break;
}
Then you would probably want to add a printf() to output the result (and/or send it back via TCP):
printf( "The answer is" "%0.1f\n", result );
If you get into fancier calculations where you have to worry about operator precedence, type conversion, etc. a simple 'case' won't be enough. You'll want to look at parsers to build a parse tree from the expression and evaluate it according to some rules (grammar). But trust me - you want to master this first ;-)
See this question for an idea of what's involved.
Upvotes: 1