|
In C++, we can declare and initialize the variable i inside the for condition. However, in C, we can't do that, we must declare it first. But almost all the time when I declare a variable, I will initialize it at the same time, this makes re-initializing variable i in for loop unnecessary.
I was wondering would that be considered poor programming style? I've seen this style a lot.
// C code
<br />
int i = 0;<br />
<br />
for( ; i < 5; i++)<br />
{<br />
}<br />
or should I?
<br />
int i = 0;<br />
<br />
for(i = 0; i < 5; i++)<br />
{<br />
}<br />
Thanks
|
|
|
|
|
Alex Ngai wrote:
for( ; i < 5; i++)
{
// code
}
this IS ugly ...
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Thanks for your reply. for( ; i < 5; i++) may seem ulgy to you,
but in fact, I've seen a lot of people use this style. In some
cases, you have no choice, for example:
int foo(int n)
{
for( ; n < 5; n++ )
{
}
}
So, I was wondering when you use for loop in C code, do you
re-initialize the variable i?
-----------------------------
C++ without virtual functions is not OO. Programming with classes but without dynamic binding is called "object based", but not "object oriented".
|
|
|
|
|
Alex Ngai wrote:
In some
cases, you have no choice
No choice?, you could use a while loop rather than a for loop!
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain)
|
|
|
|
|
Always initialise your loop variable at the beginning of a for loop. If it doesn't need to be there, it will be optimised away by the compiler anyway. If you use the first form (not reinitialising it) and you reuse the variable somewhere before the for loop, your for loop is going to be completely screwed.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
the second would be better.....
but if reinitializing is that u consider inappropriate,then tooo i wouldnt go for the first rater i would maybe modify the first a little.....
int i ;
for(i = 0; i < 5; i++)
{
}
"faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
|
|
|
|
|
It's very important for me to save big strings in CTreeView object.
I've set limit in my CTreeView object to 500, but it can display only first 250.
How can I change this parameter?
Thanks in advance, Aleksey.
|
|
|
|
|
Try setting the TVS_NOTOOLTIPS style, if it makes no tool tips then it may display the whole thing always.
-- Rocky Dean Pulley
|
|
|
|
|
I mean the max length in CTreeView is about 270 any way. It's a pity!
I need 340 letters!
|
|
|
|
|
my app needs to check if a PC connects to the internet.
(if yes, download files from our web-site.)
is there a function which can checks it very fast?
(currently i use a slow one with timeout)
thx
includeh10
|
|
|
|
|
|
Here are a few ways.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
i teste 2 functions and followings are results of checking time (ms), they are all block-functions
function -------------------- connected ------- disconnected
InternetGetConnectedState -------- 39 ---------- 52,059
InternetCheckConnection -----------451 --------- 104,041
they are too slow
thx
|
|
|
|
|
Hi everyone,
I have asked on several programming forums and googled for weeks, but I am unable to find just exactly how to send and receive data from a client to a webserver.
What I need is a simple example on how to send a POST request to a webserver using WinSock. (Form data name=YourName&Age=20)
This code should (if I understand everything correctly) work, yet it does not. Whenever the server gets back to me, all it gives me is a 0.
Here is the simple PHP script I use for testing:
<?php
print($_POST["Name"]);
?>
Please help,
Peter
--Code--
#include <iostream>
#include <string>
#include <cstdlib>
#include <winsock.h>
using namespace std;
int main() {
WSADATA WSAData;
WSAStartup(MAKEWORD(1,1), &WSAData); // Someone told me to use the lowest version of Winsock that satisfied my needs
struct hostent* Host;
struct sockaddr_in Server;
Server.sin_family = AF_INET;
Server.sin_port = htons(80);
Host = gethostbyname("tdlgames.com"); // One of my websites. Uses the Apachee webserver on a Linux platform.
Server.sin_addr = *((struct in_addr*)Host->h_addr);
SOCKET MySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
connect(MySocket, (struct sockaddr*)&Server, sizeof(struct sockaddr));
string SendInfo("POST /Test.php http/1.1\r\nHost: tdlgames.com\r\nUser-Agent: TestPostMethod/1.0\r\nContent-Type: application-www-form-urlencoded\r\n\r\nName=Peter");
// Eventually, the error might be in the things I send - again, please advise on what to do...
int BytesSent = send(MySocket, SendInfo.c_str(), SendInfo.length(), 0);
char Buffer[1024];
while (recv(MySocket, Buffer, 1023, 0))
cout << Buffer << endl;
closesocket(MySocket);
WSACleanup();
system("pause");
}
---
Thanks for reading.
|
|
|
|
|
See this[^] article. If you also want to retrieve information from the webserver's response, see this[^] article.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Dude
I think you are just missing the Content-Length specification in your POST request header. This is needed so the server knows when the content-data ends (\r\n\r\n is only the terminator for the header). Try sending something like this to the server:
POST /logon HTTP/1.1
Host: peer200:6002
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: https://peer200:6002/logon
Content-Type: application/x-www-form-urlencoded
Content-Length: 54
USERNAME=Administrator&PASSWORD=xxxxxxxx&SUBMIT=SUBMIT
...here the length of the content data (USERNAME ....SUBMIT is exactly 54 bytes). Hope this helps !
Thanks,
Neil Humphreys.
|
|
|
|
|
Thanks for the replies, all of you.
Neil, your headers worked beautifully if I sent them as-is. However, whenever I try to do something complicated like asking the user to type in his name and then change the Content-Length parameter accordingly, the server sends back a 0...
Here is the new snippet I have been adding:
connect(MySocket, (struct sockaddr*)&Server, sizeof(struct sockaddr));
string Send("POST /Test.php HTTP/1.1\r\nHost: tdlgames.com:80\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\nContent-Type: application/x-www-form-urlencoded\r\n");
cout << "Enter your name: ";
string Name;
getline(cin, Name);
char Content[100];
sprintf(Content, "Content-Length: %d\r\n\r\n", Name.length());
Send += Content;
Send += "Name=";
Send += Name;
send(MySocket, Send.c_str(), Send.length(), 0);
...
The receive code has not changed. Please help me a bit more - I am grateful for all the answers.
Thank you,
Peter.
Visit
http://www.tdlsoftware.com
|
|
|
|
|
Hi Peter,
Looks like you may have forgotten to account for "Name=" in the Content-Length. Name.length() is actually the length of your content minus the length of the string "Name=".
Hope this helps...
Thanks,
Neil Humphreys.
|
|
|
|
|
I use vc++ 6
I have set an Enviornment variable MyDir. I want to use it as search path for
1.*.h
2 *.cpp (in #include only)
3. *.dsw(for sub or dependent project)
I dont want it to be added in VC++ directory dialog. Is there some other way
. so that nothing is hardcoded
|
|
|
|
|
I have an edit box that does nothing but display information. I have been wondering how to change the position of the text cursor to always go to the bottom of the text that appears. Right now, the cursor goes back to the top everytime text is added. The scroll bars will only automaticly scroll down if the user inputs data. Basicly I need to make sure the view of the text is always at the bottom when new data is added. I am not using MFC. Anyone know how I should do this?
|
|
|
|
|
See CEdit::SetSel, or if you are feeling hardcorde, SendMessage(IDC_EDIT,EM_SETSEL..
onwards and upwards...
|
|
|
|
|
I used SendMessage.
And it works great!
|
|
|
|
|
There are macros to encapsulate some useful windows messages in the WindowsX.H file.
What with everything built on a 'foundation' or 'template' library of some sort these days, I think people tend to forget about WindowsX.H
|
|
|
|
|
hiho@ll
i need to login into a website, controlled by my c++ prog
for example i have a menu-button "Login to website"
my prog knows the username and password
and i need to login with standard browser or internet explorer in the website?
i've tried with http://user:pass@www.mytestlogin.com
but it didn't work
i just get the login site, where i have to fill out the HTML-Forms and submit it
may i can generate the needed HttpRequest and let the Internet Explorer Read the HttpResponse? (how can i do that?)
are there any topics in the forum i haven't found about this subject?
thx@ll
|
|
|
|
|
You could just submit the login form programatically by examining the site's login page HTML. See this[^] section for code samples.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|