|
Damn - you found me out!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hello everybody,
i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times.
Could you please help me to understand what i'm doing wrong?
Thanks a lot,
raeiko
<br />
<br />
<br />
#include <stdio.h><br />
<br />
int multiple ( int j, int z); <br />
<br />
int main( void ) <br />
{<br />
int num1, num2, x, i; <br />
<br />
for ( x = 1; x <= 5; x++ ){ <br />
printf( "Enter the first number:" ); <br />
scanf_s("%d", &num1 ); <br />
<br />
if ( num1 != 0){<br />
printf( "Enter the second number:" ); <br />
scanf_s("%d", &num2 ); <br />
} <br />
else {<br />
break;<br />
printf( "\nBroke from loop because num1 must be greater than 0\n" ); <br />
}<br />
}<br />
<br />
<br />
multiple( num1, num2);<br />
<br />
<br />
return 0;<br />
}<br />
<br />
int multiple ( int j, int z ) <br />
{ <br />
int y; <br />
<br />
int result;<br />
<br />
result = z % j;<br />
<br />
for ( y = 1; y <= 5; y++ ) <br />
if( result == 0){<br />
printf( "%d is multiple of %d\n", z, j );<br />
}<br />
else {<br />
printf( "%d is not multiple of %d\n", z, j );<br />
}<br />
<br />
return result;<br />
}
|
|
|
|
|
You need to put the multiple( num1, num2); call in the for loop - it's not in the loop in the code you've posted.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
If i don't ask too much,could you please show me? This thing has been driving me crazy for one week...and i guess this is still nothing in comparison to advanced C...I feel a bit stupid...
Thanks again,
raeiko
|
|
|
|
|
I see what you've done. You've got the loop in main and multiple, but you're passing single integers into multiple. There are two solutions (both built & tested under darwin-gcc-4.0.1):
- Call
multiple in the loop and remove the loop in multiple :
#include <stdio.h>
int multiple ( int j, int z);
int main( void )
{
int num1, num2, x, i;
for ( x = 1; x <= 5; x++ )
{
printf( "Enter the first number:" );
scanf("%d", &num1 );
if ( num1 != 0)
{
printf( "Enter the second number:" );
scanf("%d", &num2 );
}
else
{
break;
printf( "\nBroke from loop because num1 must be greater than 0\n" );
}
multiple( num1, num2);
}
return 0;
}
int multiple ( int j, int z )
{
int result;
result = z % j;
if( result == 0)
{
printf( "%d is multiple of %d\n", z, j );
}
else
{
printf( "%d is not multiple of %d\n", z, j );
}
return result;
} - Gather the numbers into two arrays and process them in one call to
multiple :
#include <stdio.h>
int multiple ( int* j, int* z, int n);
int main( void )
{
int num1[5] = {0};
int num2[5] = {0};
int x, i;
for ( x = 1; x <= 5; x++ )
{
printf( "Enter the first number:" );
scanf("%d", &(num1[x-1]) );
if ( num1[x-1] != 0)
{
printf( "Enter the second number:" );
scanf("%d", &(num2[x-1]) );
}
else
{
break;
printf( "\nBroke from loop because num1 must be greater than 0\n" );
}
}
multiple( num1, num2, x-1);
return 0;
}
int multiple ( int* j, int* z, int numValues)
{
int y;
int result;
for (y=0;y<numValues;++y)
{
result = z[y] % j[y];
if( result == 0)
{
printf( "%d is multiple of %d\n", z[y], j[y] );
}
else
{
printf( "%d is not multiple of %d\n", z[y], j[y] );
}
}
return result;
} Personally, I prefer the first one - why mess with arrays when you don't have to?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thank you so much!!! Now i see what was wrong
raeiko
|
|
|
|
|
IMHO your code has several flaws.
- you're treating
num1 and num2 in main (as well as j and z in multiple ) like they where array elements, but, in fact, they aren't. - in the array-like scenario, the
break on num1==0 is inappropriate, that 'exceptional' input would be better handled inside the multiple function (possibly with a continue ). - the line
raeiko wrote: printf( "\nBroke from loop because num1 must be greater than 0\n" );
is wrong: you're simply rejecting num1==0 , i.e. you code accepts negative num1 values. As already suggested by Stuart Dootson, would be probably better abandoning the array-like design: make multiply function acting on just two operands and call it inside the input loop.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Check out your multiple() function. Notice that it is called outside of the loop, so it is only going to work on the last two values of num1 and num2 .
Also, why does the multiple() function have a for loop?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
I have come across a program that displays a jpg
image, which can be opened in the usual manner.
However I noticed at start up it displays a default image loaded
from memory(resoucres).
The statement
m_pict.Load(IDR_MAINFRAME);
I thought that this sort of method could
work well,in effect with a program that plays sounds
(waveBox - from the articles), I have looked in the
resources and it allows you to load 'wave' files into it.
can anybody explain maybe why this is not working.
I have also tried including the resource.h
is there any oversite in my part
many thanks
Simon
|
|
|
|
|
simon alec smith wrote: can anybody explain maybe why this is not working.
Why what isn't working?
You can load any file type from resources - this page[^] describes the process reasonably well under the 'Creating a Resource List' heading:
- call FindResource or FindResourceEx with the resource name to get the resource handle;
- call LoadResource with the resource handle to get the global handle;
- call LockResource with the global handle to get a pointer to the resource data.
I've done this several times - it works really well.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi, Is it possible to avoid the Microsoft Visual c++ Runtimes when running a program compiled with Visual Studio 2008?
My program is dynamically linked to msvcr90.dll and msvcp90.dll and I would like to not having to redistribute these files. Thanks in advance.
|
|
|
|
|
santiageitorx wrote: Is it possible to avoid the Microsoft Visual c++ Runtimes when running a program compiled with Visual Studio 2008?
Check this out[^]
|
|
|
|
|
To paraphrase led mike - link statically with the CRT!!!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Stuart Dootson wrote: To paraphrase led mike - link statically with the CRT!!!
Accept that their are potential scenarios where that is NOT advisable. Hence the need to read the documentation. I know, most programmers reject the idea of reading but on the outside chance it could happen, I still go with that advice.
|
|
|
|
|
Sorry can you paraphrase that and be more compact about it? I got bored at Accept.
Regards,
--Perspx
"A refund for defective software might be nice, except it would bankrupt the entire software industry in the first year." -Andrew Tanenbaum
"Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer." -Fred Brooks
|
|
|
|
|
Yes, you're quite right - been there, done that, wondered about the crashes
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thank you guys I'll check it right now!
|
|
|
|
|
As mike so correctly reminded me, just be aware of when you need to use the DLL run-time rather than the .lib one.
In general, the DLL CRT is required when you need one instance of the C run-time to be shared across your executable and DLLs. In my experience, that's most often when you want to allocate things in a DLL that you then deallocate in the EXE (or vice versa, but it's usually that way round). You can work around that with explicit allocation/deallocation functions, but DAMN it's easier with a DLL CRT.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
I have developed one application which loads some jpg images and when I resize window the position of all these images gets changed. I have loaded a small image at bottom of right corner of screen (SDI application). When I restore the screen I am not able to see the same image in window. I am using GetclientRect function to calculate the screen size but after using this I am able to manage the images properly. I want to draw these images as per the screen size(e.g solitaire game). Please suggest how to do it.
Thanks
|
|
|
|
|
You shouldn't draw only the image on screen. Instead what you have to do is remember the data that should be displayed, and redraw the data when needed. If you are using an SDI application, part of the job is already done for you.
Let's take the solitaire game as an example: in your document class, you would store your current situation (e.g. what are the visible cards, the current available card in to be draw, ...). You do not store anything which is related to the screen (no images, no card position, ...). This is purely data.
Then whenever this data changes (e.g. the user moves some card), then you request the view to repaint itself. Everything is delegated to the view to set the cards to the correct position and so on. When the view is resized, it will also repaint itself in the same way.
In your situation, if the images disappear it's probably because your view is repainted and you didn't put any drawing code in the paint handler. A suggest you take a look at the Scribble example[^] on MSDN (it's a MDI application, but it will already give you a lot of feedback on how you have to do it for a SDI app).
|
|
|
|
|
Hello.
I am facing this problem, i have a parent dialog wich has a couple of buttons and edits boxes, now when i pass the mouse over a button a child modeless window should popup.
Now i have it all working, but when the child window appears the parent dialog loses focus, that is annoying, because if i am typing in the edit box and move the mouse over a button it loses focus because the child window appears.
Is there a way to solve this without losing focus in the parent window?
|
|
|
|
|
Do you have the child's parent and owner set correctly?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
i only set parent in the create method:
Create(MYDIALOG::IDD, this);
is this what you mean?
|
|
|
|
|
See here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"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
|
|
|
|
|
Try creating your popup window hidden and then show it with ShowWindow(SW_SHOWNOACTIVATE).
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|