If Else Statement Help

Prompt for and read a single upper-case element symbol into char variable sym. Print the corresponding element and atomic number. Print an error message if the symbol is invalid. Write both if statement and switch statement versions.

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
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
	char sym;
	char B, C, F, H, N, O;

	cout << "Please Enter a single upper-case element symbol: ";
	cin >> sym;
	
	if (sym = B)
	{
		cout << "Element: Boron, Atomic Number: 5";
	}
	if (sym = C)
	{
		cout << "Element: Carbon, Atomic Number: 6";
	}
	if (sym = F)
	{
		cout << "Element: Fluorine, Atomic Number: 9";
	}
	if (sym = H)
	{
		cout << "Element: Hydrogen, Atomic Number: 1";
	}
	if (sym = N)
	{
		cout << "Element: Nitrogen, Atomic Number: 7";
	}
	if (sym = O)
	{
		cout << "Element: Oxygen, Atomic Number: 8";
	}
	else (sym != B, C, F, H, N, O);
	{
		cout << "Error, the symbol entered is invalid.";
	}
}


It keeps saying that I'm using uninitialized variables, and I can't seem to think of how to fix this. Thanks!
Last edited on
B is a variable of type char that has no value. It is NOT the letter B.
This means you do not need the extra char variables. Be aware that 'b' is not equal to 'B'.

'B' is a capital B.

Second,
= is not comparison.
if(x = y) is a common mistake that sets the value of x to y and then evaluates whether x is true or not (0 is false, else true).

x == y is the comparison.

also, you canna do a lumped comparison like
x != a,b,c
you have to explicitly type in x!=a && x!=b && x!=c.
also else statements are absolute and do not have a condition at all. You can chain if statements to do this:
if(cond)
code;
else if(another condition)
othercode;


in short, you need to review the syntax of the language a bit.

Last edited on
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
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
	char sym;
	

	cout << "Please Enter a single upper-case element symbol: ";
	cin >> sym;
	
	if (sym == B)
	{
		cout << "Element: Boron, Atomic Number: 5";
	}
	if (sym == C)
	{
		cout << "Element: Carbon, Atomic Number: 6";
	}
	if (sym == F)
	{
		cout << "Element: Fluorine, Atomic Number: 9";
	}
	if (sym == H)
	{
		cout << "Element: Hydrogen, Atomic Number: 1";
	}
	if (sym == N)
	{
		cout << "Element: Nitrogen, Atomic Number: 7";
	}
	if (sym == O)
	{
		cout << "Element: Oxygen, Atomic Number: 8";
	}
	else if (sym != B && sym != C && sym != F && sym != H && sym != N && sym != O)
	{
		cout << "Error, the symbol entered is invalid.";
	}
}


Okay, now wut
Last edited on
Single-quote your character names, e.g. if (sym == 'B'), etc.
Close!

'B' is the letter B. 'C' is the letter C. Still need to fix this part.

chain the ifs with else and you don't need that big long comparison. Its good to see how to do it that way, but

if(sym == 'B')
cout...
else
if(sym == 'C')
cout
else...
//more of the same
final else (it did not equal any of the above, so simply else)
cout "Error ...



Thank you so much!:)

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
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout"

int main()
{
	char sym;


	cout << "Please Enter a single upper-case element symbol: ";
	cin >> sym;

	if (sym == 'B')
	{
		cout << "Element: Boron, Atomic Number: 5";
	}
	else if (sym == 'C')
	{
		cout << "Element: Carbon, Atomic Number: 6";
	}
	else if (sym == 'F')
	{
		cout << "Element: Fluorine, Atomic Number: 9";
	}
	else if (sym == 'H')
	{
		cout << "Element: Hydrogen, Atomic Number: 1";
	}
	else if (sym == 'N')
	{
		cout << "Element: Nitrogen, Atomic Number: 7";
	}
	else if (sym == 'O')
	{
		cout << "Element: Oxygen, Atomic Number: 8";
	}
	else
	{
		cout << "Error, the symbol entered is invalid.";
	}
	_getch();
}
Topic archived. No new replies allowed.