Thread: problems with functions

  1. #1
    catalyst
    Guest

    Question problems with functions

    Ok, here are the two functions I using:

    int Get_Valid_Type()
    {
    int type;

    gotoxy(3,4);
    textcolor(YELLOW);
    cprintf("Types of Seeds:\n");
    gotoxy(5,5);
    textcolor(RED);
    cprintf("1.");
    textcolor(WHITE);
    cprintf(" Wheat ");
    textcolor(RED);
    cprintf("2.");
    textcolor(WHITE);
    cprintf(" Rice ");
    textcolor(RED);
    cprintf("3.");
    textcolor(WHITE);
    cprintf(" Rye ");
    textcolor(RED);
    cprintf("4.");
    textcolor(WHITE);
    cprintf(" Oats ");
    textcolor(RED);
    cprintf("5.");
    textcolor(WHITE);
    cprintf(" Barley ");
    textcolor(RED);
    cprintf("6.");
    textcolor(WHITE);
    cprintf(" Corn ");
    textcolor(RED);
    cprintf("7.");
    textcolor(WHITE);
    cprintf(" Sorghum ");
    textcolor(RED);
    cprintf("8.");
    textcolor(WHITE);
    cprintf("Other");
    gotoxy(3,7);
    textcolor(GREEN);
    cprintf("Select type of seed:");
    textcolor(BLUE);
    gotoxy(24,7);
    fflush(stdin);
    cscanf("%d",&type);
    if (type == 0 || type > 8)
    {
    gotoxy(23,9);
    textcolor(RED);
    cprintf("ERROR! Please enter from menu");
    Get_Valid_Type();
    }
    else if (type >= 1 && type <=8)
    {
    return type;
    }
    fflush(stdin);
    }

    char Get_Valid_Age()
    {
    char age;
    gotoxy(3,4);
    textcolor(YELLOW);
    cprintf("Age of Seeds:");
    gotoxy(5,5);
    textcolor(RED);
    cprintf("C:");
    textcolor(WHITE);
    cprintf(" Cold Storage ");
    textcolor(RED);
    cprintf("L:");
    textcolor(WHITE);
    cprintf(" Last Years Stock ");
    textcolor(RED);
    cprintf("F:");
    textcolor(WHITE);
    cprintf(" Fresh ");
    gotoxy(3,7);
    textcolor(GREEN);
    cprintf("Select C, L, or F: ");
    gotoxy(22,7);
    textcolor(BLUE);
    do {
    cscanf("%[CLF]",&age);
    fflush(stdin);
    if (age == 0)
    {
    gotoxy(23,9);
    textcolor(RED);
    cprintf("ERROR! Invalid selection. Try again");

    }
    } while(age == 0);
    return age;
    }

    the first function has to return a value from 1 to 8. and nothing else. the second function has to return one of the three letters. problem is whenever I run this program after I type in the input for the first function it goes through 2nd function and makes the Error message appear and doesn't let me type in the 2nd input. I think is something to do with the memory locations of where the variables are being stored.

    This is a program for school if you were wondering and this is the only bug that appears now. Thanks in advance to the people that help. If you want more code, just post.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    fixed

    don't worry about it.. i just fixed the problem turned out that i should have had a space in scanf() where i didn't

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    That's not the only thing wrong
    1. there's all that poor use of fflush(stdin);
    It's an undefined operation - which means it's not the same everywhere you go. It might work for you, but it wont work for me.

    This should work everywhere
    Code:
    while ( getchar() != '\n' ) continue;
    2. Get_Valid_Type() calling itself recursively is not a good idea - when all you really need is a while loop.

    3. cscanf("%[CLF]",&age);
    scanf scan sets are like %s, not %c, so age needs to be an array of char, not a char
    char age[2]; // at least 2 - one char, and a \0

    cscanf("%1[CLF]",age);

    return age[0];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  2. Problems with polymorphism
    By roktsyntst in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 02:22 PM
  3. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  4. Problems with functions in a class with pointer to self
    By BigDaddyDrew in forum C++ Programming
    Replies: 6
    Last Post: 02-03-2003, 11:24 PM
  5. problems passing parameters between functions
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 11:41 AM