0

I am making a program for some friends and myself that calculates grades with the weighted percentage and what grades need to me made on the final to pass the class with a specified grade.

The for loop in question is this,

for(;;)
{
    scanf("%f", &s->grade);
    if(s->grade == 'x')
    {
        break;
    }else{
        grade += s->grade;
    }
}

how can I make this exit through the use of a letter rather than a number?
If you need any more code feel free to ask.

13
  • 3
    There's a comma in your scanf call, right? Commented Jul 20, 2014 at 1:30
  • 1
    Read the manual page for scanf as well Commented Jul 20, 2014 at 1:31
  • 2
    @JoelCornett you mean the floating point representation of 120 is the same as the char code for x? I did not know that. Commented Jul 20, 2014 at 1:36
  • 3
    scanf returns the number of arguments assigned. Check that! Commented Jul 20, 2014 at 1:40
  • 2
    @Deduplicator: If you make an answer I will surely upvote. scanf does omit all preceding whitespace (unless %c is used), then if first non-whitespace character is non-digit (or possibly +, - sign), then it "fails" and does not read more (in fact such "unwanted" character is given back to stdin stream and no futher characters are processed. Commented Jul 20, 2014 at 1:46

4 Answers 4

3

Either check the return value of scanf--EOF means nothing was parsed/scanned.

Or, use a two step process. First, read a string and check if it equals "x" or whatever, it not, use sscanf to convert the string into a float.

Sign up to request clarification or add additional context in comments.

2 Comments

I wanted to avoid a 2 step process was the issue I was having.
@MackenzieBinns No, you don't want to avoid that, you want to do that, because it's how to solve your problem. Use fgets, check for 'x' and then use sscanf if it isn't 'x'.
0

You are already asking for a floating point number, and you are checking this against the char 'x', so just replace the 'x' with the value you want or do it in an or.

1 Comment

The OP wants to know "how can I make this exit through the use of a letter rather than a number?"
0

So I believe that you're asking for a percentage received from somewhere and want to convert that to a letter grade.

If so, I would receive the percentage either from another function passed into the code you have or read from a file.

Then, I would create a switch statement which takes the value received. Truncate it to an int. Then, cast that char you want for the given value. Take that char and bring it into the function you have.

Scanf("%f", x);

Function(x){ Switch(x){ Case 90: (Char*)x = 'a'; Return x; ... }

Comments

0
char buf[100];

while (fgets(buf, sizeof buf, stdin) && *buf != 'x')
{
    sscanf(buf, "%f", &s->grade); // should check return value of sscanf to see if the input was correct
    grade += s->grade;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.