Reputation: 85
I'm making a Grade log application and I have this piece of code here which doesn't work as I intend it to work. (It compiles great with no errors but doesn't work) (please keep in my mind I am a begginer in C++, thank you for your time and knowledge to help me learn also). (The testscore is being given from a series of questions where the value gets a +1 if its correct and a -1 if its wrong.
I think the error is in the => it gets conflicted one with the other but i dont know how to give a value for ex. if the score is < 20 cout = failed if its <40 failedgood but see 20 is < 40 so one overrides the other how could i put if its from 20-40 cout = failed good and if its from 0 - 20 cout = failed. I hope you understand what I mean.
int testscore;
string studentmark;
if ( testscore == 10 )
{
studentmark == ( "failed" );
}
else if ( testscore >= 11 && testscore <= 20 )
{
studentmark == ( "closebutfailed" );
}
else if ( testscore >= 21 && testscore <= 30)
{
studentmark == ( "passed" );
}
else if ( testscore >= 31 && testscore <= 40 )
{
studentmark == ("excelent");
}
else if ( testscore >=49 )
{
studentmark == ("hasteachersbook");
}
cout << "Studentmark is:" << studentmark << endl;
Upvotes: 2
Views: 197
Reputation: 24897
Software development is 1% inspiration and 99% debugging. If you cannot debug, you cannot write software.
One often-used debugging technique - KISS:
Comment out or remove as much gunge as possible in an attempt to make a program actually do something. When you get down to:
int testscore;
string studentmark;
{
studentmark == ( "failed" );
}
cout << "Studentmark is:" << studentmark << endl;
..and still you're getting no studentmark output, it will surely dawn that your attempted assignment is not working and therefore almost certainly wrong, (as already explained by other posters, '==' != '=').
Practice/develop your debugging skills before posting here - you will be better off in the long run than just passing your trivial-but-non-working code to a load of experienced developers on a group and getting them to fix your bugs.
Upvotes: 0
Reputation: 42450
You need to assign value to studentmark
like this:
studentmark = "hasteachersbook";
==
checks if the value is equal. =
assigns a value.
This should work. If not, the problem is elsewhere.
int testscore;
string studentmark;
if ( testscore == 10 )
{
studentmark = "failed";
}
else if ( testscore >= 11 && testscore <= 20 )
{
studentmark = "closebutfailed";
}
else if ( testscore >= 21 && testscore <= 30)
{
studentmark = "passed";
}
else if ( testscore >= 31 && testscore <= 40 )
{
studentmark = "excelent";
}
else if ( testscore >=41 )
{
studentmark = "hasteachersbook";
}
else
{
studentmark = "scoreLessThanTen";
}
Upvotes: 2
Reputation: 193131
First off, your problem is that you should use a single equals sign for assignment, e.g.
studentmark = "failed";
/// ^^^^ Note single =
Second, you're probably better off writing these if statements as
if (testscore <= 10) { ... }
else if (testscore <= 20) { ... }
else if (testscore <= 30) { ... }
...
Because that's easier to read, and the presence of the else
statement means you don't have to test that testscore
is greater than the amounts you've already covered.
Upvotes: 14
Reputation: 49105
Your question seems to be "what if my value would be true for more than one of the tests in the if-else chain?"
C++ evaluates in textual order, i.e. from top-to-bottom. Thus, the first predicate that is true for a given value, will be the branch selected.
For example:
int a = 2;
if (a > 0) {
cout << "First branch";
} else if (a > 1) {
cout << "Second branch";
}
Even though both tests would be true for a = 2
, the first one is selected because it comes first. Thus, First branch
is printed out.
Upvotes: 1
Reputation: 106609
studentmark == ( "closebutfailed" );
does not assign the value "closebutfailed" into studentmark
-- it does an equality comparison between "closebutfailed" and studentmark
, which presumably returns false
.
Change those to be assignment rather than comparison and try again.
Upvotes: 1
Reputation: 13855
Looking at it, you have a case for 31-40, then 49 and above.
There is no case for 41-48.
I dont know if this is possible in C++, but maybe look into using CASE
Upvotes: 2
Reputation: 17960
Your code doesn't work because you're attempting to do assignment with ==. There is no code in there that actually changes the value of studentmark.
Upvotes: 2
Reputation: 13192
==
tests for equality, =
assigns.
When you write studentmark == ( "failed" );
you are testing whether the string studentmark
is equal to "failed"
, then throwing away the answer.
Upvotes: 5