Rate my Calculator 1.0 code /// Rate my other projects as well! - J. McCarthy - Mar 26, 2015
Give me any corrections I could make, errors in the code, etc.
Thanks
CALCULATOR:
This is the 1.0 I did today I'd like to make it more advanced like keep adding numbers and getting the answer every time like a normal calculator or something? not really sure yet. Also as always any new suggestions for projects or additions for this. ""% is the root of said number""
Code #include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int add(int,int);
int subtract(int,int);
int mult (int,int);
float divi (float,float);
int power (int, int);
float root(float, float);
int add(int num1,int num2)
{
int answer = num1 + num2;
return answer;
}
int subtract(int num1,int num2) {
int answer = num1 - num2;
return answer;
}
int mult (int num1,int num2) {
int answer = num1 * num2;
return answer;
}
float divi (float num1,float num2) {
float answer = num1 / num2;
return answer;
}
int power (int num1, int num2) {
int answer = pow(num1,num2);
return answer;
}
float root (float num1, float num2){
float answer = pow(num1,1.0/num2);
return answer;
}
int main()
{{
int num1, num2;
float answer;
char oper;
char quest;
start:
cout << endl << "Calculator 1.0" << endl;
cout << endl << "Please enter two numbers followed by the operator" << endl;
cout << endl << "Enter Number 1: ";
cin >> num1;
cout << endl << "Enter Number 2: ";
cin >> num2;
cout << endl << "Enter Operator ( +, -, *, /, ^, %) : ";
cin >> oper;
if (oper == '+'){
answer = add(num1,num2);}
else if (oper == '-'){
answer = subtract(num1,num2);}
else if (oper == '*'){
answer = mult(num1,num2);}
else if (oper == '/'){
answer = divi(num1,num2);}
else if (oper == '^'){
answer = power(num1,num2);}
else if (oper == '%'){
answer = root(num1,num2);}
else{
cout << "Invalid Operator, try again.";
goto start; }
cout << endl << "The answer is: " << answer << endl;
Question:
cout << "Would you like to try again [Y / N]: ";
cin >> quest;
if (quest == 'Y' || quest == 'y') {
system("Pause");
system("CLS");
goto start; }
else if (quest == 'N' || quest == 'n'){
cout << endl << "Thanks for using Calculator 1.0" << endl << endl << endl << endl << endl << endl;
}
else {
cout << endl << "Invalid Answer, Try Again";
goto Question; }
}
return 0;
}
I'd like to use the CLS on the try again page just forgot while I was coding it
RANDOM NUMB GEN:
this is my first version
I couldn't get it to work without a goto statement, what else could.
Code #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
Start:
int Number, Guess, Tries, i;
char Question;
Tries = 0;
i = 0;
srand(time(0));
Number = 1+ (rand()%100);
while (10 >= Tries) {
cout << endl << "Please guess a number between 1 and 100: ";
cin >> Guess;
if (Guess == Number) {
cout << endl << "Congrats! The number was " << Number << "." << endl;
Tries = 11;
}
else if (Guess > Number) {
cout << endl << "Your guess was to large!" << endl;
Tries++;
}
else if (Guess < Number) {
cout << endl << "Your guess was to small!" << endl;
Tries++;
}
else {
cout << endl << "You entered an invalid number. Try again with 1-100." << endl;
Tries++;
}
}
cout << endl << endl << "Would you like to Play Again [Y]es or [N]o?" << endl;
cin >> Question;
if (Question == 'Y'){
cout << "Okay lets play again!" << endl << endl;
goto Start;
}
else if (Question == 'N') {
cout << "Okay, thanks for playing." << endl << endl;
}
return 0;
}
* - 1btQzXv - Mar 26, 2015
If div and root are using floats, why not make all operations accept float so you can add 1.5 + 0.5
also you should do
Code cout << endl << "Enter Operation: ";
cin >> num1 >> oper >> num2;
so you can accept the data in one line e.g
"Enter Operation: 2 + 7"
* - J. McCarthy - Mar 26, 2015
Didn't think about that but I'll do that and your other suggestion as well
* - 0xDEAD10CC - Mar 26, 2015
You could even make them templated functions. :)
* - J. McCarthy - Mar 26, 2015
(Mar 26, 2015 06:38 PM)0xDEAD10CC Wrote: You could even make them templated functions. :) I've not seen anything on those before and I'm not really understanding it on my google search lol. Care to explain or a good place to learn it?
* - 0xDEAD10CC - Mar 26, 2015
Code template <typename T>
T add(T x, T y) { return x + y; }
* - Bibl - Mar 26, 2015
(Mar 26, 2015 07:12 PM)0xDEAD10CC Wrote: Code template <typename T>
T add(T x, T y) { return x + y; }
mov eax, dword ptr [esp+4]
add eax, dword ptr[esp+8]
* - 0xDEAD10CC - Mar 26, 2015
^^ Ignore the skid that knows how to view the disassembly.
(Mar 26, 2015 07:12 PM)0xDEAD10CC Wrote: Code template <typename T>
T add(T x, T y) { return x + y; }
Note:
T can be any type that has the + operator overloaded (as well as the copy constructor being defined--with the exception of an explicit reference type).
* - J. McCarthy - Mar 26, 2015
Okay I'll check that out tomorrow
Anyways I did a small contact book v1 I want to add some more stuff including Delete tomorrow but I had a question why can I only enter single words not a space then last name?
for example I can enter "Alex" but I can't enter "Spoder Man"?
code:
Code #include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
cout << "Welcome to Contact Book v 1.0" << endl << endl << endl;
string Contactbook[15];
int numbcontact = 0;
char Question;
bool running = true;
Start:
while (running == true) {
cout << endl << "Choose a selection [N]ew Contact, [V]iew Contacts, [E]xit Program" << endl;
cin >> Question;
if (Question == 'N' || Question == 'n') {
cout << "Enter the name of your contact: ";
cin >> Contactbook[numbcontact];
numbcontact++;
system("PAUSE");
system("CLS");
}
else if (Question == 'V' || Question == 'v') {
cout << "You have " << numbcontact << " contacts in your book." << endl << endl;
for (int i = 0; i < numbcontact; i++) {
cout << i << ". " << Contactbook[i] << endl;
cin.ignore(); }
}
else if (Question == 'E' || Question == 'e') {
cout << "Adios Amigo!" << endl;
running = false;
}
else {
cout << "invalid selection try again." << endl << endl << endl;
goto Start;
} }
return 0;
}
* - 0xDEAD10CC - Mar 26, 2015
If you used std::getline() you would. The >> operator with std::cin uses whitespace as a delimiter by default.
* - J. McCarthy - Mar 27, 2015
(Mar 26, 2015 08:32 PM)0xDEAD10CC Wrote: If you used std::getline() you would. The >> operator with std::cin uses whitespace as a delimiter by default.
I tried get line but it made an infinite loop of the original question whenever they hit a button for example N it said type name but instead of giving the option to type it immediately said the question of NVE again
I think I used getline (cin, contactbook[numbcontact]);
On my phone so capitalization might not be right in that
updated with random numb gen
I couldn't get play again to work without goto what should I do?
I tried a while statement right above my previous one should I do that somewhere else? it just kept asking the play again over again
* - 0xDEAD10CC - Mar 27, 2015
The reason is probably because you're mixing cin >> and getline(), and you don't cleanup the input buffer properly. Your condition checks for each menu item should really be a switch statement, and you should probably have functions which do each specific thing instead of having everything within main().
* - Deadbyte - Mar 27, 2015
I remember you not bad for a first calculator just try to optimize your code.
* - salmoneus - Mar 28, 2015
(Mar 26, 2015 07:14 PM)NTTITONV37 Wrote: mov eax, dword ptr [esp+4]
add eax, dword ptr[esp+8]
lol holy shit even ur troll code is wrong
[esp +4] is the return address not the first param
|