Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,314 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,065 people online right now. Registration is fast and FREE... Join Now!




Calculator Application - User Input Methods

 
Reply to this topicStart new topic

Calculator Application - User Input Methods, any one please

ladiesman
post 12 Mar, 2008 - 08:50 PM
Post #1


New D.I.C Head

*
Joined: 9 Feb, 2008
Posts: 33

Write functions to:
void clearscreen(void); /* Clears the screen for update, hint call system("clear") */
float calc(float num1,float num2,char operator); /* calculate the result of num1 operator num2 */
any other functions which you deem necessary.
Write a main program which will display a title line centered at the top of the screen

like: Fred's Calculator

and basically sit and wait for input from the keyboard.

Your program will respond to the numeric keys and form up a number on the result line, below the title (refreshing on every keypress)

Your program will also respond to the basic function keys... + - * / taking the previously entered value and then performing the action on the next entered value... just like your handheld calculator.

The enter key will replace the = key in the program.

The q key will shut down the program cleanly.

Make certain your program is well documented and executes correctly, make certain it accepts all keyboard input but then responds only to the ones listed above and ignores the rest. Use getch() to pull in input from the keyboard on a character by character basis.
heres my program to far:
i need to know how to clear screen and also how to get the numeric keys without pressing enter.
CODE

#include<math.h>
#include<stdio.h>
#include<curses.h>

int main(void)
{
float num1,num2,answer,i;
char c;
printf("\t\t\t""kieran's calculator!!!\n");
printf("\t\t\t\t""%f\n",answer);
printf("A Adition\n");
printf("S Subtraction\n");
printf("M Multiplication\n");
printf("D Division\n");
printf("R Squareroot\n");
printf("P Percentage\n");
printf("Q Quit\n");
c=getchar();
switch (c)
{
case 'A':
case 'a': addition:
printf("Enter two numbers:\n");
scanf("%f",&num1);
scanf("%f",&num2);
answer=num1+num2;
break;
case 'S':
case 's': subtraction:
printf("Enter two numbers to subtract:\n");
scanf("%f",&num1);
scanf("%f",&num2);
if (num1>num2) {
answer=num1-num2;
}
else  if (num1<num2){
answer=num2-num1;
}
break;
case 'M':
case 'm': multiply:
printf("Enter two numbers to multiply:\n");
scanf("%f",&num1);
scanf("%f",&num2);
answer=num1*num2;
break;
case 'D':
case 'd': division:
printf("Enter 2 numbers to be divided:\n");
scanf("%f",&num1);
scanf("%f",&num2);
if (num1>num2) {
answer=num1/num2;
}
else  if (num1<num2){
answer=num2/num1;
}
break;
case 'R':
case 'r': squareroot:
printf("enter number to be square rooted:\n");
scanf("%f",&num1);
answer = sqrt(num1);
break;
case 'P':
case 'p': percent:
printf("Enter first number to be the percent and the second number to be\n");
scanf("%f",&num1);
scanf("%f",&num2);
answer = (num1*num2)/100;
break;
case 'Q':
case 'q': return;
}
printf("%f\n",answer);
}
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 13 Mar, 2008 - 06:55 AM
Post #2


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


Why are you including the curses libraries if you're not using curses? Initializing the screen in curses will clear it.

To get a key without having to hit "enter", I use:

cpp

int myKeys;

myKeys = getch();

if( myKeys == 48 )
cout << "You entered a 0!" << endl;
else if( myKeys == 49 )
cout << "You entered a 1!" << endl;
// Not putting any more here. I'm lazy.


It does store the actual scan code, and not the character pressed, but you can find out all the scan codes that match the characters at http://www.asciitable.com/
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:20AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month