EvilZone
Programming and Scripting => C - C++ => : ManiK October 16, 2012, 02:47:16 PM
-
#include <iostream>
#include <cmath>
int main()
{
using namespace std;
double answer;
double option;
double num1;
double num2;
do{
cout << "Enter operator <1=add 2=subtract 3=multiply 4=divide>" << endl;
cin >> option;
if (option == 1)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 + num2 << endl;
cout << endl;
}
else if (option == 2)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 - num2 << endl;
cout << endl;
}
else if (option == 3)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 * num2;
cout << endl;
}
else if (option == 4)
{ cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 / num2 << endl;
cout << endl;
}
else{
cout << "Enter the real operator you dingbat." << endl;
}
} while(.1);
system("pause");
}
This is a basic calculator.I am still learning C++.This is a program i made with help from some tutorial.The problem is when i enter a option and use it smoothly .After that i need to clear the screen,it keeps stacking after using operators.I am not sure if u understood my problem.I tried using system("CLS") but i failed.Help me.
Thank you.
-
try
include "stdlib.h"
when you do a system cls
another option might be to add multiple empty lines with a loop
(http://evilzone.org/data:image/gif,GIF89a%12%00%12%00%B3%00%00%FF%FF%FF%F7%F7%EF%CC%CC%CC%BD%BE%BD%99%99%99ZYZRUR%00%00%00%FE%01%02%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%04%14%00%FF%00%2C%00%00%00%00%12%00%12%00%00%04X0%C8I%2B%1D8%EB%3D%E4%00%60(%8A%85%17%0AG*%8C%40%19%7C%00J%08%C4%B1%92%26z%C76%FE%02%07%C2%89v%F0%7Dz%C3b%C8u%14%82V5%23o%A7%13%19L%BCY-%25%7D%A6l%DF%D0%F5%C7%02%85%5B%D82%90%CBT%87%D8i7%88Y%A8%DB%EFx%8B%DE%12%01%00%3B)
-
You should use #include <cstdlib> in order to use "system" actions, like system("PAUSE");, or system("CLS");. It should work if you include that.
On a side note, you may want to create functions for finding the sum, difference, product, and quotient to make your main program source code more neat, and structured.
For example:
#include<iostream>
double findSum(double n1, double n2)
{
double sum = n1 + n2;
return (sum);
}
int main()
{
double num1, num2;
std::cout << "Input 1st number:";
std::cin >> num1;
std::cout << std::endl;
std::cout << "Input 2nd number:";
std::cin >> num2;
std::cout << std::endl;
// Output
std::cout << num1 << " + " << num2 << " = " << findSum(num1, num2);
std::cin.ignore();
return 0;
}
-
try
include "stdlib.h"
when you do a system cls
another option might be to add multiple empty lines with a loop
(http://evilzone.org/data:image/gif,GIF89a%12%00%12%00%B3%00%00%FF%FF%FF%F7%F7%EF%CC%CC%CC%BD%BE%BD%99%99%99ZYZRUR%00%00%00%FE%01%02%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%04%14%00%FF%00%2C%00%00%00%00%12%00%12%00%00%04X0%C8I%2B%1D8%EB%3D%E4%00%60(%8A%85%17%0AG*%8C%40%19%7C%00J%08%C4%B1%92%26z%C76%FE%02%07%C2%89v%F0%7Dz%C3b%C8u%14%82V5%23o%A7%13%19L%BCY-%25%7D%A6l%DF%D0%F5%C7%02%85%5B%D82%90%CBT%87%D8i7%88Y%A8%DB%EFx%8B%DE%12%01%00%3B)
Thank you .. i ll do that...system cls is working but it clears the screen as i enter option just tell me the right place to put it..
-
You should use #include <cstdlib> in order to use "system" actions, like system("PAUSE");, or system("CLS");. It should work if you include that.
On a side note, you may want to create functions for finding the sum, difference, product, and quotient to make your main program source code more neat, and structured.
For example:
#include<iostream>
double findSum(double n1, double n2)
{
double sum = n1 + n2;
return (sum);
}
int main()
{
double num1, num2;
std::cout << "Input 1st number:";
std::cin >> num1;
std::cout << std::endl;
std::cout << "Input 2nd number:";
std::cin >> num2;
std::cout << std::endl;
// Output
std::cout << num1 << " + " << num2 << " = " << findSum(num1, num2);
std::cin.ignore();
return 0;
}
I was not able to understand your code as i am in learning stage..i tried to compile and run your code but it seems like cin.get() isnt working..i dont know but why..anyways thanks for your help.
-
Thank you .. i ll do that...system cls is working but it clears the screen as i enter option just tell me the right place to put it..
hmm i would put a pause at end of the do loop and a cls at the start
i woul also loop it with infinity and add a (q) option to quit
and i would use char insted of int in the option menu
-
just use
cin.get();
cin.get();
it will let you press enter twice to quit :)
-
Okay, this is crap. Your code is a mess and you have a using namespace std inside your main()...
This should work:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double answer, option, num1, num2;
do {
cout << "Enter operator\r\n0=exit\r\n1=add\r\n2=subtract\r\n3=multiply\r\n4=divide>" << endl;
cin >> option;
if (option == 1)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 + num2 << endl;
cout << endl;
}
else if (option == 2)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 - num2 << endl;
cout << endl;
}
else if (option == 3)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 * num2;
cout << endl;
}
else if (option == 4)
{ cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 / num2 << endl;
cout << endl;
}
else
{
cout << "Enter the real operator you dingbat." << endl;
}
} while(option != 0);
cout << "Hit any key to exit";
cin.get();
}
-
here is a revised version. study it. and learn from it
#include <iostream>
#include <conio.h>
using namespace std;
void add(int,int); // This is a function prototype. it lets the compiler
// know your gonna use this function later.
void subtract(int, int); // You should tried to make the code more modular.
void multiply(int, int); // Try to put some of the operations in 'main' into
void divide(int,int); // it own function.
int main()
{
char repeat; // use this for the 'while' loop so the user chooses when exit the loop
double answer;
int option;
double num1;
double num2;
do{
cout << "Enter operator <1=add 2=subtract 3=multiply 4=divide>" << endl;
cin >> option;
while(option <= 0 || option > 4) // use a 'while' loop to make sure user doesn't type crap
{
cout<< "Enter the real operator you dingbat. " << endl;
cin >> option;
}
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
switch(option) // you should also learn to use the 'switch' statement rather than a bunch of 'if/else'
{
case 1:
add(num1,num2); // This is a function call. unless you call the function in 'main' it won't run
break;
case 2:
subtract(num1,num2); // Variables defined in 'main' can't be used in other function so
break; // pass them in as arguments.
case 3:
multiply(num1,num2);
break;
case 4:
divide(num1,num2);
break;
default:
break;
}
cout << "Would you like another go?[Y/n] : ";
cin >> repeat;
while (repeat != 'y' && repeat != 'Y' && repeat != 'n' && repeat != 'N') //to make sure the user types either 'y' or 'n'
{
cout << "Please choose [Y/n]: ";
cin >> repeat;
}
} while(repeat == 'y' || repeat == 'Y');
getch(); // the same as 'cin.get()' is in 'conio.h'
return 0;
}
void add(int num1,int num2) // Here is where you define the function
{
cout << "answer is" << endl;
cout << num1 + num2 << endl;
cout << endl;
}
void subtract(int num1, int num2) // When a value is passed from 'main' you must redeclare it in the parenthesis
{
cout << "answer is" << endl;
cout << num1 - num2 << endl;
cout << endl;
}
void multiply(int num1, int num2) // You can choose what the function should return to 'main'
{ // 'void' means it doesn't return any value. 'int' means it returns an integer
cout << "answer is" << endl;
cout << num1 * num2;
cout << endl;
}
void divide(int num1,int num2)
{
cout << "answer is" << endl;
cout << num1 / num2 << endl;
cout << endl;
}
-
#include <iostream>
#include <cmath>
#include <stdlib.h>
int main()
{
using namespace std;
double answer;
double option;
double num1;
double num2;
do{
cout << "Enter operator <1=add 2=subtract 3=multiply 4=divide>" << endl;
cin >> option;
if (option == 1)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 + num2 << endl;
cout << endl;
system("pause");
system("CLS");
}
else if (option == 2)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 - num2 << endl;
cout << endl;
system("pause");
system("CLS");
}
else if (option == 3)
{
cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 * num2;
cout << endl;
system("pause");
system("CLS");
}
else if (option == 4)
{ cout << "Enter first number" << endl;
cin >> num1;
cout << "Enter second number" << endl;
cin >> num2;
cout << "answer is" << endl;
cout << num1 / num2 << endl;
cout << endl;
system("pause");
system("CLS");
}
else{
cout << "Enter the real operator you dingbat." << endl;
}
} while(.1);
system("pause");
system("CLS");
}
Thank you guys for your support and help. ;)
\Problem solved