So, I am fairly new to C++ programming, so if you can help me, please try to keep it simple...I'm still learning. What I'm trying to do is assign a score to a grade letter (A,B,C,D,F) and then display it. This is what I'm have:

string lettergrade1;
	string lettergrade2;

	if (s1score >= 0.90)
	{
		string lettergrade1 = "A";
	}
	else if (s1score >= 0.80)
	{
		string lettergrade1 = "B";
	}
	else if (s1score >= 0.70)
	{
		string lettergrade1 = "C";
	}
	else if (s1score >= 0.60)
	{
		string lettergrade1 = "D";
	}
	else 
	{
		string lettergrade1 = "F";
	}

However, nothing is being assigned to "lettergrade1"...I'm sure it is some stupid mistake I am overlooking, but I can't seem to find the answer.

Recommended Answers

All 3 Replies

You are re declaring lettergrade1. You don't need to say string inside your if else blocks. Also , since you only want a single character i.e. A,B,C,D, F ..why not use a char instead

To clarify what DJSAN10 is saying, assuming it needs clarification, in each IF block you are creating a new lettergrade1 which gets destroyed as soon as that block ends. The lettergrade1 value at the top is never used.

Kudos for using the 'proper' IF-ELSE construct and not using else if (s1score >= 0.70 && s1score < 0.80)

Bada-bing. Worked like a charm. Thanks a ton!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.