Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,176 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,395 people online right now. Registration is fast and FREE... Join Now!




Help with a Beginning C Program

 
Reply to this topicStart new topic

Help with a Beginning C Program

tristangreer
2 Jun, 2008 - 04:19 PM
Post #1

New D.I.C Head
*

Joined: 2 Jun, 2008
Posts: 4

I need help with a beginning C program. It's a program designed to take numerical grades and convert them to letter grades, and then output both the numerical and letter grades. In addition, it has a loop which should allow someone to type y and enter another grade. If they hit n, it should stop.

The program I've coded has a couple of issues. First, any numerical grade I enter gives me a letter grade of 70 (which isn't even a letter!) How do I fix this?

In addition, when I press y to enter a new grade, it takes me back to the "do you want to enter another grade?" statement. I'm not sure how I screwed up the loop on this one.

Any help you could give would be greatly appreciated.

CODE


#include<stdio.h>
#pragma warning(disable:4996)

int getNumGrade ();
int calcLetterGrade();

int main (void)
{
    int numgrade = 0;
    int lettergrade = 0;
    char cgoAgain = 'y';
    printf ("Enter numerical grade value now:");
    numgrade = getNumGrade ();
    lettergrade = calcLetterGrade();
    printf ("\n When numerical grade is %d,",numgrade);
    printf ("letter grade is %d.", lettergrade);

while (cgoAgain =='y' || cgoAgain == 'Y')
{
    printf("\nDo you want to enter another grade?");
    scanf("%c%*c",&cgoAgain);
}
return 0;
}

int getNumGrade()
{
int numgrade = 0;
scanf("%d%*c",&numgrade);
return numgrade;
}

int calcLetterGrade (numgrade)
{
    int lettergrade;
    if (numgrade < 60)
        lettergrade = 'F';
    else if (numgrade < 70)
        lettergrade = 'D';
    else if(numgrade < 80)
        lettergrade = 'C';
    else if (numgrade <90)
        lettergrade = 'B';
    else if (numgrade <100)
        lettergrade = 'A';
    return lettergrade;
}


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Help With A Beginning C Program
2 Jun, 2008 - 04:43 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,229



Thanked: 40 times
Dream Kudos: 25
My Contributions
You get a letter grade of 70 because you are setting the letter grade value as an integer data type - I expect you actually wish to use a char variable type.

Your loop is functioning exactly as designed - what functionality did you want in the loop? Currently, all you do in the loop is prompt for user input.
User is online!Profile CardPM
+Quote Post

tristangreer
RE: Help With A Beginning C Program
2 Jun, 2008 - 04:48 PM
Post #3

New D.I.C Head
*

Joined: 2 Jun, 2008
Posts: 4

I wanted the loop to basically repeat the whole getNumGrade function again. I wanted to be able to type "y" and enter a new numerical grade, which basically repeats the whole process again.

so instead of int calcLetterGrade and int lettergrade they should be char calcLetterGrade and char lettergrade?
User is offlineProfile CardPM
+Quote Post

skater_00
RE: Help With A Beginning C Program
2 Jun, 2008 - 05:23 PM
Post #4

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
The code below is C++, but it does what you want your program to do. Just convert it to C.

cpp

#include <iostream>

using namespace std;

int main()
{
short int numgrade(0);
char lettergrade, input('y');

while (input == 'y' || input == 'Y') {
cout << "Enter a numeric grade (0 - 100): ";
cin >> numgrade;

if (numgrade >= 0 && numgrade <= 100) {
if (numgrade >= 0 && numgrade < 60) lettergrade = 'F';
else if (numgrade >= 60 && numgrade < 70) lettergrade = 'D';
else if (numgrade >= 70 && numgrade < 80) lettergrade = 'C';
else if (numgrade >= 80 && numgrade < 90) lettergrade = 'B';
else if (numgrade >= 90 && numgrade <= 100) lettergrade = 'A';
cout << endl << "For a numeric grade of " << numgrade << ", the corresponding letter grade is " << lettergrade << "." << endl << endl;
} else
cout << endl << "Invalid numeric grade! Please try again." << endl << endl;

while (true) {
cout << "Do you want to enter another numeric grade? (y/n) ";
cin >> input;
cout << endl;

if (input == 'y' || input == 'Y')
main();
else if (input == 'n' || input == 'N')
break;
else {
cout << endl << "Invalid entry! Please try again." << endl << endl;
continue;
}
}
}

return 0;
}


Hope this helps.
User is offlineProfile CardPM
+Quote Post

Whizzy
RE: Help With A Beginning C Program
2 Jun, 2008 - 05:34 PM
Post #5

Unregistered





and by the way Tristangreer, Welcome to our little family here at Dream in Code. biggrin.gif
+Quote Post

tristangreer
RE: Help With A Beginning C Program
2 Jun, 2008 - 05:50 PM
Post #6

New D.I.C Head
*

Joined: 2 Jun, 2008
Posts: 4

It does help, but I'm totally new to C, and I've never touched C++. Is there a way to convert it easily?
User is offlineProfile CardPM
+Quote Post

skater_00
RE: Help With A Beginning C Program
2 Jun, 2008 - 06:12 PM
Post #7

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
I don't know C so I hope the code below is C. It's only the first part of the program though. The second part loves to battle me.

c

#include <stdio.h>

int main()
{
short int numgrade(0);
char lettergrade, input('y');

while (input == 'y' || input == 'Y') {
printf("Enter a numeric grade (0 - 100): ");
scanf_s("%d", &numgrade);

if (numgrade >= 0 && numgrade <= 100) {
if (numgrade >= 0 && numgrade < 60) lettergrade = 'F';
else if (numgrade >= 60 && numgrade < 70) lettergrade = 'D';
else if (numgrade >= 70 && numgrade < 80) lettergrade = 'C';
else if (numgrade >= 80 && numgrade < 90) lettergrade = 'B';
else if (numgrade >= 90 && numgrade <= 100) lettergrade = 'A';
printf("\nFor a numeric grade of %d, ", numgrade);
printf("the corresponding letter grade is %c.\n\n", lettergrade);
} else
printf("\nInvalid numeric grade! Please try again.\n\n");
}

return 0;
}


This post has been edited by skater_00: 2 Jun, 2008 - 06:12 PM
User is offlineProfile CardPM
+Quote Post

tristangreer
RE: Help With A Beginning C Program
2 Jun, 2008 - 06:52 PM
Post #8

New D.I.C Head
*

Joined: 2 Jun, 2008
Posts: 4

We're getting close. I can feel it, but that code doesn't look quite right. Could anyone tweak my code?

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 10:55AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month