954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Strings inside if statements.

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.

SmackTubby
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

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)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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

SmackTubby
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: