Like Tree1Likes
  • 1 Post By Salem

help to store data inside temp string

This is a discussion on help to store data inside temp string within the C++ Programming forums, part of the General Programming Boards category; hi, i am new in this language i made little progress but still have issue with this 3 red lines ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    3

    help to store data inside temp string

    hi,
    i am new in this language i made little progress but still have issue with this 3 red lines anyone can help me with it please.

    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <iostream>
    using namespace std;
    #define hosty "localhost"
    #define page "update/check.php"
    int check()
    {
        char temp;
            WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
            cout << "WSAStartup failed.\n";
            system("pause");
            return 1;
        }
        SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        struct hostent *host;
        host = gethostbyname(hosty);
        SOCKADDR_IN SockAddr;
        SockAddr.sin_port=htons(80);
        SockAddr.sin_family=AF_INET;
        SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
        if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
            return 1;
        }
        send(Socket,"GET /" page " HTTP/1.1\r\nHost: " hosty "\r\nConnection: close\r\n\r\n", strlen("GET /" page " HTTP/1.1\r\nHost: " hosty "\r\nConnection: close\r\n\r\n"),0);
        char buffer[10000];
        int nDataLength;
        temp = "";
        while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){        
            int i = 0;
            while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
                cout << buffer[i];
                temp = temp & buffer[i];
                i += 1;
            }
        }
        closesocket(Socket);
            WSACleanup();
    }
    int main()
    {
        for(;;)
        {
            check();
            printf("\nchecked.");
            Sleep(10000);
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,703
    FYI: This is C++ code you posted; It is NOT C code!!

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Good point, so moved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    sorry didn't notice C and c++ hahaha

    anyone can help me with this little problem?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Aside from the fact that temp = ""; shouldn't even compile, temp doesn't do anything because it is never used.

    All you have is a write-only variable.
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I am not "new to this language" but honestly am still having a problem figuring out what you are trying to do here. You highlighted points in the code that you didn't understand but you shouldn't be looking at code like this if you don't know what the first highlight means.

    char temp;

    That is a declaration of a character that is named temp. If you don't know that you shouldn't be looking at this code at all.

    Second line is

    temp = "";

    What do you think this is? An initialization of a char that was previously declared and is now being initialized as equal to nothing (this prevents undefined behavior), but you are using "" which is a const char* rather than a character.

    the third line has actually peaked my curiosity as I cannot find a direct answer to what it does.

    temp = temp & buffer[i];

    we have a few options with the & operator either we are refering to an adress of or we are refering to a reference to. Anything else I couldn't find any references to it or knowledge on the subject.

    What are you trying to do?
    Last edited by Lesshardtofind; 01-03-2013 at 02:04 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Operators in C and C++ - Wikipedia, the free encyclopedia
    & is a bitwise operator in this context.

    Though TBH, this looks like a VB programmer trying to do string concatenation.

    Perhaps (in this case)
    Code:
    string temp = "";
    while (i < nDataLength && (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') ) {
                cout << buffer[i];
                temp += buffer[i];
                i += 1;
            }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    3
    Quote Originally Posted by Salem View Post
    Operators in C and C++ - Wikipedia, the free encyclopedia
    & is a bitwise operator in this context.

    Though TBH, this looks like a VB programmer trying to do string concatenation.

    Perhaps (in this case)
    Code:
    string temp = "";
    while (i < nDataLength && (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') ) {
                cout << buffer[i];
                temp += buffer[i];
                i += 1;
            }
    thank you mr salem it worked 100%

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Store combined data types into string?
    By 843 in forum C Programming
    Replies: 5
    Last Post: 12-01-2010, 10:56 AM
  2. Replies: 2
    Last Post: 04-13-2010, 03:14 PM
  3. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  4. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21