need help with coding

we're supposed to write a centimeter to inch/ inch to centimeter converter
and my if statements are not working im still a newbie, can someone please tell me what i did wrong?



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
#include <iostream>
using namespace std; 

void main()
{
	int nb;
	double value;
	double result;


	cout << ("1 for inch to centimeter conversion") << endl;
	cout << ("2 for centimeter to inch conversion") << endl;
	cout << ("please make a selection: ");
	cin >> nb;

	if ( nb=1 ){
		cout <<("please enter the value in centimeters: ");
		cin >> value;
		result= value*0.39370;
		cout << value << (" centimeters = ") << result << endl;
	}
	else if ( nb=2 ) {
		cout <<("please enter the value in inches: ");
		cin >> value;
		result= value/0.39370;
		cout << value << (" centimeters = ") << result << endl;
	}
	else 
		cout <<(" wrong input ") << endl;



		
	system ("pause");

}
	


1
2
3
if ( nb == 1 )
...
else if ( nb == 2 )
thank you!
Well, someone already explained the problem. To explain that further, from what I understand, most programming languages treat a single equals sign as if you're setting something equal to what follows the equals sign. However, a double equals sign signifies a test for that condition to be equal.
thank you guys,
does the code look good?
you are printing "centimeters =" in both cases.
it's best to initialise variables e.g.
1
2
3
        nt nb = 0;
	double value = 0.0;
	double result = 0.0;

and there's duplicate code that could be refactored.
dont use using namespace std;
and i wouldnt use system("pause").

looks ok though :)

edit: wait a minute.. i dont think your conversion is right.
I chose 1 (inch to centimetre)
I chose 1 again (i.e. give me 1 inch in centimetres)
answer = 0.3937

Which is wrong as there's about 2 and half cm in an inch isn't there?

yea it's the other way around.
change:
1
2
	cout << ("1 for inch to centimeter conversion") << endl;
	cout << ("2 for centimeter to inch conversion") << endl;

to
1
2
        cout << ("1 for centimeter to inch conversion") << endl;
	cout << ("2 for inch to centimeter conversion") << endl;	
Last edited on
because you are using assignment operator instead of comparison operator.
Thank you very much mutexe. code is fixed. you guys are awesome.
Cool.
On the refactoring front, i just meant reduce some duplicate code. Something like this maybe:
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>

int main()
{
	int choice = 0;
	double value = 0.0;
	double result = 0.0;
	const double conversion = 0.39370;
	std::string units = " centimeters = ";

	std::cout << ("1 for centimeter to inch conversion") << std::endl;
	std::cout << ("2 for inch to centimeter conversion") << std::endl;
	
	std::cout << ("please make a selection: ");
	std::cin >> choice;

	std::cout << ("please enter the value to convert: ");
	std::cin >> value;

	if (choice == 1)
	{			
		result = value*conversion;				
	}
	else if (choice == 2)
	{
		result = value / conversion;	
		units = " inches = ";
	}
	else
	{
		std::cout << (" wrong input ") << std::endl;
	}

	std::cout << value << units << result << std::endl;

	return 0;
}
thank you this looks much better.
may i ask why use std::cout when we can declare using namespace std;?
Topic archived. No new replies allowed.