Switch to Function
Mar 10, 2017 at 12:01am UTC
Hey programmers, for this code what I'm trying to do is rewrite this program in terms of functions. I've messed around with it a little bit in the addition section but I don't know enough to make it work properly and I've looked around everywhere for an explanation, but nothing seems to help.
From there, I'm supposed to get the numbers from a file instead of using cin statements. Any clarification on how to do any of this is greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <math.h>
using namespace std;
int main()
{
char operation;
int num1 = 0, num2 = 0, remainder = 0;
double result;
cout << "What kinda basic algebra are you too lazy to do by hand? " << endl;
cout << "Addition: +\nSubtraction: -\nMultiplication: *\nDivision: /\nNumber to power n: ^\nFactorial: !\nQuit: q" << endl << endl;
cout << "Pick one, yo: " ;
cin >> operation;
cout << endl;
while (operation != 'q' and operation != 'Q' )
{
switch (operation)
{
case '+' :
{
std::cout << "Pick the first number: " ;
std::cin >> num1;
std::cout << "Pick the second number: " ;
std::cin >> num2;
int addition(num1, num2)
return result;
std::cout << num1 << " + " << num2 << " = " << result << endl << endl;
break ;
}
case '-' :
{
std::cout << "Pick the first number: " ;
std::cin >> num1;
std::cout << "Pick the second number: " ;
std::cin >> num2;
result = num1 - num2;
std::cout << num1 << " - " << num2 << " = " << result << endl << endl;
break ;
}
case '*' :
{
std::cout << "Pick the first number: " ;
std::cin >> num1;
std::cout << "Pick the second number: " ;
std::cin >> num2;
result = num1 * num2;
std::cout << num1 << " * " << num2 << " = " << result << endl << endl;
break ;
}
case '/' :
{
std::cout << "Pick the dividend: " ;
std::cin >> num1;
std::cout << "Pick the divisor: " ;
std::cin >> num2;
result = num1 / num2;
remainder = num1 % num2;
std::cout << num1 << " / " << num2 << " = " << result << endl << endl;
std::cout << num1 << " % " << num2 << " = " << remainder << endl << endl;
break ;
}
case '^' :
{
std::cout << "Pick the base number: " ;
std::cin >> num1;
std::cout << "Pick the exponent: " ;
std::cin >> num2;
result = pow(num1, num2);
std::cout << num1 << " ^ " << num2 << " = " << result << endl << endl;
break ;
}
case '!' :
{
std::cout << "Pick your number: " ;
std::cin >> num1;
result = 1;
for (int cnt = 2; cnt <= num1; cnt++)
{
result *= cnt;
}
std::cout << num1 << "! = " << result << endl << endl;
break ;
}
default :
{
std::cout << "I know you're bad at math, but reading? Really? Check the options again, fam, and pick a viable one: " << endl << endl;
std::cout << "Addition: +\nSubtraction: -\nMultiplication: *\nDivision: /\nNumber to power n: ^\nFactorial: !\nQuit: q" << endl << endl;
std::cout << "So, which one is it: " ;
std::cin >> operation;
}
}
cout << "\nAddition: +\nSubtraction: -\nMultiplication: *\nDivision: /\nNumber to power n: ^\nFactorial: !\nQuit: q" << endl << endl;
cout << "Anything else?: " ;
cin >> operation;
cout << endl;
}
cout << "Later, scrub." ;
return 0;
}
Mar 10, 2017 at 1:59am UTC
You must put a text file of all the data you need into your project folder and then write the code to open it. Once thats completed you can use getline();
If youre not sure how to do this let me know and ill post some of my old code showing how.
Topic archived. No new replies allowed.