Jump to content

What have I done wrong?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
27 replies to this topic

#1
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Need little help...
I am new to c++ and would really kindly need help with this code...

#include <iostream>

using namespace std;


int main() {


	double plus1,minus1,multi1,division1;

	double plus2,minus2,multi2,division2;

	double countP;

	double countM;

	double countMM;

	double countD;


	int in;


	std::cout << " What do you want to do\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n";

	cin >> in;

	if(in = 1) {

		cout << " First value\n";

		cin >> plus1;

		cout << " Second value\n";

		cin >> plus2;


		countP = plus1 + plus2;


		cout << " The total value became " << countP << " happy now?\n";

	}

	else if(in = 2) {

		cout << " First value\n";

		cin >> minus1;

		cout << " Second value\n";

		cin >> minus2;


		countM = minus1 - minus2;

		cout << " The total value became " << countM << " happy now?\n";

	}

	else if(in = 3) {

		cout << " First value\n";

		cin >> multi1;

		cout << " Second value\n";

		cin >> multi2;


		countMM = multi1 * multi2;

		cout << " The total value became " << countMM << " happy now?\n";

	}

	else if(in = 4) {

		cout << " First value\n";

		cin >> division1;

		cout << " Second value\n";

		cin >> division2;


		countD = division1 / division2;

		cout << " The total value became " << countD << " happy now?\n";

	}

	return 0;

}
Whenever I do subtraction it ends up adding the two values I mean come on. value one - value two does not equal value one + value two...

Anyway to do it the "right" way?
Posted Image

#2
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
Should it not be if(in == 1) rather than if(in = 1) and same with all other if functions

also, have you tried using a strcmp? can come in handy too.

(Not knowing C++, I hope strcmp exists for that :P)

#3
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Phoenixz said:

Should it not be if(in == 1) rather than if(in = 1) and same with all other if functions

also, have you tried using a strcmp? can come in handy too.

(Not knowing C++, I hope strcmp exists for that :P)

Doesn't matter :S
strcmp what?
Posted Image

#4
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
Well, isn't it one equals to assign something, and two equals to mean "is equal too"?

also ignore the strcmp I said about, it's for strings not integers. ^^

#5
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
#include <cstdlib>

#include <iostream>


using namespace std;


int main(int argc, char *argv[])

{

    double plus1,minus1,multi1,division1;

	double plus2,minus2,multi2,division2;

	double countP;

	double countM;

	double countMM;

	double countD;


	int in;


	std::cout << " What do you want to do\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n";

	cin >> in;

	if(in == 1) {

		cout << " First value\n";

		cin >> plus1;

		cout << " Second value\n";

		cin >> plus2;


		countP = plus1 + plus2;


		cout << " The total value became " << countP << " happy now?\n";

}

	else if(in == 2) {

		cout << " First value\n";

		cin >> minus1;

		cout << " Second value\n";

		cin >> minus2;


		countM = minus1 - minus2;

		

        cout << " The total value became " << countM << " happy now?\n";

	}

	else if(in == 3) {

		cout << " First value\n";

		cin >> multi1;

		cout << " Second value\n";

		cin >> multi2;


		countMM = multi1 * multi2;

		cout << " The total value became " << countMM << " happy now?\n";

	}

	else if(in == 4) {

		cout << " First value\n";

		cin >> division1;

		cout << " Second value\n";

		cin >> division2;


		countD = division1 / division2;

		cout << " The total value became " << countD << " happy now?\n";

	}

    system("PAUSE");

    return EXIT_SUCCESS;

}


Enjoy...


R u happy now? :P:p

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#6
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
Upss..I was staying the code while u two posted here. I can see that the solution is already done but......I am happy I could solve it by myself :P:p

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#7
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

MathXpert said:

#include <cstdlib>

#include <iostream>


using namespace std;


int main(int argc, char *argv[])

{

    double plus1,minus1,multi1,division1;

	double plus2,minus2,multi2,division2;

	double countP;

	double countM;

	double countMM;

	double countD;


	int in;


	std::cout << " What do you want to do\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n";

	cin >> in;

	if(in == 1) {

		cout << " First value\n";

		cin >> plus1;

		cout << " Second value\n";

		cin >> plus2;


		countP = plus1 + plus2;


		cout << " The total value became " << countP << " happy now?\n";

}

	else if(in == 2) {

		cout << " First value\n";

		cin >> minus1;

		cout << " Second value\n";

		cin >> minus2;


		countM = minus1 - minus2;

		

        cout << " The total value became " << countM << " happy now?\n";

	}

	else if(in == 3) {

		cout << " First value\n";

		cin >> multi1;

		cout << " Second value\n";

		cin >> multi2;


		countMM = multi1 * multi2;

		cout << " The total value became " << countMM << " happy now?\n";

	}

	else if(in == 4) {

		cout << " First value\n";

		cin >> division1;

		cout << " Second value\n";

		cin >> division2;


		countD = division1 / division2;

		cout << " The total value became " << countD << " happy now?\n";

	}

    system("PAUSE");

    return EXIT_SUCCESS;

}


Enjoy...


R u happy now? :P:p

Did not work I can't do subtraction nor multiple nor division...
Posted Image

#8
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
I am using DEV (as u can see from the code) and it works perfectly here. I donno know what's wrong out there. I copied and pasted the code from here and it worked :D

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#9
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

MathXpert said:

I am using DEV (as u can see from the code) and it works perfectly here. I donno know what's wrong out there. I copied and pasted the code from here and it worked :D

I think something is wrong with my compiler :(
Posted Image

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your problem is here:
	if(in = 1) {
What that does is assign the value 1 to in and return 1 as the result of "in = 1". 1 converts to true, so it enters the logic for addition. You need to change it to:
	if(in == 1) {

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Phoenixz

Phoenixz

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 256 posts
That's so what I said, but in better words. :p

#12
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

WingedPanther said:

Your problem is here:

	if(in = 1) {

What that does is assign the value 1 to in and return 1 as the result of "in = 1". 1 converts to true, so it enters the logic for addition. You need to change it to:

	if(in == 1) {


Thank you this part made my code work a bit more :D
Posted Image