Link to home
Start Free TrialLog in
Avatar of flubbster
flubbsterFlag for United States of America

asked on

Need help with functions using strings in C programming. What am I doing wrong here?

Hello everyone. I am trying to learn C on my own and I am stuck badly. I am very confused about functions. I am trying to write a program that allows the user to enter a number between 0 and 100. Then, using functions, specifically a function called numToLetter, the program needs to do a few things.

(1)Test the entered value to verify it is between 0 and 100. If not, exit out.

(2) Test the number, using the function numToLetter, against the following limits:

Between 60 and 70, return D
Greater than 90, return A
between 70 and 80, return C
Between 0 and 60, return F
Between 80 and 90, return B

The above is the exact order shown in the exercise (crazy, I know)

(3) print (display) "The letter grade for a score of <number entered> is <letter grade>

ex. The letter grade for a score of 93 is A.

I can't figure out what I am doing wrong. Can anyone help me to finish this program???

One final note on this. Per the exercise, you can not use any global variable, everything must be done with local variables from within main.

Thanks. Code listing below. Once more, this is in C only, not C+ or C++

The m=70 represents the numeric score.

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
// function prototype, also called function declaration
int limit_test ( int x ); 
char numToLetter(char letter_grade);

// main function, program starts from here
int main( )               
{

        int m,n;
        char letter_grade;
        char letter(1);
        m=70;
        // function call
        n = limit_test( m );
        letter=numToLetter(letter_grade);
        printf ("\nYour Letter Grade is %s",letter);
        //IGNORE THIS printf ( "\nSquare of the given number %d is %d",m,n );

}

int limit_test ( int x )   // function definition
{
        if (x<0)
        {
        printf("\nToo Low");
        exit(0);
        }
        else if (x>100)
        {
        printf("\nToo High");
        exit(0);
        }
        int p ;
        p = x * x ;
        return ( p ) ;
}
char numToLetter(char letter_grade)
        if (x<60)
        {
        letter_grade="F";
        return (char letter_grade);
        }
        else if (x<70)
        {
        letter_grade="D";
        return (char letter_grade);
        }
        else if (x<80)
        {
        letter_grade="C";
        return (char letter_grade);
        }
        else if (x<=90)
        {
        letter_grade="B";
        return (char letter_grade);
        }
        else if (x>90)
        {
        letter_grade="A";
        return (char letter_grade);
        }
        exit(0);

Open in new window

Avatar of it_saige
it_saige
Flag of United States of America image

Try something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration
int limit_test (int x);
char numToLetter (int grade);

int main()
{
    int m = 70;
    char letter = numToLetter(limit_test(m));
    // function call
    printf("\nThe letter grade for a score of %d is %c\n", m, letter);

    return 0;
}

int limit_test (int x)
{
    if (x < 0)
    {
        printf("\nGrade is too low\n");
        exit(0);
    }
    else if (x > 100)
    {
        printf("\nGrade is too high\n");
    }
    else
    {
        return x;
    }
}

char numToLetter( int grade )
{
    if (grade < 60)
    {
        return 'F';
    }
    else if (grade < 70)
    {
        return 'D';
    }
    else if (grade < 80)
    {
        return 'C';
    }
    else if (grade < 90)
    {
        return 'B';
    }
    else
    {
        return 'A';
    }
}

Open in new window

Produces the following output -User generated image-saige-
Avatar of flubbster

ASKER

saige... thank you for your help. I ran it and it seems to have an issue where if the numeric score is outside the allowable range, greater than 100, it reports a grade of "F" instead of quitting with just the message that it is too high. Does it need another exit(0) there?

Also, I need to add a function that asks the user to input the number value. Right now I just have it hardcoded as you see. Is it difficult to do another function to get a user input? Would that just be a scanf command that returns the 'm' value. Still a bit confused.

Thanks again.
Yes there should be an exit(0) after the 'printf("\nGrade is too high\n");' line, that was an oversight on my part.  As for an additional function, you are correct.

What is confusing you?  Let's see if we can shed some light on that.

-saige-
Ok. I just tried to do a function to print the instructions to the user. Basically 2 lines. Below is the code I added.

Added this to the function declarations at the top:

int instructions (int grade_conversion);

Nothing really gets returned here. I just want a function to print out 2 lines. I then added this to the code:

int instructions (int grade_conversion)
{
    printf("\nEnter a value from 0 to 100");
    printf("\nThe program will convert the number into the equivalent grade score");
    return (0);
}

btw... I could not figure out how to place the code in a code block... hmmmm

Once I get this, I will work on the user input.
almost forgot. When I add the code above, no errors, but nothing is displayed. lol
Here is a quick and dirty example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration
int getGrade ();
int limit_test (int grade);
char numToLetter (int grade);

int main()
{
    int m = getGrade();
    char letter = numToLetter(limit_test(m));
    // function call
    printf("\nThe letter grade for a score of %d is %c\n", m, letter);

    return 0;
}

int getGrade()
{
    int grade;
    printf("Please enter a grade number between 0 and 100:");
    scanf("%d", &grade);
    return grade;
}

int limit_test (int x)
{
    if (x < 0)
    {
        printf("\nGrade is too low\n");
        exit(0);
    }
    else if (x > 100)
    {
        printf("\nGrade is too high\n");
        exit(0);
    }
    else
    {
        return x;
    }
}

char numToLetter( int grade )
{
    if (grade < 60)
    {
        return 'F';
    }
    else if (grade < 70)
    {
        return 'D';
    }
    else if (grade < 80)
    {
        return 'C';
    }
    else if (grade < 90)
    {
        return 'B';
    }
    else
    {
        return 'A';
    }
}

Open in new window

Produces the following output -User generated image
However, can you identify the inherent problems with this kind of implementation?

-saige-
The code you added should not return anything because it just displays text.  This is the purpose of a void function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration
void instructions();
int getGrade ();
int limit_test (int grade);
char numToLetter (int grade);

int main()
{
    instructions();
    int m = getGrade();
    char letter = numToLetter(limit_test(m));
    // function call
    printf("\nThe letter grade for a score of %d is %c\n", m, letter);

    return 0;
}

void instructions()
{
    printf("Enter a value from 0 to 100\n");
    printf("The program will convert the number into the equivalent grade score\n");
}

int getGrade()
{
    int grade;
    printf("Please enter a grade number between 0 and 100:");
    scanf("%d", &grade);
    return grade;
}

int limit_test (int x)
{
    if (x < 0)
    {
        printf("\nGrade is too low\n");
        exit(0);
    }
    else if (x > 100)
    {
        printf("\nGrade is too high\n");
        exit(0);
    }
    else
    {
        return x;
    }
}

char numToLetter( int grade )
{
    if (grade < 60)
    {
        return 'F';
    }
    else if (grade < 70)
    {
        return 'D';
    }
    else if (grade < 80)
    {
        return 'C';
    }
    else if (grade < 90)
    {
        return 'B';
    }
    else
    {
        return 'A';
    }
}

Open in new window

Produces the following output -User generated image-saige-
Actually let me qualify my void function remark.  The purpose of a void function is to process the instructions specified in its body without the need to return a value.

-saige-
I can think of a couple of things. Besides having to test for the stated limits of 0 to 100, you would need to test for any non-numeric characters such as alphabetic, symbols, etc. Also, it would sit there waiting for an input.

Here is the code I have now, with your fantastic help. I just can't get the instructions to display properly. Not sure if it is the shell I am using. It is an on-line compiler for C++ that can be used for C also.

The website is:  

http://cpp.sh/

Figured out the code block.. yay lol

here is the latest:

Oh yeah... Just now realized that all the functions must be called from the main, so I moved a few things but I think it is ok. What do you think?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    //function declarations
int instructions ();
int getGrade ();
int limit_test (int x);  // Test for limits
char numToLetter (int grade);  // Convert number to letter grade

    int m = getGrade();
    char letter = numToLetter(limit_test(m));
    // function call
    printf("\nThe letter grade for a score of %d is %c\n", m, letter);

    return 0;
}

int instructions ()
{
    printf("\nEnter a value from 0 to 100");
    printf("\nThe program will convert the number into the equivalent grade score");
    return (0);
}
    
int getGrade()
{
    int grade;
    printf("Please enter a grade number between 0 and 100:");
    scanf("%d", &grade);
    return grade;
}

int limit_test (int x) // Test Limits
{
    if (x < 0)
    {
        printf("\nGrade is too low\n");
        exit(0);
    }
    else if (x > 100)
    {
        printf("\nGrade is too high\n");
        exit(0);
    }
}

char numToLetter( int grade ) // Convert number to grade
{
    if (grade < 60)
    {
        return 'F';
    }
    else if (grade < 70)
    {
        return 'D';
    }
    else if (grade < 80)
    {
        return 'C';
    }
    else if (grade < 90)
    {
        return 'B';
    }
    else
    {
        return 'A';
    }
}

Open in new window

It looks like the call to numToLetter in turn calls the limit test. Is that correct? That's cool.
I need to cut out. Will check back in tomorrow. Thank you for your help so far saige. I will probably close this tomorrow in case I need any other questions.

Thanks again.
Correct, methods can use other methods as a parameter if the returned value from the called method matches the parameter type in the method prototype.  But it is often times better to be verbose when dealing with methods so that you can identify problems.

It is also easier to read your code and understand it's intentions when you are more verbose.

-saige-
Correct, methods can use other methods as a parameter...
I propose a slight modification to that statement:

Correct, methods can use other methods' return values as a parameters...

...only because it is also possible to pass functions themselves (via function pointers) to other functions. Function pointers are kind of an advanced topic, though, and not really pertinent to this task.
@Kaufmed, I like your statement better than mine.  It's clearer and more concise.

-saige-
Just saw this as I was leaving and thought I would respond. I don't quite understand that, though I certainly like the methodology.

If the two functions were not combined, how would the code change? Could you possibly post an alternate version showing the two functions (numToLetter and limit_test) working individually so that I can better understand the syntax of each?

Thanks yet again.
Yes you can:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration
void instructions();  // Displays instructions.
int getGrade ();  // Gets a grade from the user.
int limit_test (int grade); // Tests the grade parameter to ensure that it is not less than 0 or greater than 100.
char numToLetter (int grade);  // Produces a letter value that represents the grade parameter.

int main()
{
    // Print the instructions.
    instructions();
    // Get the grade and assign it to a variable.
    int m = getGrade();
    // Test the grade and assign it to a variable.
    int n = limit_test(m);
    // Get the letter value of the grade and assign the letter value to a variable.
    char letter = numToLetter(n);
    // Print the results to the screen.
    printf("\nThe letter grade for a score of %d is %c\n", n, letter);
    return 0;
}

void instructions()
{
    printf("Enter a value from 0 to 100\n");
    printf("The program will convert the number into the equivalent grade score\n");
}

int getGrade()
{
    int grade;
    printf("Please enter a grade number between 0 and 100:");
    scanf("%d", &grade);
    return grade;
}

int limit_test (int x)
{
    if (x < 0)
    {
        printf("\nGrade is too low\n");
        exit(0);
    }
    else if (x > 100)
    {
        printf("\nGrade is too high\n");
        exit(0);
    }
    else
    {
        return x;
    }
}

char numToLetter( int grade )
{
    if (grade < 60)
    {
        return 'F';
    }
    else if (grade < 70)
    {
        return 'D';
    }
    else if (grade < 80)
    {
        return 'C';
    }
    else if (grade < 90)
    {
        return 'B';
    }
    else
    {
        return 'A';
    }
}

Open in new window

Produces the following output -User generated image-saige-
Hello saige. Thank you very much for your help. I understand it so much more now presented in this manner. I would like to ask one more change if I may. I was trying to change it so that a floating value, such as 85.5 could be entered, but was running into trouble. Could you do one more change to this to allow that. SO it should allow a value like 85.5 to be entered, and then display the same number in the final output like it does now. I am actually copying each version to notepad documents so that I can study your progression for each iteration. It is a great learning tool for me. After having helped so many people on here over the years, it is a great feeling to get some help from a fellow expert when needed.

Once more, thanks again for your fast, exceptional, and kind assistance.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahhhhh... I see what I was doing wrong in this last iteration. Thank you. I now have 3 documents showing the various stages of the working program.

The first with the combined functions
The second with separate functions
The third with the floating format inputs and outputs.

I can hopefully use these as I move through to the next stages in my book. What a difference learning something on your own and not in a classroom environment.

Anyway, I am going to set you free.... for now anyway lol. Thanks for all your help. I am sure I will be posting on here for more help in the future. Hope you are still around to set me straight.

Take care saige....
Fantastic help
Not a problem.  Glad I could be of assistance.

-saige-