Solved

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

Posted on 2015-02-12
22
183 Views
Last Modified: 2015-02-13
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

0
Comment
Question by:flubbster
  • 10
  • 10
22 Comments
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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 -Capture.JPG-saige-
0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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-
0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
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.
0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
almost forgot. When I add the code above, no errors, but nothing is displayed. lol
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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 -Capture.JPG
However, can you identify the inherent problems with this kind of implementation?

-saige-
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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 -Capture.JPG-saige-
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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-
0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
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

0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
It looks like the call to numToLetter in turn calls the limit test. Is that correct? That's cool.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 30

Author Comment

by:flubbster
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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-
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
@Kaufmed, I like your statement better than mine.  It's clearer and more concise.

-saige-
0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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 -Capture.JPG-saige-
0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
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.
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
Sure thing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration
void instructions();  // Displays instructions.
float getGrade ();  // Gets a grade from the user.
float limit_test (float grade); // Tests the grade parameter to ensure that it is not less than 0 or greater than 100.
char numToLetter (float 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.
    float m = getGrade();
    // Test the grade and assign it to a variable.
    float 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 %.2f 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");
}

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

float limit_test (float x)
{
    if (x < 0.0f)
    {
        printf("\nGrade is too low\n");
        exit(0);
    }
    else if (x > 100.0f)
    {
        printf("\nGrade is too high\n");
        exit(0);
    }
    else
    {
        return x;
    }
}

char numToLetter(float grade)
{
    if (grade < 60.0f)
    {
        return 'F';
    }
    else if (grade < 70.0f)
    {
        return 'D';
    }
    else if (grade < 80.0f)
    {
        return 'C';
    }
    else if (grade < 90.0f)
    {
        return 'B';
    }
    else
    {
        return 'A';
    }
}

Open in new window

Produces the following output -Capture.JPG-saige-
0
 
LVL 30

Author Comment

by:flubbster
Comment Utility
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....
0
 
LVL 30

Author Closing Comment

by:flubbster
Comment Utility
Fantastic help
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
Not a problem.  Glad I could be of assistance.

-saige-
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
if loop in java 3 114
tenRun challenge 28 81
word0 challenge 4 52
WPF issue with Trigger 2 65
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now