|
thank you 1谢谢!where are you ?你在那个国家?
|
|
|
|
|
|
Ok here is the goal of the program:
Create a C++ Program that acts as a calculator for the user. The program should start off by printing, to the screen, a menu that offers the user one of five options. The menu should look somethings like this, but feel free to dress it up any way you want to make it look nice.
1.Add
2.Subtract
3.Multiply
4.Divide
5.Exit
The user wil then select one of the options. Then the program must prompt the user forthe input data and print the results to the screen. After the user has found the desired results, the menu should then reappear to allow the user to do another calculation. Do not let the user out of the program until the user has typed 5 for exit.
Rules: The program must use at least 5 functions, not including MAIN.One funtion will be for the menu and one function for each of the calculations.
Input for the program will be data that is in the integer range, and you can make the assumption that I will NOT type 0 to force a divide by 0 error ( Nice instructor eh?? . However, if the user types in a menu item number that does not appear, then the program should return the user an error message that they have typed an incorrect number and allow the user to retype a valid number.
Ok with all that said below is the source code that I have written in MS Visual C++. I am thinking I am running into problems with my functions, although I am not to sure. And thanks in advance for anyone who takes the time to offer some help.
#include <iostream>
#include <conio.h>
using namespace std;
int sum = 0;
int ssum = 0;
int msum = 0;
double dsum = 0;
int x = 0;
int y = 0;
int menu();
int c = menu();
int main()
{
do
{
int menu();
if(c == 1)
int add();
else if(c == 2)
int sub();
else if(c == 3)
int multi();
else if(c == 4)
double div();
}
getch();
return 0;
}
int menu()
{
int sel = 0;
cout<<"Please select a menu choice..."<<endl
<<"1. Add"<<endl
<<"2. Subtract"<<endl
<<"3. Multiply"<<endl
<<"4. Divide"<<endl
<<"5. Exit program"<<endl;
cin>>sel;
if(c = 5)
cout<<"Thank you for using the calculator!"<<endl;
else if(c > 5 && c < 1)
cout<<"You have entered an invalid selection!"<<endl;
return sel;
}
int add()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
sum = x + y;
cout<<"The answer is "<<sum<<endl;
return sum;
}
int sub()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
ssum = x - y;
cout<<"The answer is "<<ssum<<endl;
return ssum;
}
int multi()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
msum = x * y;
cout<<"The answer is "<<msum<<endl;
return msum;
}
double div()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
dsum = x / y;
cout<<"The answer is "<<dsum<<endl;
return dsum;
}
*Edit* Hopefully this helps now
|
|
|
|
|
Hi Fightingbee, welcome to CP. It's hard to read your code, please edit the post and either wrap your code in a <pre> block, or check the "Display this message as-is (no HTML)" box so spacing is preserved.
From a quick glance, your function calls are wrong.
int add();
is only a prototype, telling the compiler that a function add() exists and returns an int . When you want to call the function, you write
add();
--Mike--
"I'm working really, really fast at the moment, so a 3 minute outage becomes, due to time dilation, a 5 minute outage."
-- Chris Maunder, relativistic system administrator
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
|
|
|
|
|
Well I tried pulling the int out of the int add(); and likewise functions ( when they are called and when I am defining them and the compiler threw up about 7 errors at me. So I am not sure exactly what it is I am to do.
|
|
|
|
|
Fightingbee wrote:
and the compiler threw up about 7 errors at me.
What errors did you get?
Michael
'War is at best barbarism...Its glory is all moonshine. It is only those who have neither fired a shot nor heard the shrieks and groans of the wounded who cry aloud for blood, more vengeance, more desolation. War is hell.' - General William Sherman, 1879
|
|
|
|
|
Well ok here is the copy of the changed code,
#include<iostream>
#include<conio.h>
using namespace std;
int sum = 0;
int ssum = 0;
int msum = 0;
double dsum = 0;
int x = 0;
int y = 0;
int menu();
int c = menu();
int main()
{
do
{
menu();
if(c == 1)
add();
else if(c == 2)
sub();
else if(c == 3)
multi();
else if(c == 4)
div();
}
while(c == 5);
cout<<"Thank you for using the calculator!"<<endl;
getch();
return 0;
}
menu()
{
int sel = 0;
cout<<"Please select a menu choice..."<<endl
<<"1. Add"<<endl
<<"2. Subtract"<<endl
<<"3. Multiply"<<endl
<<"4. Divide"<<endl
<<"5. Exit program"<<endl;
cin>>sel;
if(c > 5 && c < 1)
cout<<"You have entered an invalid selection!"<<endl;
return sel;
}
int add()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
sum = x + y;
cout<<"The answer is "<<sum<<endl;
return sum;
}
int sub()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
ssum = x - y;
cout<<"The answer is "<<ssum<<endl;
return ssum;
}
int multi()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
msum = x * y;
cout<<"The answer is "<<msum<<endl;
return msum;
}
double div()
{
cout<<"Please enter a number..."<<endl;
cin>> x;
cout<<"Please enter your second number..."<<endl;
cin>>y;
dsum = x / y;
cout<<"The answer is "<<dsum<<endl;
return dsum;
}
And here are the errors it is throwing up,
--------------------Configuration: assign4 - Win32 Debug--------------------
Compiling...
assign4.cpp
D:\Assignment 4\assign4.cpp(33) : error C2065: 'add' : undeclared identifier
D:\Assignment 4\assign4.cpp(35) : error C2065: 'sub' : undeclared identifier
D:\Assignment 4\assign4.cpp(37) : error C2065: 'multi' : undeclared identifier
D:\Assignment 4\assign4.cpp(39) : error C2660: 'div' : function does not take 0 parameters
D:\Assignment 4\assign4.cpp(68) : error C2373: 'add' : redefinition; different type modifiers
D:\Assignment 4\assign4.cpp(81) : error C2373: 'sub' : redefinition; different type modifiers
D:\Assignment 4\assign4.cpp(94) : error C2373: 'multi' : redefinition; different type modifiers
Error executing cl.exe.
assign4.obj - 7 error(s), 0 warning(s)
|
|
|
|
|
OK First of all you should make all of your int be double.
Whats up with your spacing anyways it's like 20 spaces for a tab.
Your making it too complicated for your self. Here is what I made when I was a beginner hopefully it will teach you something.
#include <iostream>
using namespace std;
float inputOne, inputTwo, answer;
char operator_, yn;
int main()
{
while (yn != 'n')
{
cout << "First number + - / * ^ second number\n";
cin >> inputOne >> operator_ >> inputTwo;
if (operator_ == '+')
answer = inputOne + inputTwo;
if (operator_ == '-')
answer = inputOne - inputTwo;
if (operator_ == '*')
answer = inputOne * inputTwo;
if (operator_ == '/')
{
if (inputTwo == 0)
{
cout << "Cannot divide by 0";
}
else
answer = inputOne / inputTwo;
}
if (operator_ == '^')
{
answer = inputOne;
for (int i=2; i<=inputTwo; i++)
answer = answer * inputOne;
}
cout << endl;
cout << inputOne << " " << operator_ << " ";
cout << inputTwo << " = " << answer;
cout << "\n\nSolve another? <y><n> ";
cin >> yn;
cout << "\n";
}
return 0;
}
[It is possible to represent everything in this universe by using 0 and 1]
I was born intelligent
Education ruined me!.
An idea is useless until it has been implemented.
|
|
|
|
|
How could I change the Font for the Application. For the dialogs it is easy, but for my Statusbar / Menu I didn't find any good solution by now. Could anyone help ?
|
|
|
|
|
我觉得状态栏的文字给对话框的文字一样呀!你同样可以采用在对话框中的方法
下面是我的做法:
HBRUSH CXXXX::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
/************************************************************************/
if ((nCtlColor == CTLCOLOR_STATIC ))
{
pDC->SetBkMode(TRANSPARENT);
int nID = pWnd->GetDlgCtrlID();
if( (nID == CONTROL'S ID ))
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(TRANSPARENT);
//pDC->SetBkColor(RGB(255,255,255));
}
HBRUSH hbr = ::CreateSolidBrush(RGB(248,251,255));
return hbr;
}
// TODO: Return a different brush if the default is not desired
/************************************************************************/
return hbr;
}
|
|
|
|
|
The color of the Font is not important. The Font itself is. In this case I want to use Arial Unicode MS by default.
I don't want to change the Windows Settings for this.
|
|
|
|
|
for statusbar, use the SetFont member function.
Read mike dunn's FAQ first.
Hari Krishnan
|
|
|
|
|
I think I should explain :
I tried the SetFont some times at different location, everytime with the same result : 1. I don't change anything or 2. It change the Font to anything like default System Font. But I never got the Font I want.
I checked and tied several Onlinetuturial to change the Font in StatusBar, also the one in the MSDN (mainly thought for Bitmaps in CStatusBar). Everytime only this results. Cause I can use the Font in all my Dialogs (thanks to Visual Net, I could choose this in the Resource Editor), I thought there may be an easy way also for the MainFrame. That could help also with the title of the dialogs/windows (maybe).
Is there any help for me ?
|
|
|
|
|
Hi All
Not sure what this problem is called but I've stored information in an array
of integers and stuck in OnPaint an attemp to redraw the rectangles and
spots of colour using SetPixel. When I un-minimize the information ie the
drawings are not re-appearing. Am I placing the "re-draw" in the wrong
function? OnPaint sounded reasonable for Un-minimzing.
Terry
|
|
|
|
|
how is that possible to draw text contained in a Cstring object in spiral form in a SDI application in view window.Object can be accesed in OnDraw function of MFC View class.What r the function those describe the features in which we can draw characrters in any direction.
HUMAYUN AJMAL
|
|
|
|
|
Image file I suppose.
I am interested in knowing as well.
|
|
|
|
|
I think you might be able to do it using OpenGL programming.
|
|
|
|
|
the different between the framework of dll and application
in the VC++ 6.0??
a good man in china
|
|
|
|
|
YEAH,WIN32 DLL AND WIN32 APPLICATION
a good man in china
|
|
|
|
|
A dll is a dynamic link library of functions and variables that are loaded dynamically by an application or service into its address space at run time.
John
|
|
|
|
|
LOL
[It is possible to represent everything in this universe by using 0 and 1]
I was born intelligent
Education ruined me!.
An idea is useless until it has been implemented.
|
|
|
|
|
I've got a component which uses a library which MUST be properly closed when you exit from using it. It takes a couple of seconds.
Is it ok to create a Thread to do all this work (as long as I don't modify or read memory inside the component from within the thread) in the Destructor of the component? And exit the component immediately?
|
|
|
|
|
Paul Farry wrote:
Is it ok to create a Thread to do all this work (as long as I don't modify or read memory inside the component from within the thread) in the Destructor of the component?
Yes, it's fine. As you mentioned, watch out for accessing memory that has been deleted.
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"
|
|
|
|
|
Hi,
I was wondering if anyone else has seen this problem:
When I right click and open up a context menu in a CWnd sub-classed dialog, then go into a submenu my mouse cursor goes from the arrow to the hourglass and never reverts back to the arrow cursor until after I close the dialog box.
It doesn't seem to matter which way I create the menu (either through resources or purely by code) and I have tried overwritting the OnSetCursor function which always sets the mouse to the arrow.
I know that OnSetCursor is being called because outside of a menu the cursor is not an arrow.
Here is the sample of my code:
for cursor override:
BOOL CCMFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (message == 0)
{
HCURSOR hCursor = LoadCursor(NULL, IDC_ARROW);
SetCursor(hCursor);
return TRUE;
}
return CWnd::OnSetCursor(pWnd, nHitTest, message);
}
for menu creation:
CMenu mnuTop;
mnuTop.LoadMenu(IDR_POPUP_MENU);
CMenu* pPopup = mnuTop.GetSubMenu(0);
ASSERT_VALID(pPopup);
pPopup->TrackPopupMenu(TPM_RIGHTBUTTON |
TPM_LEFTALIGN, Point.x, Point.y, this, NULL);
Thank you for any insight into what could be causing this!!
Crystal
|
|
|
|
|
dWorkVan wrote:
When I right click and open up a context menu in a CWnd sub-classed dialog, then go into a submenu my mouse cursor goes from the arrow to the hourglass and never reverts back to the arrow cursor until after I close the dialog box.
Seems to mimic how Windows 2000 Explorer.exe and its Property dialog behaves - seems you are in good (or bad, depending on how you look at it) company.
Have you registered a cursor for your dialog? Do you handle all the WM_* messages as you should?
To give you a hint: The problem isn't with the manu code (no matter what it does) - the problem is with setting the right cursor when the focus returns to the dialog. If the hCursor is NULL, guess what it *can* set it (or *not* set it) to.
|
|
|
|