-
Calc challenge
I have almost the exact code from the challenge.
Code:
#include <iostream>
int multiply(int x,int y)
{
return x*y;
}
int divide(int x, int y)
{
return x/y;
}
int add(int x, int y)
{
return x+y;
}
int subtract(int x, int y)
{
return x-y;
}
using namespace std;
int main()
{
char op='c';
int x,y;
while (op!=e)
{
cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
cin>> op;
switch(op)
{
case '+':
cin>>x;
cin>>y;
cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
break;
case'-':
cin>>x;
cin>>y;
cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
break;
case'/':
cin>>x;
cin>>y;
cout<<x<<"/"<<y<<divide(x,y)<<endl;
break;
case'*':
cin>>x;
cin>>y;
cout<<x<<"*"<<y<<multiply(x,y)<<endl;
break;
case'e':
return 0;
default:
cout<<"sorry, try again."<<endl;
}
}
return 0;
}
It says e is undeclared even though I filled in all the blanks on the challenge thing.
-
Hello,
Didn't you mean:- Stack Overflow
-
-
I added another cout, because I didn't know if it was working after I entered + so now it prompts you to enter the number.
My finished product:
Code:
#include <iostream>
int multiply(int x,int y)
{
return x*y;
}
int divide(int x, int y)
{
return x/y;
}
int add(int x, int y)
{
return x+y;
}
int subtract(int x, int y)
{
return x-y;
}
using namespace std;
int main()
{
char op='c';
int x,y;
while (op!='e')
{
cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
cin>> op;
switch(op)
{
case '+':
cout<<"Enter the numbers you would like to add."<<endl;
cin>>x;
cin>>y;
cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
break;
case'-':
cout<<"Enter the numbers you would like to subtract."<<endl;
cin>>x;
cin>>y;
cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
break;
case'/':
cout<<"Enter the numbers you would like to divide."<<endl;
cin>>x;
cin>>y;
cout<<x<<"/"<<y<<divide(x,y)<<endl;
break;
case'*':
cout<<"Enter the numbers you would like to multiply."<<endl;
cin>>x;
cin>>y;
cout<<x<<"*"<<y<<multiply(x,y)<<endl;
break;
case'e':
return 0;
default:
cout<<"sorry, try again."<<endl;
}
}
return 0;
}
Another victory for me! :D :p
-
Just finished the array challenge to.
Code:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int array[8];
for (int x=0; x<8; x++)
cin>>array[x];
for (int x=0; x<8; x++)
cout<<array[x];
cin.get();
cin.ignore();
}
Now what should I do?
-
Code:
#include <iostream>
int multiply(int x,int y)
{
return x*y;
}
int divide(int x, int y)
{
return x/y;
}
int add(int x, int y)
{
return x+y;
}
int subtract(int x, int y)
{
return x-y;
}
using namespace std;
int main()
{
char op='c';
int x,y;
while (op!='e')
{
cout<<"What operation would you like to perform add(+), subtract(-),divide(/),multiply(*), (e)exit?";
cin>> op;
switch(op)
{
case '+':
cout<<"Enter the numbers you would like to add."<<endl;
cin>>x;
cin>>y;
cout<<x<<"+"<<y<<"="<<add(x,y)<<endl;
break;
case'-':
cout<<"Enter the numbers you would like to subtract."<<endl;
cin>>x;
cin>>y;
cout<<x<<"-"<<y<<"="<<subtract(x,y)<<endl;
break;
case'/':
cout<<"Enter the numbers you would like to divide."<<endl;
cin>>x;
cin>>y;
cout<<x<<"/"<<y<<"="<<divide(x,y)<<endl;
break;
case'*':
cout<<"Enter the numbers you would like to multiply."<<endl;
cin>>x;
cin>>y;
cout<<x<<"*"<<y<<"="<<multiply(x,y)<<endl;
break;
case'e':
return 0;
default:
cout<<"sorry, try again."<<endl;
}
}
return 0;
}
Now when you multiply or divide it doesn't look like the second number is part of the answer. Otherwise excellent job.