|
int smallest = 0;
if (j < smallest) { smallest = j; }
Have a look at those 2 lines, and think a little bit more about what is going on.
If you're not certain, just "run" your program on paper. (yeah, pen and paper).
This signature was proudly tested on animals.
|
|
|
|
|
[code]
// ints.cpp
#include <iostream.h>
int main ()
{
int i = 0;
int j,totals,x;
double avg;
int k = 0;
int largest = 1;
int smallest = 1;
int l;
do
{
cout << "Enter a number (enter zero to exit): ";
cin >> j;
i = i + j;
k = k + 1;
if (j > largest) {largest = j; }
if (j < largest) { smallest = j; }
}
while (j != 0);
totals = k - 1;
avg = double(i) / (double(k) - 1);
cout << "\n\n\nThe number of numbers is " << totals << endl;
cout << "The average is " << avg << endl;
cout << "the largest number is " << largest << endl;
cout << "The Smallest Number is " << smallest << endl;
cin.ignore(0);
system("PAUSE");
return 0;
}
[/code]
this is the code i currently have i wrote it out and thought i need j to be the standard input, then i would want if smallest is greater than j i would want the smallest to be j then if j was less then smallest i would want it to be replaced, i dont know if i explained that well but thats what i have so far...
|
|
|
|
|
You should really indent and beautify the code.
It make very easy for another person to look at.
This is not at all readable.
«_Superman_»
|
|
|
|
|
ok so i got it to this:
[code]
// ints.cpp
#include <iostream.h>
int main ()
{
int i = 0;
int j,totals,x;
double avg;
int k = 0;
int largest = 1;
int smallest = 99;
int l;
do
{
cout << "Enter a number (enter zero to exit): ";
cin >> j;
i = i + j;
k = k + 1;
if (j > largest) {largest = j; }
if (j < smallest) { smallest = j; }
}
while (j != 5);
totals = k - 1;
avg = double(i) / (double(k) - 1);
cout << "\n\n\nThe number of numbers is " << totals << endl;
cout << "The average is " << avg << endl;
cout << "the largest number is " << largest << endl;
cout << "The Smallest Number is " << smallest << endl;
cin.ignore(5);
system("PAUSE");
return 0;
}
[/code]
which works but the problem being i need to ignore a ZERO ( 0 ) instead of a 5 :-/ but whenever i put a 0 as the stopping then it doesnt work...
|
|
|
|
|
Did you mean it doesn't work if you put 0 in place of 5 here - while (j != 5); ?
«_Superman_»
|
|
|
|
|
exactly if i change
while (j != 0);
cin.ignore(0);
to
while (j != 5);
cin.ignore(5);
the program works, but problem being is i need the number to be 0 to end the program...
|
|
|
|
|
1/ Please use the code block formatting, and the preview button - you'll be amazed how much easier your code is to read.
2/ As suggested, run your program with a pen and paper. You'll be surprised how much it helps.
3/ I'll give you a big clue you're already been given.
int smallest = 0;
if (j < smallest) { smallest = j; }
3a/
Think about how many numbers are smaller than zero. Are the numbers you've entering small than zero?
Changing it to 1 only makes things better than one.
3b/ You'll have the same problem with largest, if you type in all negative numbers.
Enjoy,
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Initialize smalltest to the largest 4 byte integer :
int smallest = 4294967295 ;
if (j
|
|
|
|
|
*I* know that... though I'd recommend using MAX_INT...
But I was trying to lead the obviously a homework question in the right direction.
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Hi
In my MFC dll I have this code in function:
HINSTANCE hInstance = AfxFindResourceHandle(MAKEINTRESOURCE(iID), RT_RCDATA);
return ::PlaySound(MAKEINTRESOURCE(iID), hInstance, SND_RESOURCE | SND_ASYNC | SND_NODEFAULT);
wave file is in resource of the dll, but when I call it from the exe, it can't play it, because it can not find it, (there is no RT_WAVE type!)
how can I play a wave file that is in a dll file?
Thanks
www.logicsims.ir
|
|
|
|
|
Hi,
I have a Multiline Dialog Box would anyone have the sequence
of Win32 SDK API calls to calculate/set the number of characters in a
Line of a Multiline Dialog Box
BTW I am using Rich Edit 2.0 though I don't think this has any
bearing on my question
|
|
|
|
|
i guess it's done automatically
Don't try it, just do it!
|
|
|
|
|
what about CRichEditCtrl::GetTextLength() ?
how dumb of me... shoulda read the whole thing.
what about getting the whole text and looking for CR-LF and counting the characters between them ?
This signature was proudly tested on animals.
|
|
|
|
|
I am using Plains old C so EM_LINELENGTH
But if I cann't fit the Number of Characters I want on the Line
I would to some how want to make the font smaller ???
I guess I shoud re-prhase the question if I for Instance want 12
characters on one line of a MultiLine Dialog Box How Can I
calculate/set the right Font ??
|
|
|
|
|
I'm useing MFC and have a SDI app with Doc/View architecture. The main window has got two pane horizontally. Now I'm gonna change their height programmatically; but how?
Thank you masters!
|
|
|
|
|
|
See here[^].
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
Hello everyone,
Anything wrong with my code below? I got the compile error!
typedef unsigned char BYTE;
void foo(char* & p)
{
return;
}
int main()
{
BYTE * buffer;
foo ((char*)buffer);
return 0;
}
thanks in advance,
George
|
|
|
|
|
this isnt strange as foo required char*& and not char*.
Don't try it, just do it!
|
|
|
|
|
My confusion is reference type is the same as original type, for example when type Foo is needed, we could pass Foo or pass Foo&. So, I think then char* & is needed I could pass char*? What is wrong with my understanding?
|
|
|
|
|
BYTE is an unsigned char (8 bit)
char* is a pointer (32 bit)
When converting BYTE to char* then it needs to create a temporary variable. It is not possible to make return-by-reference with temporary variables.
Instead make the conversion from BYTE to char* by using a local variable, and then pass that local variable to the function.
|
|
|
|
|
I agree with your points, but confused. Since I am using BYTE* not BYTE.
Anyway, could you show me your code please? I think code clarifies everything.
regards,
George
|
|
|
|
|
Where is he converting a BYTE to char*? He's converting a BYTE* to char*. In other words, unsigned char* to char*. I think there's no problem with the pointer size.
this is enough:
foo ((char*&)buffer);
|
|
|
|
|
It's all to do with the semantics of casting - you can't pass a reference to something you've produced with a cast, even if the size and location of the thing is the same.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
You must call it as below
char* pp = (char*)buffer;
foo(pp);
«_Superman_»
|
|
|
|