|
one of those #includes is defining its own version of new ?
|
|
|
|
|
but why:
#include <fstream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
----can compile success?
|
|
|
|
|
Hello all,
I am working on my homework for my C++ programming class and I am hitting a brick wall. Everywhere I look online does not seem to have any information in which I can apply to my program. I am supposed to use a "while" loop which I am, and thats working.
The part which is not working is the users input should add it to the previous entry, and after the user types "0" it will display the sum on the screen. I have looked at the following references to try and make sense of things:
http://www.cplusplus.com/reference/std/numeric/partial_sum/[^]
http://www.cplusplus.com/reference/std/numeric/accumulate/[^]
http://frank.mtsu.edu/~csci117/manual/lab7/lab7.html[^]
I am trying very hard to understand this, but I can not find a good online reference of the libraries. www.cplusplus.com is very good but dosent have the info I need, or more than likely I can not find it.
But here is the code to which I am trying to correct:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int userinput;
int sum = 0;
sum = sum + userinput;
while (userinput != 0)
{
cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
cin >> ++userinput ;
}
cout << "The sum is:" << sum << endl;
return 0;
}
Thank's to all in advance for pointing me in the correct direction so I can figure this out. I should have put this at the top: I DO NOT WANT THE ANSWER, I WOULD PREFER THE REFERENCE TO FIND MY OWN.
|
|
|
|
|
Outside the loop you should initialize the variable "userinput". The statement
sum = sum + userinput
before the loop should be removed. This statement should appear somewhere inside the loop. Also, for this you can use the += operator instead. The ++userinput inside the loop should just be userinput without the ++ operator.
The variables "userinput" and "sum" are not "Global Variables". They are local to the function "main".
You need to work out the details.
|
|
|
|
|
Thank you very much. I got it working now. I read over this while I was at dinner with the wife and figured it out at the dinner table!
here is the working code:
#include <iostream>
using namespace std;
int main()
{
int userinput;
int sum = 0;
while (userinput != 0)
{
cout << "Please enter a number. When you are finished, type 0 and press Enter." ;
cin >> userinput ;
sum = sum + userinput;
}
cout << "The sum is:" << sum << endl;
return 0;
}
|
|
|
|
|
Even though this program works, you are relying on the variable userinput having an initial non-zero value. The compiler actually does not always guarantee that. To make the program robust, I suggest you take control on the values of variables and not rely on some random factors.
Good luck and happy programming!
|
|
|
|
|
Now that the program adds the numbers and provides the sum at the end, I need to provide a means of displaying how many times a user input something.
The example output would be:
Sum = 134
Total numbers of inputs = 8
So I have been looking to find something on counters. I have looked at multiple web sites yet again and can not find anything. I have fixed my previous code and it is listed below. Once again, I DO NOT WANT THE ANSWER, I WANT THE REFERENCE SO I CAN LOOK IT UP MYSELF. Being pointed in the right direction helps me most.
Thank you all again.
#include <iostream>
using namespace std;
int main()
{
int userinput;
int sum = 0;
int count = 0 ;
cout << "Roberts Number program v1" << endl;
cout << endl;
cout << endl;
cout << "When you are finished, enter '0'" << endl;
cout << endl;
while (userinput != 0)
{
cout << "Input a number, then press enter. " ;
cin >> userinput ;
sum += userinput;
}
cout << "The sum is:" << sum << endl;
return 0;
}
|
|
|
|
|
rbwest86 wrote: counters
A counter is just a variable that holds a number. So per your requirement I will reword it to provide the hint that you need...
Each time that the user enters a number, increase the counter, then when the user is done entering numbers, output the number of times that the user entered a value.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|
Hello people!
Here's a simplified version of what i am trying to do (sorry for the long code):
class CTestClass
{
public:
void AMethod(int x);
BOOL BMethod(char y);
};
template<typename TParamType>
class TTestTemplate
{
typedef void(CTestClass::* LPFN_AMETHOD)(TParamType);
typedef BOOL(CTestClass::* LPFN_BMETHOD)(TParamType);
bool fIsAFn;
union {
LPFN_AMETHOD fnA;
LPFN_BMETHOD fnB;
};
public:
TTestTemplate(LPFN_AMETHOD pFn):fnA(pFn), fIsAFn(true ) {}
TTestTemplate(LPFN_BMETHOD pFn):fnB(pFn), fIsAFn(false) {}
void Call(CTestClass *pInstance, TParamType val);
};
template<typename TParamType>
void TTestTemplate<TParamType>::Call(CTestClass *pInstance, TParamType val)
{
if (fIsAFn) (pInstance::*fnA)(val);
else (pInstance::*fnB)(val);
}
At the highlighted line i am getting an INTERNAL COMPILER ERROR, i googled around and this seems to be a bug in the compiler (am not absolutely sure though), so my question is: does anyone maybe know of any workaround for this? I am using VC++ 2003 (using a newer version of VC++ is sadly not an option since this is out of my hand, i am stuck with it). If i could somehow get it to work it would simplify my life a lot...
[ANSWER] Ok, seems to be that i am trying to call the methods thorough the pointers wrong, if i use this:
(pInstance->*fnA)(val); instead of the :: version it compiles and runs fine. So i guess the bug is that it does not give any usefull error not that it can't "eat" function pointers and templates together. What are the odds...[/ANSWER]
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
modified on Tuesday, January 19, 2010 12:56 PM
|
|
|
|
|
pInstance is class instance. Did you ever see any people use it like:
pInstance::
For a class pointer, using "pInstance->" is correct.
"::" is just for "class" or "namespace"
|
|
|
|
|
Yes, as you can see on my post i realized that already, but thank you, i would have noticed that sooner if the error message i got would have been something like "don't use :: on that, you moron" instead of "duh, compiler is broke, boohoohoo"...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
|
|
|
|
|
Does anyone have the book "Programming Microsoft Visual C++ 5th Edition, David J. Kruglinski, George Shepherd, and Scot Wingo"? I bought the book secondhand but it never came with a CD because it was lost. I manage to type up the code listings to try out the examples. However am stuck on chapter 12: Windows Message Processing and Multithreaded Programming. Apparently the example 12B & 12C relies on 12A code, however it doesn't do complete listings for 12B or 12C but vaguely describes changes. I don't think it's explained all changes because I can't get it to compile.
Just wondering if anyone had the CD (code listing files) for this book?
|
|
|
|
|
Hi all i am using the method InternetOpenUrl with a callback
then i can tell when status complete that proxy authentication is needed
in MSDN it says to call InternetSetOption with the proxy user name and password and then call resend
the problem is that i don't know how to call resend because if i call InternetOpenUrl again then it says again when complete that proxy authentication is required
???
can any1 help?
Interface basics click here :
http://www.codeproject.com/com/COMBasics.asp
don't forget to vote
|
|
|
|
|
the trick was not to call again internetopenurl (since it opens a new session), but to call httpsendrequest...
Interface basics click here :
http://www.codeproject.com/com/COMBasics.asp
don't forget to vote
|
|
|
|
|
Hi.
I am trying to stream audio with waveOutWrite(). I have a server which gets continuously packets with voice data. What I do is playing data with waveOutWrite() and then in callback function when the playing is done I call to waveOutWrite() with next packet (there is no such situation that there are no ready packets). The problem is that because of a very small size of packets and so a large amount of packets there is a small gap between them, so I dont get a clear playing. How can I stream it more smoothly?
I am not a genius, but shed more sweat!
|
|
|
|
|
When do you start to play a new packet?
In general you should use at least two buffers where one is filled and queued for play with waveOutPrepare & waveOutWrite while the other buffer is still playing.
I prefer 5 or more buffers for that because also with 2 buffers you hear sometimes a gap.
Maybe you could post your callback or the function where you prepare and write.
Greetings
Covean
|
|
|
|
|
thx!
I am not a genius, but shed more sweat!
|
|
|
|
|
Could you give me your code on how to play multi buffers with waveOutWrite?
I am not a genius, but shed more sweat!
|
|
|
|
|
I would take a look at this CodeProject-Article.
It helped me to work out how to handle audio playback.
Hope that helps.
Greetings
Covean
|
|
|
|
|
Aric Green wrote: The problem is that because of a very small size of packets and so a large amount of packets there is a small gap between them...
As well as multiple buffers as suggested above by Covean, you should try to use larger packets, to reduce the amount of unnecessary processing you're doing. Using very small packets will be more likely to cause you problems, as you're experiencing.
Days spent at sea are not deducted from one's alloted span - Phoenician proverb
|
|
|
|
|
Thank you answay!Covean's reply just to the point.I found my fault,thx again!
I am not a genius, but shed more sweat!
|
|
|
|
|
hi
can the tutorial in learn vc++ 21 days about socket programming work without the code related to SetParent(this). I am implementing the same program but not in a dialog or a view class.
thanks
Darshi
|
|
|
|
|
Yes it will work fine until the GetParent() call (i.e. dumb question-dumb answer)...
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]
|
|
|
|
|
well u mite think its dumb.
The original code goes like this
void CMySocket::SetParent(CDialog *pWnd)
{
m_pWnd = pWnd;
}
<pre>
I wanna modify it to
void CMySocket::SetParent(CWeigh *pWnd)
{
m_pWnd = pWnd;
}
Weigh is a class created for my program which has no base class. I have made all the declarations correctly. When i add the header file of CWeigh Class to the header file of Socket class i get errors.
Plz help, and hence the question of eliminating SetParent
|
|
|
|
|
OK, things are getting a little better now.
In the original code (you have it, I haven't) the CMySocket::m_pWnd member is possibly a pointer to a CDialog object while you're passing a pointer to CWeight one, hence (probably) the compile errors.
To get better help, you should post, at least, the exact error message and the line of code wherein the error occurs.
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]
|
|
|
|