Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have tried ... the c++ coding was very long...
I am using if(mark_1>=80&&mark_1<=100), it will be very long...

Any other ways of solution? Thanks

Whats-App-Image-2023-02-18-at-10-59-17-AM hosted at ImgBB — ImgBB[^]

What I have tried:

float A=4.00
if(mark_1>=80&&mark_1<=100)
{
gp_1=A
}
Posted
Updated 18-Feb-23 5:57am
Comments
Dave Kreskowiak 18-Feb-23 12:09pm    
Nobody has a clue what you're talking about. How can you do what "question?" You haven't said a word about what problem you're having and nothing about the "question."

You just say "it will be very long." WHAT will be "very long?"

In alternative to a long chain of if statements you could use an ordered container and the properly searching inside it. For example
C++
#include <iostream>
#include <map>
using namespace std;

string get_grade( const map <int, string> & gm, int marks )
{
  for (auto const & p : gm )
  {
    if ( p.first >= marks )
      return p.second;
  }
  return string("Err");
}


int main()
{
  map<int, string> grade_map{ {20, "E"}, {40, "D"}, {60, "C"}, {80, "B"}, {100, "A"}};

  const int marks = 49;

  cout << "grade of " << marks  << " marks is " << get_grade(grade_map, marks) << "\n";
}


or, using more the standard library power:
C++
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main()
{
  map<int, string> grade_map{ {20, "E"}, {40, "D"}, {60, "C"}, {80, "B"}, {100, "A"}};

  const int marks = 49;

  auto it = find_if( grade_map.begin(), grade_map.end(),  [] (const auto & x) { return (marks <= x.first); });

  if ( it != grade_map.end() )
    cout << "grade of " << marks  << " marks is " << it->second << "\n";
}
 
Share this answer
 
OK, there are a lot of problems with your question - starting with "it's impossible to answer".

Don't take this as me being nasty, or telling you off - all I'm trying to do is explain what your question looks like to us, and to educate you in what you need to do to get an answer to a question.

And at the moment, you don't actually ask a question!

Your link doesn't work - it comes back with a 404 error and most people won't follow it anyway as it looks dodgy at best: any really short URL like "ibb.co/2S3t5VY" could go anywhere and return anything. I tried to open it in the hope that it would have something useful at the other end, but only once I'd spun up a VM specifically for it in case there was something nasty waiting.
And the automated spam detector didn't like it either - which is why it and you went to moderation and didn't get released for several hours. We are pretty careful, as there are some very objectionable people who try to post some seriously nasty stuff regularly.
So ... don't post links. If you want to share an assignment or code, copy and paste it rather than trying to take the easy way and throwing a snapshot at us.

Secondly, read your question carefully. what does it mean? I have no idea - the code doesn't seem to relate to the text at all!

So ... this is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.

Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!
I'd also strongly suggest that you have a look here: Asking questions is a skill[^] and think about what you need to know, and what you need to tell us in order to get help.

Than ask an actual question, with information that helps us to help you!
 
Share this answer
 
The source code under "What I have tried:" unfortunately does not present the problem and even with imagination it is not possible to see which solution was tried here. Unfortunately the link does not work either. Having to follow an anonymous link is perceived as unpleasant. Therefore it is not to be expected that someone could suggest alternatives to it.
C++
float A=4.00
if(mark_1>=80&&mark_1<=100)
{
gp_1=A
}

Any other ways of solution? Thanks

It would be necessary to describe the task for which a solution is sought.
Then an explanation what is wrong with the own solution attempt and finally also a source code that makes sense would be necessary.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900