|
How can I get the program below to reject anything but numbers when the program requests a number?
//Calculator program
#include <iostream>
using namespace std;
int main ()
{ long double var1;
long double var2;
char sign;
//Begin calculator loop. Will loop continuously until first
//number entered is a 0.
do {
cout << "Enter a number ";
cin >> var1;
cout << "Enter a sign ";
cin >> sign;
cout << "Enter a number ";
cin >> var2;
//The function that rejects anything but numbers here
//Begin calculator functions. +, -, /, *.
switch (sign){
case ('+'):
cout << var1 + var2 << '\n' << '\n';
break;
case ('-'):
cout << var1 - var2 << '\n' << '\n';
break;
case ('*'):
cout << var1 * var2 << '\n' << '\n';
break;
case ('/'):
cout << var1 / var2 << '\n' << '\n';
break;
default:
cout << "You didn't enter a correct sign";
cout << '\n' << '\n';
break;
//If the user enters an unrecognized sign they will get a
//message that says: You ddn't enter a correct sign.
}
} while (var1 != 0); //If first number entered is a 0
//the program will terminate at the end
//of the loop. (The Do While loop)
}
|
|
|
|
|
int var;
cin >> var;
if (cin.fail())
cout << "Not a number!" << endl;
Robert-Antonio
"CRAY is the only computer, which runs an endless loop in just 4 hours"
|
|
|
|
|
What I'm trying to do, is to pass a 2-dimensional array as an argument to a function. In one trial and error case, I was able to get a clean compile, but the program crashed when run. In another case, the program produced one compile error stating that the subscript required an array or pointer type.
My feeling is that the one compile error is more like the truth. Here's the code.
short limit = ReadThisLevel( (&tree)[SZ][SZ] );
...
short ReadThisLevel(short* tree)
{
short i, j;
for(i=0, lim=0; i<lmt; ++i)
{
for(j=0; j<SZ; ++j)
{
node = tree[i][j];
}
}
return lim;
} Leaving the code the way it is (as shown in the sample here), but make the following change to that one line, produced this other compiler message, "illegal indirection"
node = *(*(tree+i)+j); FWIW, 'tree' is defined as:
short tree[SZ][SZ]; // SZ is of type 'const short'
Thanks for any insight.
William
Fortes in fide et opere!
|
|
|
|
|
WREY wrote:
// passing an array this way allows you to pass the size of it
short limit = ReadThisLevel( (&tree)[SZ][SZ] );
No it doesn't. This won't do anything like you're wanting to do. To get your program to compile properly, declare the parameter as "short** tree " and just pass "tree " to it.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Thanks for replying.
What you suggested, was one of several things I had tried prior to posting the question, and was one of the many results that produced a compile error (of one kind or another). (Take my word for it, I tried many different things, including the use of numbers to depict the size of the array.)
Lets give the size of the array (for simplicity sake) a size of 5. The compile error would then read, "cannot convert parameter 1 from 'short [5][5]' to 'short ** '." (That's the error received from trying your suggestion, which I myself had tried long before.)
With regards to your reason why the compiler would never do what I'm trying to accomplish, I have narrowed it down to one of three reasons. But before I get to what they are, allow me to point you to Lippman's "C++ Primer: 3rd Ed." page 348, first line (second sentence) where he states, "When the parameter is a reference to an array type, the array size becomes part of the parameter and argument types, and the compiler checks that the size of the array argument matches the one specified in the function parameter type." He then proceeds to give two examples: one correct and the other, incorrect (for the reader to understand what he's talking about).
Notice also, the compiler did not complain about the way I was passing that kind of parameter and argument. It complained about the way it was being accessed in the "called" function, which brings me around to one of my three reasons why I believe I'm not getting a clean compile.
1. I cannot pass multi-dimensional arrays (of ANY size) this way.
2. The compiler I'm using is out of compliance with the Standard.
3. Lippman is wrong.
Because I've had problems before with this compiler (MS VC6) being out of compliance with the Standard, that suspicion is very plausible. However, in this instance, I more tend to believe it's reason #1.
Bottom line: What you suggested did not work either.
Thanks anyway. I appreciate the suggestion.
William
Fortes in fide et opere!
|
|
|
|
|
Is there a way to get the red,green,blue of a specified pixel in MFC? I have been trying to figure this out for awile, so I'm guessing its not easy? Any help is appreciated.
-Dev578
|
|
|
|
|
Yes - if I get what you mean. You have a value of type RGB and you want to get the reg green and blue values from it?
You have the GetRValue, GetGValue and GetBValue functions that will extract the values from it.
|
|
|
|
|
Sorry, I may have worded the question badly. I am trying to get the RGB values from a pixel on the screen. I'm guessing I need to use the "GetPixel(HDC,int,int);" function. I know that the two int's are x and y. What is HDC? Any help would be appreciated.
-Dev578
|
|
|
|
|
Dev578 wrote:
What is HDC?
The handle of the Device Context your trying to get a value from.
Heard in Bullhead City - "You haven't lost your girl - you've just lost your turn..." [sigh] So true...
|
|
|
|
|
If your not sure what an HDC is then your in for a rough ride in windows programming.
An HDC is a handle to interact with a display surface (e.g. the screen). Every window has an HDC which you can get by using
HDC hdc = ::GetDC(HWND);
... use the hdc
ReleaseDC(HWND, hdc);
HWND, is the handle for the window you want to access the HDC of. In your case, you want to get a pixel colour on the screen. You can pass NULL as the HWND to return the HDC of the entire screem:
HDC hdcScreen = ::GetDC(NULL);
COLORREF = col = ::GetPixel(hdcScreen, x, y);
::ReleaseDC(NULL, hdcScreen);
You can then extract the individual RGB values from the COLORREF returned.
When working with HDCs, make sure you return them in the same state to the OS that you got them in (i.e. use SaveDC()/RestoreDC()), and please make sure that any you Get, you release otherwise as your program runs, it will slowely consume all available drawing resources, until at some point, windows will run out and start giving you very eratic behaviour.
Roger Allen - Sonork 100.10016
Roger Wright: Remember to buckle up, please, and encourage your friends to do the same. It's not just about saving your life, but saving the quality of life for those you may leave behind...
|
|
|
|
|
Hi!
You all certainly know the little icon in systray which opens the volume control, where you can enable/disable and set the volume of certain speakers (in my case, "wave", "front", "rear", "subwoofer" and "center").
Are there any API calls to programmatically set the volume of each speaker?
thanks in advance
modified 12-Sep-18 21:01pm.
|
|
|
|
|
This article[^] may be of some use.
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|
You want the mixerXXXX functions.
Look up mixerOpen on MDSN and go from there - there is a sample that does it for VB, shouldn't be too hard to convert.
|
|
|
|
|
Thanks!
I heard the mixer classes are a pain in the ass to use, but I'll give them a go.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I have some problem in writing An ATL COM Simple Server. When i try to make a new project i notice that some files where the main code must be are not created.
For example i write a project with name FirstCom, and i don't have FirstCom.h, and in FirstCom.cpp i have only this:
And that's why i think i have error, that my servers isn't registred(smth like this). I haveVisual Studio Net 2002 and WTL installed. Can it be the problem??? How to uninstall in that case WTL??? Or What To Do???Thanks.
|
|
|
|
|
That all looks like a standard VS.NET ATL app wizard generated code (using attributes)
You certainly shouldn't have a FirstCom.h, because all the code is added into the stdafx.h for the DLL.
After you've generated the dll framework, you need to add a new ATL Simple Object class. This is the COM object, for which you should have a cpp and h file for.
Have you added a simple object or did you just run the wizard?
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|
Yes, i'd added the simple object.
By the way, i've seen an example of minimal com server in MSDN and the had the normall code in .h and .cpp files.
So i still don't know what is the problemm.
Thanks
|
|
|
|
|
When you generated your project, you left the wizard attribute option set. This is why you don't see any code. It is all done by the new attribute stuff (which I still don't fully understand myself).
If you want to see the C++ code for your COM server, you will have to regenerate your project and make sure you to turn the Attribute checkbox off.
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|
I've just tried to write simple ATL server on another computer with Visual Studio Net. And Everithing passed OK.
May be it bacause of my computer??? I have a firewall installed on it... Now i think that problem is not in Visual Studio Net .
Thank you.
|
|
|
|
|
That what i have on my computer:
Error PRJ0019 : A tool returned an error code: "Performing registration".
And this happens nomater if i have chosen "Attributed" or not.
And on another computer i successfully built the project.
What is Wrong with my PC or my "Studio" ???
Thanks
|
|
|
|
|
Rassul Yunussov wrote:
What is Wrong with my PC or my "Studio" ???
Do you have admin privileges on that machine?
Have you tried manually registering the DLL from the command prompt
regsvr32 mydll.dll
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|
Thank you very much for "regsvr32" but i want to say something about it .
For the first - i couldn't find it on my computer . So i had to copy it from another .
And about visual studio NET - i still don't know why it didn't register itself this dll.
Thanks again.
|
|
|
|
|
hi all
after succeeding in creating tabbed pages in my application. I realised to my greatest dismay that i cant display e combo boxes (Cant display the page after adding >25 combo boxes) I really dn't understand the situation. where did i go wrong?
everything works perfectly fine with <25 boxes......
hope someone can help me here!
|
|
|
|
|
Do you get an error message or an ASSERT?
How are the combo-boxes created? Are they done via the resource editor or via a call to Create?
Your best bet is to step into the page creation code and find out the exact line the problem occurs.
Michael
But you know when the truth is told,
That you can get what you want or you can just get old,
Your're going to kick off before you even get halfway through.
When will you realise... Vienna waits for you? - "The Stranger," Billy Joel
|
|
|
|
|
oh... there's mo error when compiling. Its when i execute the program, the tabbed pages don't display correctly. I really don't know why. I created the combo boxes using the resource editor.
Page creation code? Do you mean the File View?
Thanks =)
|
|
|
|