Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
Or, use your gamerDNA username: (more...)
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 04-13-2009, 11:43 AM   #1 (permalink)
prescient63
Registered User
 
Join Date: Jan 2002
Posts: 1,918
-42 Internets
C Switch Statement

This is driving me crazy. Basically, it just skips past the the if statement and doesn't get a character. I'm sure it's something ridiculously simple I'm doing incorrectly.
Quote:
Welcome to the Grade Calculation Program
Menu
1. Enter Test Grades
2. Enter Quiz grades
3. Enter Homework grades
4. Calculate final grade
5. Quit
Please enter your choice: 1
Are you sure you would like to enter new test scores? Previous values will be er
ased (y or n): Menu
1. Enter Test Grades
2. Enter Quiz grades
3. Enter Homework grades
4. Calculate final grade
5. Quit
Please enter your choice:
Below is the problem code and below that will be all the code. Thanks for any help!
Code:
 switch(selection) {
            case 1:
                printf("Are you sure you would like to enter new data? Previous values will be erased (y or n): ");
                yn = getchar();
                if (yn == 'y')
                    {
                    test();
                    }
                break;
Code:
#include 
#include 
int selection;
float quiz1, homework1, test1;



int welcome()
{
    printf("Welcome to the Grade Calculation Program\n");
}

int test()
{

    int num1, i;
    float grade, average;
    grade = average = 0;


    printf("How many tests do you have to enter? ");
    scanf("%i", &num1);
    for (i = 1; i <= num1; i++)
    {
        printf("%i. ", i);
        scanf("%f", &grade);
        average = average + grade;
    }
    average = average / num1;
    printf("Test average = %.2f\n\n", average);

    test1 = average;
    return (average);

}

int quiz()
{
    int num1, i;
    float grade, average;
    grade = average = 0;

    printf("How many quizes do you have to enter? ");
    scanf("%i", &num1);
    for (i = 1; i <= num1; i++)
    {
        printf("%i. ", i);
        scanf("%f", &grade);
        average = average + grade;
    }
    average = average / num1;
    printf("Quiz average = %.2f\n\n", average);
    quiz1 = average;
    return (average);
}
int homework()
{
    int num1, i;
    float grade, average;
    grade = average = 0;

    printf("How many homeworks do you have to enter? ");
    scanf("%i", &num1);
    for (i = 1; i <= num1; i++)
    {
        printf("%i. ", i);
        scanf("%f", &grade);
        average = average + grade;
    }
    average = average / num1;
    printf("Homework average = %.2f\n\n", average);
    homework1 = average;
    return (average);
}
int menu()
{
    printf("\tMenu\n");
    printf("1. Enter Test Grades\n2. Enter Quiz grades\n3. Enter Homework grades\n4. Calculate final grade\n5. Quit\n");
    printf("Please enter your choice: ");
}

int scan()
{
    scanf("%i", &selection);
    return(selection);
}

int final()
{
    float quiz, homework, test, final;


    final = .5 * test1 + .3 * quiz1 + .2 * homework1;
    printf("\nFinal Grade: %f.0\n\n", final);

}
int quit()
{
    printf("Thank you. Have a great day!\n\n");
}

int main()
{
char yn;



    welcome();

    do {
        menu();
        scan();

        switch(selection) {
            case 1:
                printf("Are you sure you would like to enter new test scores? Previous values will be erased (y or n): ");
                yn = getchar();
                if (yn == 'y')
                    {
                    test();
                    }
                break;
            case 2:
                quiz();
                break;
            case 3:
                homework();
                break;
            case 4:
                final();
                break;
            case 5:
                quit();
                break;
            default:
                printf("That is an invalid entry please try again.");
                break;
        }
    }while (selection != 5);
}
prescient63 is offline   Reply With Quote
Old 04-13-2009, 11:59 AM   #2 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 1,622
My only assumption is int selection is not being properly set. Does it only fail on case 1 or does it fail on every case?

If it fails for everything then I don't understand why selection is global to begin with in the first place. It's scope should be reduced to only the main() method.

Code:
 int selection = scan();
 switch(selection) {
            case 1:
                printf("Are you sure you would like to enter new data? Previous values will be erased (y or n): ");
                yn = getchar();
                if (yn == 'y')
                    {
                    test();
                    }
                break;
__________________
Quote:
Originally Posted by GrobbeeTrull2.0 View Post
If you can stomach it, most chicks I've been with absolutely go bananas when you blow your load in them, go down on them, make them cum, suck it out, and feed it to them in a big wet kiss.
Tenks is offline   Reply With Quote
Old 04-13-2009, 12:06 PM   #3 (permalink)
prescient63
Registered User
 
Join Date: Jan 2002
Posts: 1,918
-42 Internets
Quote:
Originally Posted by Tenks View Post
My only assumption is int selection is not being properly set. Does it only fail on case 1 or does it fail on every case?

If it fails for everything then I don't understand why selection is global to begin with in the first place. It's scope should be reduced to only the main() method.

Code:
 int selection = scan();
 switch(selection) {
            case 1:
                printf("Are you sure you would like to enter new data? Previous values will be erased (y or n): ");
                yn = getchar();
                if (yn == 'y')
                    {
                    test();
                    }
                break;
If I pull out the if statement the program works fine, and yes there is no need for the selection statement to be global. The selection will only fail for case 1. It has something to do with the If statement but i cant figure it out for the life of me.
prescient63 is offline   Reply With Quote
Old 04-13-2009, 12:13 PM   #4 (permalink)
Araex
Fires of Heaven WoW Officer
 
Join Date: Jul 2006
Location: Irvine, CA
Posts: 202
-2 Internets
The getchar() is returning the leftover newline character from the previous input.

You can substitute the getchar() line with something like
Code:
do {yn = getchar();} while (yn <= 32);
to filter the newline and whitespace characters.

P.S. Disclaimer, this is just a quick fix/hack to your program, and not how I would do it

Last edited by Araex; 04-13-2009 at 12:29 PM..
Araex is offline   Reply With Quote
Old 04-13-2009, 12:17 PM   #5 (permalink)
Zuuljin
So there's this plane on a treadmill...
 
Zuuljin's Avatar
 
Join Date: Jan 2005
Location: Southern California
Posts: 3,217
+20 Internets
Send a message via AIM to Zuuljin
nm thats probably it. =)
__________________
" We are all atheists about most of the gods that societies have ever believed in. Some of us just go one god further."
Richard Dawkins (1941 - ), "The Root of All Evil", UK Channel 4, 2006


Last edited by Zuuljin; 04-13-2009 at 12:19 PM..
Zuuljin is offline   Reply With Quote
Old 04-13-2009, 12:24 PM   #6 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 1,622
Is the issue getchar() does not set yn to a proper variable? I am not a C programmer by any stretch but from my quick google search it appears like getchar() is used to read a single char from a file and you may want to just use a scanf here.

-edit-

nm looks like a real C programmer answered
__________________
Quote:
Originally Posted by GrobbeeTrull2.0 View Post
If you can stomach it, most chicks I've been with absolutely go bananas when you blow your load in them, go down on them, make them cum, suck it out, and feed it to them in a big wet kiss.
Tenks is offline   Reply With Quote
Old 04-13-2009, 12:33 PM   #7 (permalink)
prescient63
Registered User
 
Join Date: Jan 2002
Posts: 1,918
-42 Internets
Quote:
Originally Posted by Araex View Post
The getchar() is returning the leftover newline character from the previous input.

You can substitute the getchar() line with something like
Code:
do {yn = getchar();} while (yn <= 32);
to filter the newline and whitespace characters.

P.S. Disclaimer, this is just a quick fix/hack to your program, and not how I would do it
Awesome thanks! I doubt anyone who has any clue what they were doing would do any of this program like I did

Edit: Props to everyone for the help!

Last edited by prescient63; 04-13-2009 at 12:36 PM..
prescient63 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On

uberguilds network



All times are GMT -7. The time now is 07:45 AM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6