Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9207925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:29:52+00:00 2026-06-18T00:29:52+00:00

This is my code, I don’t really like it and I know it can

  • 0

This is my code, I don’t really like it and I know it can be improved but I just don’t know how. How can I refactor this program in terms of the many std::cin and std::cout operations over each other on each line? Is there any STL library magic that can be done to improve this? Thanks.

#include <iostream>
#include <string>

int main() {

   int grade;

   std::cout << "Enter the grade you recieved: ";
   std::cin >> grade;
   std::cout << grade << "\n\n";

   if (grade == 100) {
      std::cout << "You got a perfect score!\n";
   } else if (grade > 90 && grade < 100) {
      std::cout << "You got an A\n";
   }

   if (grade == 100) {
      std::cout << "Grade: A+";
   } else if (grade > 90 && grade < 100) {
      std::cout << "Grade : A";
   } else if (grade > 80 && grade < 90) {
      std::cout << "Grade: B";
   } else if (grade > 70 && grade < 80) {
      std::cout << "Grade: C";
   } else if (grade > 60 && grade < 70) {
      std::cout << "Grade: D";
   } else if (grade < 60) {
      std::cout << "Grade: F";
   }

}

To make it clear, I’m wanting to improve this code because it seems very un-idiomaitc to use all these if statements and print functions. How can I take advantage of the STL or template functions/classes to refactor this? C++11 is also welcome. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:29:53+00:00Added an answer on June 18, 2026 at 12:29 am
    std::string GetGrade(int grade);
    
    cout<<GetGrade(grade);
    

    Is one way to go to clean up stuff. Basically move all the ifs into the GetGrade function and call cout once.

    Once inside GetGrade, you can use an array of the below and do away with the ifs in favour of a loop.

    struct Grade{
        int start,end;
        std::string gradeStr;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody help with this code I just don't know where I am going
I have this code and don't know if what I would like to achieve
Hello everyone don't know why this code works on windows but not on linux
So I created this code (probably don't even have to show it- but just
can anyone tell me why this code don't work: $q = $_GET['q']; // Load
I was just looking at this code and i don't understand what RETURN TRUE
this code always return '0' i don't know why Dim cur_month = DatePart(DateInterval.Month, Now).ToString()
This code is giving me this error, which I don't understand. I can't even
I've got this code and I don't know how to order my query function
At the moment I have this code (and I don't like it): private RenderedImage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.