# 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 );

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

int m,n;
char letter(1);
m=70;
// function call
n = limit_test( m );
//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 ) ;
}
if (x<60)
{
}
else if (x<70)
{
}
else if (x<80)
{
}
else if (x<=90)
{
}
else if (x>90)
{
}
exit(0);
``````
LVL 30
###### Who is Participating?

x

DeveloperCommented:
Sure thing:
``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration
void instructions();  // Displays instructions.
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.
// 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 limit_test (float x)
{
if (x < 0.0f)
{
exit(0);
}
else if (x > 100.0f)
{
exit(0);
}
else
{
return x;
}
}

{
{
return 'F';
}
{
return 'D';
}
{
return 'C';
}
{
return 'B';
}
else
{
return 'A';
}
}
``````
Produces the following output --saige-
0

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

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

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)
{
exit(0);
}
else if (x > 100)
{
}
else
{
return x;
}
}

{
{
return 'F';
}
{
return 'D';
}
{
return 'C';
}
{
return 'B';
}
else
{
return 'A';
}
}
``````
Produces the following output --saige-
0

Author Commented:
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

DeveloperCommented:
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

Author Commented:
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:

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

{
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

Author Commented:
almost forgot. When I add the code above, no errors, but nothing is displayed. lol
0

DeveloperCommented:
Here is a quick and dirty example:
``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration

int main()
{
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)
{
exit(0);
}
else if (x > 100)
{
exit(0);
}
else
{
return x;
}
}

{
{
return 'F';
}
{
return 'D';
}
{
return 'C';
}
{
return 'B';
}
else
{
return 'A';
}
}
``````
Produces the following output -
However, can you identify the inherent problems with this kind of implementation?

-saige-
0

DeveloperCommented:
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 main()
{
instructions();
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 limit_test (int x)
{
if (x < 0)
{
exit(0);
}
else if (x > 100)
{
exit(0);
}
else
{
return x;
}
}

{
{
return 'F';
}
{
return 'D';
}
{
return 'C';
}
{
return 'B';
}
else
{
return 'A';
}
}
``````
Produces the following output --saige-
0

DeveloperCommented:
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

Author Commented:
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 limit_test (int x);  // Test for limits

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 limit_test (int x) // Test Limits
{
if (x < 0)
{
exit(0);
}
else if (x > 100)
{
exit(0);
}
}

{
{
return 'F';
}
{
return 'D';
}
{
return 'C';
}
{
return 'B';
}
else
{
return 'A';
}
}
``````
0

Author Commented:
It looks like the call to numToLetter in turn calls the limit test. Is that correct? That's cool.
0

Author Commented:
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

DeveloperCommented:
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

Commented:
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

DeveloperCommented:
@Kaufmed, I like your statement better than mine.  It's clearer and more concise.

-saige-
0

Author Commented:
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

DeveloperCommented:
Yes you can:
``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//function prototype, also called function declaration
void instructions();  // Displays instructions.
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.
// 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 limit_test (int x)
{
if (x < 0)
{
exit(0);
}
else if (x > 100)
{
exit(0);
}
else
{
return x;
}
}

{
{
return 'F';
}
{
return 'D';
}
{
return 'C';
}
{
return 'B';
}
else
{
return 'A';
}
}
``````
Produces the following output --saige-
0

Author Commented:
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

Author Commented:
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

Author Commented:
Fantastic help
0

DeveloperCommented:
Not a problem.  Glad I could be of assistance.

-saige-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.