Thread: help to store data inside temp string

  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
    4,183
    FYI: This is C++ code you posted; It is NOT C code!!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good point, so moved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    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.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    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 03:04 AM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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, 11: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