Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdio.h>

int main()
{
    int marks;
    printf("Enter the Marks:");
    scanf("%d", &marks);

    if("marks >= 95"){
        printf("Grade is A+ \n");
    }
    else if("marks > 80 && marks < 95"){
        printf("Grade is B+ \n");
    }
    else if("marks > 70 && marks < 80"){
    printf("Grade is B \n");
    }
    else {
        printf("Grade is C \n");
    }
    printf("Thank you!");
    return 0;
}


What I have tried:

I have learned about if and else if function. I have tried compiling the code and its not running as it is supposed to.
Posted
Updated 20-Jan-24 5:45am
v2

C++
if("marks >= 95"){
   ^           ^

You have put your if expressions in double quotes which means they will never be evaluated*. Just remove the quotes so that the compiler can build the proper expression test.

*In reality, because the strings are not NULL they always evaluate as TRUE.
 
Share this answer
 
if and else if are not functions. They're language constructs.

You said you learned about if, but not enough. DO NOT put your conditional expressions inside quotes.
C++
if("marks >= 95"){

becomes
C++
if(marks >= 95)
{

Since you're a beginner, I HIGHLY suggest you put the opening curly brace on it's own line so you can more easily keep track of which braces line up with each other.
C++
if (marks >= 95)
{
    printf("Grade is A+ \n");
}
 
Share this answer
 
To add to what has been said, you almost certainly have a tool which would help you: it's called a debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need. What it does is allows you to execute your code line by line and look at variables to see what they contain, and follow where the code goes while it is running.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Two final notes.
Your conditions don't cover all cases (e.g., what happens if marks is 80 ?) and redundant (in the second if, for instance, you don't need to check for marks less than 95).
I would write the code this way:
C
#include <stdio.h>

int main()
{
  int marks;
  printf("Please enter the marks:");
  scanf("%d", &marks);
    
  if(marks >= 95)
  {
    printf("Grade is A+ \n");
  }
  else if(marks >= 80)
  {
    printf("Grade is B+ \n");
  }
  else if(marks >= 70 )
  {
    printf("Grade is B \n");
  }
  else 
  {
    printf("Grade is C \n");
  }

  printf("Thank you!");
  return 0;
}
 
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