|
I am a beginner and I am trying to write a program that takes a five digit inputed value and adds the digits together. I am trying to use a for loop to do this but I am having trouble. Please help.
|
|
|
|
|
Say more !
What are your problems ?,
Where are you stuck ?
Can you describe how you would do this in your own word ?
Can you formalise it into code ?
Show some code ...
Max.
|
|
|
|
|
So far I have:
int digit; //digit is the five digit number to be inputed by user
cin<
|
|
|
|
|
Is this what you are tring to do?
#include <stdio.h>
int main(int argc, char* argv[])
{
if(argc > 1)
{
double nval = 0;
for(size_t i = 0, count = strlen(argv[argc - 1]);
i < count;++i)
nval += argv[argc - 1][i] - '0';
printf("sum is : %f\n", nval);
}
return 0;
}
Is this some kind of homework or something?
|
|
|
|
|
How are you storing the five digits?
Are they in a char [] or an int []?
Michael
Fat bottomed girls
You make the rockin' world go round -- Queen
|
|
|
|
|
Post the code you have already begun to write and we will help you. If you have not already started to code, tell us more precisely how you would like to do it. You wno't learn anything if we throw the solution on this forum.
~RaGE();
|
|
|
|
|
Talk you your teacher or teacher's aid; you are clearly missing basic concepts that were discussed in class.
|
|
|
|
|
//I have now come up with the following but it still is not working properly:
int main()
{
int digit;
int sum;
int Total;
int num;
cout << "Enter a five digit number followed by enter: " ;
cin >> digit;
for (int i=4; i>=0; i--)
{
int p = pow(10,i);
num=digit/p;
digit=digit%p;
sum = sum + num;
}
cout << "The sum is " << sum << endl;
//Something is wrong with the sum = sum + num line. The previous lines work properly.
|
|
|
|
|
Key problem:
- Sum is not initialized to zero. (Set your warnings to level 4 if using VC7. If using VC6, you'll have to use pragma to ignore several warnings.)
Minor problems:
- Total is not used
- num should be a local variable.
Algorithm problem:
- Using pow() is inefficient. (Remember, it doesn't matter in which order you add the digits!)
|
|
|
|
|
So do you advise me putting cin >> digit into the for loop and then adding the digits to sum?
|
|
|
|
|
Only if you are allowed to have the user enter one digit at a time.
Otherwise you can process the digits from right to left:
while (digit)
{
sum += digit%10;
digit /= 10;
}
Of course this assumes: digit >= -99999 && digit <= 99999.
|
|
|
|
|
Hi, I have a problem (I think a big problem).
The language is C++. The operation sistem is Windows 2000.
If I create two application. In the first application fill a structure, open a file and send this information (pointer to file) at second application which fill the file. When the second application end your job, the control switch at first application which close the file.
The question is:
can I open a file in a application sending the pointer at second applicaton to fill the file.
|
|
|
|
|
No. Handles are unique to an application's memory space.
|
|
|
|
|
Hi,
Can any one help me out in sorting out the problem that is getting my brains out ...it's like Iam calling the CSocket object in the secondary thread and at this point I get an exception raised "Debug assertion failed" ..and I did the same thing with the raw socket handle and it worked absolutely perfect ..can ne one plz help cause I have to use the CSocket class and plz if there are any alternatives..
Thanx and Regards,
Junni
|
|
|
|
|
|
CSocket* connect()
{
pClientSocket = new CSocket;
pClientSocket->Create();
bConnect = pClientSocket->Connect(
m_strServerIPAddress.c_str(),
m_iServerPort
);
}
if( ! bConnect )
{
CString strTmp;
strTmp.Format("Error: Failed to connect to server (%s).",m_strServerIPAddress.c_str());
AfxMessageBox(strTmp);
return FALSE;
}
return pClientSocket;
}
//then I did
CSocket m_pClientSocket;
m_pClientSocket=new CSocket;
m_pClientSocket = Connect();
//then I used the m_pClientSocket->Receive() func that worked
//now I created a secondary thread using beginthreadex()
//then I called the m_pClientSocket->Receive()func in the secondary thread func threadproc()in a while loop ..it seems that evrytime I call the Receive method it raises the Debug assertion failed exception ..if I ignore the error I continues to execute the receive func of CSocket but after quite some delay..
Is there ant way out of this problem...
Thanx and Regards
Junni
|
|
|
|
|
As Rage pointed, you should provide more complete information about your problem. Which assertion failed? This is important info. Some pseudo-code would be useful for helping you too.
A common error on multithreaded CSocket code is made with CSocketServer; from MSDN:
"
If you want to handle this connection in a different thread, just passing the MFC object to the thread will not work correctly. To correctly set up everything for the connection to be handled in a different thread, the following steps are required:
1. Detach the socket handle from the CAsyncSocket object in the thread where the connection was accepted.
2. Pass the socket handle to the thread.
3. In the thread, attach this handle to a CAsyncSocket derived object.
"
I see dumb people
|
|
|
|
|
I vaguely remember comming across this problem before. I just opted to use socket handle as I was under presure to get the project finished on time. I never had the chance to go back and figure it out. If you figure it out I'd be grateful if you posted the answer here.
Regards,
Brian Dela
|
|
|
|
|
|
yes it is a worker thread and can u provide some info about the Message Pump and how to implement that..a snippet of code would do a lot..
Thanx
junni
|
|
|
|
|
does the error have something to do with m_hash?
Rob
|
|
|
|
|
|
I am trying to write a program that writes pure hexadecimal text into a file.
For example I want to write in the figure 0x00010000 into the file. However I have tried using streams but when I look at the file the contents in a hex viewer the contents are 34 38 for a 0 whereas I would like there to be 00 01.
Does anybody know of a way to do it using std::streams?
|
|
|
|
|
If you are writing "text" into a file, then it wouldn't matter if it was hex or not ... everything will be characters. When looked at in a hex viewer, you will be seeing characters, regardless of what they represent.
If you want the resulting file to be viewed with a hex viewer and to look like hex bytes, then you will want to write the data to the file in binary.
Good luck,
Dave
"You can say that again." -- Dept. of Redundancy Dept.
|
|
|
|
|
Hi code gurus,
I'm trying to find out how to retrive the desktop background (a.k.a. desktop wallpaper) to use it in my applications (just the wallpaper without the desktop icons or any other things). Also, I want to change the wallpaper bitmp (in memory by retrieving the bitmap handle or a pointer to the bitmap). For example, I want to write a screen saver that utilizes the desktop background to apply some graphics effects on the background bitmap. Also I want to use it in other applications .. I want to change the bitmap itself e.g. draw a clock on the bitmap. I don't want to load the wallpaper bitmap file, I know how to do this but I want to get the wallpaper from memory, is this possible?
Thanks in advance ...
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|