|
Try this code.
It reads a number from CMD [For example: 1234].
Then puts each of its components in an array - digitArray[3]=1, digitArray[2]=2, digitArray[1]=3, digitArray[0]=4
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int digit=0,digitInput=0;
int digitArray[4]={0},digitn;
void getIntKey(void)
{
digitArray[0]=0;
digitArray[1]=0;
digit=0;
digitInput=0;
while((digit<4))
{
if (kbhit())
{
digitInput=getch();
if ((digitInput>47) && (digitInput<59))
{
digitArray[digit]=(unsigned char)digitInput-48;
digit++;
}
if (digitInput==13) { digitn=digitArray[0]; break; }
}
}
if (digitInput!=13) digitn=digitArray[3]*1+digitArray[2]*10+digitArray[1]*100+digitArray[0]*1000 ;
printf("\n%i\n\n",digitn);
}
int main()
{
system("color 1F");
printf("This program by TopCoder requires you to input 4 digits \n ");
printf("Input Digits (ex. 1=0001 , 1234=1234) \n ");
printf("\nInput Digits >");
getIntKey();
printf("\ndigitArray[3]=%d \n",digitArray[3]);
printf("digitArray[2]=%d \n",digitArray[2]);
printf("digitArray[1]=%d \n",digitArray[1]);
printf("digitArray[0]=%d \n",digitArray[0]);
printf("\n%i\n\n",digitn);
return 0;
}
..
|
|
|
|
|
That was co0l, realy!!
But, is it possible to wait until Enter pressed to diagnose the number of digits? And then the program dynamicaly tunes itself in order to put these digits into the char array which its dimension is defined by the number of digits, the user entered?
|
|
|
|
|
Yes, digit++ can sets the dimension.
Lets say you want to limit the user input to a 9 figure number or 100,000,000,
just set while((digit<4)) to be 9 or whatever and then write the code to process it.
eg.. digitn=digitArray[3]*1+digitArray[2]*10+digitArray[1]*100+digitArray[0]*1000 to digitArray[n]*100000000
This method is clunky .
|
|
|
|
|
Now it is just clunky
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int digit=0,digitInput=0;
int digitArray[10]={0},digitn;
void getIntKey(void)
{
digitArray[0]=0;
digitArray[1]=0;
digit=0;
digitInput=0;
while((digit<9))
{
if (kbhit())
{
digitInput=getch();
if ((digitInput>47) && (digitInput<59))
{
digitArray[digit]=(unsigned char)digitInput-48;
digit++;
printf("%d",digitInput-48);
}
if (digitInput==13) { digitn=digitArray[0]; break; }
}
}
switch(digit)
{
case 0:
case 1:
digitn=digitArray[0]*1 ;
break;
case 2:
digitn= digitArray[1]*1 +digitArray[0]*10 ;
break;
case 3:
digitn= digitArray[2]*1+digitArray[1]*10 +digitArray[0]*100 ;
break;
case 4:
digitn=digitArray[3]*1+digitArray[2]*10+digitArray[1]*100+digitArray[0]*1000 ;
break;
case 5:
digitn=digitArray[4]*1+digitArray[3]*10+digitArray[2]*100+digitArray[1]*1000+digitArray[0]*10000 ;
break;
case 6:
digitn=digitArray[5]*1+digitArray[4]*10+digitArray[3]*100+digitArray[2]*1000+digitArray[1]*10000
+digitArray[0]*100000;
break;
case 7:
digitn=digitArray[6]*1+digitArray[5]*10+digitArray[4]*100+digitArray[3]*1000+digitArray[2]*10000
+digitArray[1]*100000 +digitArray[0]*1000000;
break;
case 8:
digitn=digitArray[7]*1+digitArray[6]*10+digitArray[5]*100+digitArray[4]*1000+digitArray[3]*10000
+digitArray[2]*100000 +digitArray[1]*1000000+digitArray[0]*10000000;
break;
case 9:
digitn=digitArray[8]*1+digitArray[7]*10+digitArray[6]*100+digitArray[5]*1000+digitArray[4]*10000
+digitArray[3]*100000 +digitArray[2]*1000000+digitArray[1]*10000000 +digitArray[0]*100000000;
break;
}
printf("\n%i\n\n",digitn);
}
int main()
{
system("color 1F");
printf("DigitsIntoArrayComponents 3.1 by TopCoder \n ");
printf("Max Input is 8 Digits ( . 99,999,999) \n ");
printf("\nInput Digits >");
getIntKey();
printf("\ndigitArray[7]=%d \n",digitArray[7]);
printf("digitArray[6]=%d \n",digitArray[6]);
printf("digitArray[5]=%d \n",digitArray[5]);
printf("digitArray[4]=%d \n",digitArray[4]);
printf("digitArray[3]=%d \n",digitArray[3]);
printf("digitArray[2]=%d \n",digitArray[2]);
printf("digitArray[1]=%d \n",digitArray[1]);
printf("digitArray[0]=%d \n",digitArray[0]);
printf("\n%i\n\n",digitn);
return 0;
}
...
|
|
|
|
|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
The most cplusplusy way of doing this is to just copy the input from std::cin into your array of characters:
std::vector<char> text;
std::copy( std::istream_iterator<char>( std::cin ), std::istream_iterator<char>(), std::back_inserter( text ) );
You'd have to press CTRL-Z to terminate the input though.
If I've misread what you want and you really want an array of integers, one per digit in the number you have to do a bit of conversion instead of a copy:
std::vector<int> digits;
std::transform( std::istream_iterator<char>( std::cin ), std::istream_iterator<char>(), std::back_inserter( digits ), []( const char c ) { return int( c - '0' ); } );
If your compiler doesn't support Lambda functions (For VC++ it's VC++ 2010 or for gcc it's version 4.3 or later) then it's a bit fiddlier but you need a function object instead of the Lambda function. (The Lamba's the final parameter to the std::transform call, []( const char c ) { return int( c - '0' ); ).
Cheers,
Ash
|
|
|
|
|
bijan.8k wrote: I want to read a number from CMD...
Do you mean the command line?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hallo,
I try to open the html return from an Internet URL, for example http://leavemusic.de/live/leavemusic/index.php, and I want to get the html code in a CString. What is the best way to do it? It does not work with CFile for me.
Thank you!
|
|
|
|
|
Have you tried URLDownloadToFile()?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
If you don't want to use the function David suggested and if you've got time to experiment I can wholeheartedly recommend using cURL[^] for what you're trying to do. It's very customisable, flexible and portable. It's also got a bit of a clunky C handle based interface only it's programmer could love but for the sort of thing you're after it's not too bad. It took me about 4 hours of reading and playing to get a small file downloader working with it.
If you know how to use sockets then you could just open a socket on the remote computer, send and HTTP get and read everything between the opening <HEAD> tag and the closing </BODY> tag. I'd only recommend this method if you're already comfy with network programming otherwise the small details might get a bit much.
Cheers,
Ash
|
|
|
|
|
I encountered a hard question:
#include <windows.h>
#include <time.h>
int main(int argc, char* argv[])
{
LPCTSTR fileName = "\\\\192.169.3.9\\tempdir\\MYFILE.TXT";
HANDLE hFile;
DWORD dwError;
time_t nowTick = time(NULL);
hFile = CreateFile(fileName, // open MYFILE.TXT
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL | // normal file
FILE_FLAG_OVERLAPPED, // asynchronous I/O
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
printf("error:%x, diff:%d\n",dwError, time(NULL) - nowTick);
return 0;
}
CloseHandle(hFile);
return 0;
}
Question:
1.Why does the function CreateFile block a long time when the path specified by the variable fileName is not exist?
2.If i does not expect the function CreateFile block a long time, does it have a better solution?
3.what's the best solution of accessing the file on the net?
I am a c++ programer!
It's cheerful to cooperate with you!
|
|
|
|
|
the API is accessing the file via networking, so you got a long timeout.
C++ programmer are writing for lenghtly operations threads.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
...and go for a coffee or play some game during.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Computers are evil, EVIL i tell you!! <
|
|
|
|
|
thats what i do. or listening to some music.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
When you create a file over the network there's loads of things that can go wrong - the OS has got to wait a while to see if the remote system responds before failing.
There's probably not a lot you can do to speed things up, but there's a couple of tricks that can work.
First one is to do the opening assynchronously. Spin off a thread and use a future to signal when the file's open. If you can reorder your logic a bit so you start opening the file, process what you can in memory then write to the file when it's open you might be able to make it look like the file open opened quicker.
The second one only works if you know you're going to want to open the file well in advance of needing to use it. Just open the file as one of the first things your program does. When you need the file just open it again. This uses the fact that most OS filesystems are better at reopening already open files as it just needs to duplicate the file handle. Of course if you don't know what the file is well in advance this is absolutely no use to you whatsoever.
Cheers,
Ash
|
|
|
|
|
Any has any experience with these two?
I searched throughout the Internet for some getting started/tutorial/documentation for libmms but found absolutely nothing.
ffmpeg is a bit better documented, but I want to go one step at a time... I need to get the stream first...
We are using Linux daily to UP our productivity - so UP yours!
|
|
|
|
|
Hi,
I have to create a dll file to bind all the source code in a sinle library to ease of use.
For which dll I should go for
1. Regular DLL using shared MFC DLL
2. Regular DLL with MFC statically linked
3. MFC extension DLL
|
|
|
|
|
Why don't you create a .lib file instead ? If the only purpose is to "bind all the source code in a single library", then I would opt for a static library.
|
|
|
|
|
for size constraint and MFC classes uasge, I am looking going for MFC Ext dll.
|
|
|
|
|
you can reduce size of DLL, by eliminating not required library files, search CP for same
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
john5632 wrote: 1. Regular DLL using shared MFC DLL
2. Regular DLL with MFC statically linked
almost same, but 2 one would be bulky in comparison of 1, as MFC dll would be statically linked.
MFC extension dll can be used only with MFC application! rest can be used with n number of lanaguage
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I am able to build MFC Ext DLL. I also intergarte .h,.lib and .dll files in my application and working fine. But my worry is..I have almost 15 file (.h and .cpp) which is need to be placed inside dll file.
Is there any solution that I am able to add only single .h file for all .cpp file.
If yes then how to do?
|
|
|
|
|
It all depends what you're trying to achieve. MFC extension DLLs do what they say on the tin, they extend MFC and you can't use it in application if the application's not MFC. So if you don't want to write an MFC app then this choice isn't for you!
Regular DLLs can be called by any application. They expose a C or C++ interface that doesn't use MFC in the interface. You can still use MFC internally though, in the implementation. If you want you code callable by non-MFC apps use a regular DLL (say you implement a control using MFC but give it a fairly bog standard message interface, or you're insane and for some reason like CString).
The final choice is whether to link MFC statically or dynamically. Linking statically increases the size of your DLL but means you won't have to ship an MFC DLL with your app (or use the merge module in your installer). This makes it a bit more self contained. However even using a simple window class can be quite costly in terms of what MFC stuff your DLL has to drag in to do it's stuff. If you link dynamically your DLL will be smaller but you'll have to ship another stonking great DLL. If you're already shipping the MFC DLL for another nefarious purpose it's a no brainer, link dynamically. Otherwise build both ways and see which gives you a size you're happy with.
Cheers,
Ash
|
|
|
|
|
Hi, every one,
when i do encrypt, i meet some problem, if somebody who meet this kind of problem,can you tell me how to solve this problem, thank you!
First: I write a encrypt funtion using C++, and after I get the asymmetric key pair, then I want to save the key to the key store, in C++, I did not find such of function. But under .NET, there is a class named CspParameters, it can save a key to a container, all the code I see is below:
int main()
{
// creates the CspParameters object and sets the key container name used to store the RSA key pair
CspParameters^ cp = gcnew CspParameters;
cp->KeyContainerName = "MyKeyContainerName";
// instantiates the rsa instance accessing the key container MyKeyContainerName
RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider( cp );
// add the below line to delete the key entry in MyKeyContainerName
// rsa.PersistKeyInCsp = false;
//writes out the current key pair used in the rsa instance
Console::WriteLine( "Key is : \n{0}", rsa->ToXmlString( true ) );
}
My qusetion is :
If I donot use the CspParameters class, how can I save the key pair to the key store using C++,if donot have this kind of function , how can i save my key pair, but not saved to BLOB.
Thank you very much!
|
|
|
|
|
Hi,
I drived a class from CStaic. Now I am using this new class for static control.
I am facing problem to draw the color on backgroung, I tried OnCtlColor() but breakpoint is not coming in this function.
What might be the cause?
|
|
|
|
|