Thread: Error why..??

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    Error why..??

    Code:
    #include "stdafx.h"
     #include <windows.h>
     #include <stdio.h>
     #include <winsock.h>
     
     int APIENTRY WinMain( HINSTANCE hinstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
     {
         WSADATA ws;
         char buff[1000],buf1[100],buf2[100],buf3[100],buf4[100],buf5[100],buf6[100];
         WSAStartup(0x0101,&ws);
         buf[0]="";
         sprintf(buf1,"\nWinsock Ver Requested: %d %d",HIBYTE(ws.wVersion),LOBYTE(ws.wVersion));
         sprintf(buf2,"\nWinsock Ver Available: %d %d",HIBYTE(ws.wVersion),LOBYTE(ws.wVersion));
         sprintf(buf3,"\nCurrent WinSock Implementation:%s",&ws.szDescription);
         sprintf(buf4,"\nSystem Status: %s",&ws.szSystemStatus);
         sprintf(buf5,"\nMaximum Sockets: %u",ws.iMaxSockets);
         sprintf(buf6,"\nMaximum message size: %u",ws.iMaxUdpDg);
         strcat(strcat(strcat(strcat(strcat(strcat(buf,buf1),buf2),buf3),buf4),buf5),buf6);
         MessageBox(0,buf,"Info",0);
         WSACleanup();
         return(0);
     }
     Error in the first line (header file #include "stdafx.h")Can anyone tell me please why..???

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    A general tip
    I saw that in this and your previous post,you just say that there is an error and you indicate he line.Why do not describe the error? This will help the others to help you
    Even better,why not post what compiler says? Usually he is very explicit with errors

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    strcat(strcat(strcat(strcat(strcat(strcat(buf,buf1),buf2),buf3),buf4),buf5),buf6);
    What. The. ****???

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by rags_to_riches View Post
    Code:
    strcat(strcat(strcat(strcat(strcat(strcat(buf,buf1),buf2),buf3),buf4),buf5),buf6);
    What. The. ****???
    hehe
    In another language that could probably be written as
    Code:
    buf = buf + buf1 + buf2 + buf3 + buf4 + buf5 + buf6;

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Code:
        sprintf(buf1,"\nWinsock Ver Requested: %d %d",HIBYTE(ws.wVersion),LOBYTE(ws.wVersion));
         sprintf(buf2,"\nWinsock Ver Available: %d %d",HIBYTE(ws.wVersion),LOBYTE(ws.wVersion));
         sprintf(buf3,"\nCurrent WinSock Implementation:%s",&ws.szDescription);
         sprintf(buf4,"\nSystem Status: %s",&ws.szSystemStatus);
         sprintf(buf5,"\nMaximum Sockets: %u",ws.iMaxSockets);
         sprintf(buf6,"\nMaximum message size: %u",ws.iMaxUdpDg);
         strcat(strcat(strcat(strcat(strcat(strcat(buf,buf1),buf2),buf3),buf4),buf5),buf6);
    another approach to avoid the strcat's is to sprintf into a single buffer with a moving index, since sprintf returns the number of characters written. also, might want to use snprintf to be safe(r) from overrun.
    Code:
       char buff[1000];
       int i;
         WSAStartup(0x0101,&ws);
         buf[0]="";
         i = 0;
         i +=  sprintf(&buff[i],"\nWinsock Ver Requested: %d %d",HIBYTE(ws.wVersion),LOBYTE(ws.wVersion));
         i +=  sprintf(&buff[i],"\nWinsock Ver Available: %d %d",HIBYTE(ws.wVersion),LOBYTE(ws.wVersion));
         i +=  sprintf(&buff[i],"\nCurrent WinSock Implementation:%s",&ws.szDescription);
         i +=  sprintf(&buff[i],"\nSystem Status: %s",&ws.szSystemStatus);
         i +=  sprintf(&buff[i],"\nMaximum Sockets: %u",ws.iMaxSockets);
         i +=  sprintf(&buff[i],"\nMaximum message size: %u",ws.iMaxUdpDg);

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What. The. ****???
    We must go deeper!

    Soma

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    or just:

    Code:
    sprintf(buf,"\nWinsock Ver Requested: %d %d" \
                   "\nWinsock Ver Available: %d %d"," \
                      "\nCurrent WinSock Implementation:%s" \
                   "\nSystem Status: %s" \
                   "\nMaximum Sockets: %u" \
                   "\nMaximum message size: %u",
         HIBYTE(ws.wVersion),LOBYTE(ws.wVersion), HIBYTE(ws.wVersion), LOBYTE(ws.wVersion),&ws.szDescription,&ws.szSystemStatus, ws.iMaxSockets, ws.iMaxUdpDg);
    And just cut out all of the repeated string scanning, tokenisation, parsing, copying, concatenation and everything else that you do in multiple steps and multiple times to print one long string.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Plus, you don't even need the \ to fold the format string into one long string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by Salem View Post
    Plus, you don't even need the \ to fold the format string into one long string.
    Yes, if you want it to be quite unreadable and need a 200+ column display.
    My personal rule is to put a \ continuation after any new line in the source string, so the source-string looks vaguely like the real string when printed.

    But I hacked out their original code which had newlines at the start of strings instead, which I find odd, and just went with that instead.

    But to steer the conversation back, if including "stdafx.h" gives you an error, you don't have it in your include path (wrong include paths, wrong spelling of the filename, "" instead of <>, or even because you don't use Visual C++). Try removing it, or rebuilding the project it's in. Other than that, you'll need to post your exact error (by which time you could just google that error specifically, but there you go...)

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You only need \ in multi-line #defines, which must be a single logical source line.

    These are the same.
    Code:
    int main ( ) {
    	printf("hello " \
    		   "world\n");
    	printf("hello "
    	       "world\n");
    	printf("hello world\n");
    	return 0;
    }
    As for the stdafx.h thing:
    1. Delete that line from the source file.
    2. Project->settings->(compiler or preprocessor)->precompiled headers and turn it OFF (for debug and release)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  2. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM