Dont know what is wrong with program?
I have a calculator program
it ask you a for a number
then ask for an operator +-*/.. this is where it goes wrong
then once you type the operator in it just goes to next line and its blank
I wont let me enter the 2nd number
I am really stuck
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
|
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
#include "Functions.h"
void main ()
{
double num1;
double num2;
char Op;
char ValidOperators [] = "+-*/cCxX";
do
{
cout << "Please Enter A Number: ";
cin >> num1;
do
{
cout << "Please Enter An Operator: ";
cin >> Op;
} while (!IsOperatorValid(ValidOperators, Op));
cout << "Please Enter Another Number: ";
cin >> num2;
switch (Op)
{
case '+':
cout << "The Sum is: " << Addition <<endl;
break;
case '-':
cout << "The Sum is: " << Subtraction <<endl;
break;
case '*':
cout << "The Sum is: " << Multiply <<endl;
break;
case '/':
cout << "The Sum is: " << Divide <<endl;
break;
case 'Q':
case 'q':
cout << "You Have Quit the Program: " <<endl;
exit (0);
break;
case 'C':
case 'c':
break;
default: cout << "Invalid Operator: " << endl;
}
cout << "Clear your calculator? (y or n) ";
cin >> Op;
}
while (Op == 'y' );
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
bool IsOperatorValid (const char [], char &);
double Addition(double , double);
double Subtraction(double , double);
double Multiply (double , double );
double Divide(double , double );
#endif
|
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
|
#include <iostream>
#include <string>
using namespace std;
#include "Functions.h"
bool IsOperatorValid ( const char ValidOperators [], char & Operator)
{
cin >> Operator;
for(int i = 0; i = strlen (ValidOperators);i++)
if(Operator == ValidOperators [i])
return true;
else;
return false;
}
double Addition(double num1, double num2)
{
double add;
add = num1 + num2;
return add;
}
double Subtraction(double num1, double num2)
{
double sub;
sub = num1 - num2;
return sub;
}
double Multiply (double num1, double num2)
{
double mult;
mult = num1 * num2;
return mult;
}
double Divide(double num1, double num2)
{
double divide;
divide = num1 / num2;
return divide;
}
|
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
|
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Functions.h"
using namespace std;
int main (){
double num1;
double num2;
char Op;
char ValidOperators [] = "+-*/cCxX";
do{
cout << "Please Enter A Number: ";
cin >> num1;
do{
cout << "Please Enter An Operator: ";
cin >> Op;
} while (!IsOperatorValid(ValidOperators, Op));
cout << "Please Enter Another Number: ";
cin >> num2;
switch (Op) {
case '+':
cout << "The Sum is: " << Addition(num1,num2) <<endl;
break;
case '-':
cout << "The Sum is: " << Subtraction(num1,num2) <<endl;
break;
case '*':
cout << "The Sum is: " << Multiply(num1,num2) <<endl;
break;
case '/':
cout << "The Sum is: " << Divide(num1,num2) <<endl;
break;
case 'Q': case 'q':
cout << "You Have Quit the Program: " <<endl;
exit (0);
break;
case 'C': case 'c':
break;
default: cout << "Invalid Operator: " << endl;
}
cout << "Clear your calculator? (y or n) ";
cin >> Op;
}while (Op == 'y' );
return 0;
}
|
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
|
#include <iostream>
#include <string>
#include <cstring>
#include "Functions.h"
using namespace std;
bool IsOperatorValid ( const char ValidOperators [], char & Operator){
for(int i = 0; i < strlen(ValidOperators);i++){
if(Operator == ValidOperators [i])return true;
}
return false;
}
//------------------------------------------------------------------------------
double Addition(double num1, double num2){
cerr<<"here";
double add;
add = num1 + num2;
return add;
}
//------------------------------------------------------------------------------
double Subtraction(double num1, double num2){
double sub;
sub = num1 - num2;
return sub;
}
//------------------------------------------------------------------------------
double Multiply (double num1, double num2){
double mult;
mult = num1 * num2;
return mult;
}
//------------------------------------------------------------------------------
double Divide(double num1, double num2){
double divide;
divide = num1 / num2;
return divide;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
bool IsOperatorValid (const char [], char &);
double Addition(double , double);
double Subtraction(double , double);
double Multiply (double , double );
double Divide(double , double );
#endif
|
There are still multiple problems with this code.
Last edited on
: ( this is making me go crazy but thanks for hellping now i have an error about the
i < strlen..
o well thanks for your help
Post the error message
Did you include <cstring>?
Last edited on
yes i put <cstring> in the function file
warning C4018: '<' : signed/unsigned mismatch
Linking...
1 2 3 4 5 6
|
bool IsOperatorValid ( const char ValidOperators [], char & Operator){
for(unsigned i = 0; i < strlen(ValidOperators);i++){
if(Operator == ValidOperators [i])return true;
}
return false;
}
|
it run normally in my computer may be there is an exessive memory using in your's
while (!IsOperatorValid(ValidOperators, Op));
Remove that semicolon for starters, it creates an infinite loop if the operator is valid
Warnis, it's a do-while loop.
Topic archived. No new replies allowed.