PDA

View Full Version : first cpp, calculator



Awkwardsaw
03-07-2010, 10:20 PM
#include <iostream>

using namespace std;

struct num {
int num1;
int num2;
} number;

void getnums(void)
{
cout << "First Number: ";
cin >> number.num1;
cout <<"Second Number: ";
cin >> number.num2;
}

int addition (void)
{
return (number.num1 + number.num2);
}

int subtraction (void)
{
return (number.num1 - number.num2);
}

int multi (void)
{
return (number.num1 * number.num2);
}

int divide (void)
{
return (number.num1 / number.num2);
}

int square (void)
{
return (number.num1 * number.num1);
}



int main()
{
string x;
loop:
cout << "instructions - type: " << endl;
cout << "+ to add" << "- to subtract" << endl << "* to multiply" << endl;
cout << "/ to divide" << endl << "^2 to square" << endl;
cin >> x;
getnums();
switch (x) {
case '+':
cout << addition();
break;
case '-':
cout << subtraction();
break;
case '*':
cout << multi();
break;
case '/':
cout << divide();
break;
case '^2':
cout << square();
break;
default:
cout << "wrong input!";
goto loop;
}
system("PAUSE");
return EXIT_SUCCESS;
}


if something is not right, tell me how to fix it pl0x :) i did a lot of the things as best as i can think of, and to learn/ try different stuff

i cannot compile this my self for some reason, i keep getting a "all-before does not exist - don't know how to make it" error

also, i would like more ideas on what to make, i am stumpt :(

x[Warrior]x3500
03-08-2010, 04:42 AM
#include <iostream>

using namespace std;

struct num {
int num1;
int num2;
} number;

void getnums(void)
{
cout << "First Number: ";
cin >> number.num1;
cout <<"Second Number: ";
cin >> number.num2;
}

int addition ()
{
return (number.num1 + number.num2);
}

int subtraction (void)
{
return (number.num1 - number.num2);
}

int multi (void)
{
return (number.num1 * number.num2);
}

int divide (void)
{
return (number.num1 / number.num2);
}

int square (void)
{
return (number.num1 * number.num1);
}



int main()
{
char x;
loop:
cout << "instructions - type: " << endl;
cout << "'+' to add" << endl << "'-' to subtract" << endl << "'*' to multiply" << endl;
cout << "'/' to divide" << endl << "'2' to square" << endl;
cin >> x;
getnums();
switch (x) {
case '+':
cout << addition()<<endl;
break;
case '-':
cout << subtraction()<<endl;
break;
case '*':
cout << multi()<<endl;
break;
case '/':
cout << divide()<<endl;
break;
case '2':
cout << square()<<endl;
break;
default:
cout << "wrong input!";
goto loop;
}
system("PAUSE");
return EXIT_SUCCESS;

}


i changed some stuff around in your main(). it should compile now. you used strings w/o #include <string>. also, idk if one can use strings in a switch() function. (i changed it to a char)

x[Warrior]x3500
03-08-2010, 05:12 AM
hey sry for the dub post:


#include <iostream>


using namespace std;

struct num {
float num1;
float num2;
} number;

void getnums() {
cout << "First Number: ";
cin >> number.num1;
cout <<"Second Number: ";
cin >> number.num2;
}

class answer {
public:
float addition () {return(number.num1 + number.num2);}
float subtraction () {return (number.num1 - number.num2);}
float multi () {return (number.num1 * number.num2);}
float divide () {return (number.num1 / number.num2);}
float square () {return (number.num1 * number.num1);}
};

int main() {
char x;
loop:
cout << "instructions - type: " << endl;
cout << "'+' to add" << endl << "'-' to subtract" << endl << "'*' to multiply" << endl;
cout << "'/' to divide" << endl << "'2' to square" << endl;
cin >> x;
getnums();
answer ans;
switch (x) {
case '+':
cout << ans.addition()<<endl;
break;
case '-':
cout << ans.subtraction()<<endl;
break;
case '*':
cout << ans.multi()<<endl;
break;
case '/':
cout << ans.divide()<<endl;
break;
case '2':
cout << ans.square()<<endl;
break;
default:
cout << "wrong input!";
goto loop;
}
system("PAUSE");
return EXIT_SUCCESS;

}



i just made it look nicer. it uses a class now instead of all those annoying functions. just looks cleaner and is easier to read.

EDIT: changed INTs to FLOATs


EDIT2: when you are programming in C++, you should try and make your main(); as simple and small as possible. i re-adjusted this code so that main(); would be significantly smaller:

#include <iostream>
using namespace std;

int a;

class answer {
struct num {
float num1;
float num2;
} number;
char x;
public:
float addition () {return(number.num1 + number.num2);}
float subtraction () {return (number.num1 - number.num2);}
float multi () {return (number.num1 * number.num2);}
float divide () {return (number.num1 / number.num2);}
float square () {return (number.num1 * number.num1);}
void getinput();
void getnums();
void switcharoo();
};

void answer::getinput() {
cout << "instructions - type: " << endl;
cout << "'+' to add" << endl << "'-' to subtract" << endl << "'*' to multiply" << endl;
cout << "'/' to divide" << endl << "'2' to square" << endl;
cin >> x;

}

void answer::getnums() {
cout << "First Number: ";
cin >> number.num1;
cout <<"Second Number: ";
cin >> number.num2;
}

void answer::switcharoo() {
switch (x) {
case '+':
cout << addition()<<endl;
break;
case '-':
cout << subtraction()<<endl;
break;
case '*':
cout << multi()<<endl;
break;
case '/':
cout << divide()<<endl;
break;
case '2':
cout << square()<<endl;
break;
default:
cout << "wrong input!";
a=0;
}
}

int main() {
answer ans;
while (a==0) {
a=1;
ans.getinput();
ans.getnums();
ans.switcharoo();
}
system("PAUSE");
return 0;
}


if you dont understand anything, please ask.

Awkwardsaw
03-08-2010, 01:23 PM
oo nice. thanks :D

although i still get the error, i think it just has to do with dev-c++(my compiler)

x[Warrior]x3500
03-09-2010, 12:28 AM
umm idk. i use dev-c++. what error do you get? have u tried creating a new empty project and running it in there?

EDIT: what version do you have?
http://aditsu.freeunixhost.com/dev-cpp-faq.html#allbefore

Awkwardsaw
03-09-2010, 12:46 AM
i have 4.9.9.2, and when i start up dev-c++ i get a box, picuture attatched

e: lol, i never read the box. /me fails

i guess i should try and fix the path thing

x[Warrior]x3500
03-09-2010, 12:50 AM
http://aditsu.freeunixhost.com/dev-cpp-faq.html#runfile

use the two links i provided. should help fix . (the other link is in my previous post)

Awkwardsaw
03-09-2010, 01:21 AM
fank you :)

id rep, but it says i cant lol

x[Warrior]x3500
03-09-2010, 01:29 AM
lol np at all. feel free to msg me if you ever have questions.

Awkwardsaw
03-09-2010, 01:31 AM
x3500;692377']lol np at all. feel free to msg me if you ever have questions.

well, i would like more ideas on what else i should make :)

i dont feel like making a game just yet.. something more mathematical or mechanicalised, if you know what i mean :)

x[Warrior]x3500
03-09-2010, 01:56 AM
hmm no games :( ahh lol. maybe you should make a GUI type of a calculator. no more cmd prompt stuff?

or maybe enhance your calculator: make them enter a line like "5+3" and have the calculator automatically do it. make it a 1 step process.

if you like physics, then you could make a super complicated physics solver. (like from kinematics to thermodynamics)

if you enjoy business, maybe make an accounting program.

oh, i have seen this one done before: make an encryption/decryption password program

Awkwardsaw
03-09-2010, 02:29 AM
is there an explode type of thing? i can do the one line calculator with that. i could also do some formulas i guess, or an f(x) thingy

x[Warrior]x3500
03-09-2010, 02:57 AM
explode? ugh... idk what u mean by that. but u can use string-streams. and .get/.getline .

Awkwardsaw
03-09-2010, 03:00 AM
x3500;692406']explode? ugh... idk what u mean by that. but u can use string-streams. and .get/.getline .

explode as in the same thing in scar/ php.

explode(';', 'hello;hi');
into
['hello, 'hi'];

in scar :p

x[Warrior]x3500
03-09-2010, 04:11 AM
explode as in the same thing in scar/ php.

explode(';', 'hello;hi');
into
['hello, 'hi'];

in scar :p

oh... duh. lol well yah you dont have a super easy command in c++ for that (at least i dont think so). but you can accomplish the same task with string streams.

Awkwardsaw
03-09-2010, 01:09 PM
alrighty, thanks :)