Mar 26, 2015 04:17 PM
(This post was last modified: Mar 27, 2015 03:04 PM by J. McCarthy.)
Give me any corrections I could make, errors in the code, etc.
Thanks
CALCULATOR:
Spoiler (Click to View)
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""
I'd like to use the CLS on the try again page just forgot while I was coding it
RANDOM NUMB GEN:
Spoiler (Click to View)
this is my first version
I couldn't get it to work without a goto statement, what else could.
Thanks
CALCULATOR:
Spoiler (Click to View)
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:
Spoiler (Click to View)
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;
}


![[Image: dead.gif]](https://image.ibb.co/gSF95a/dead.gif)