Why won't this work?

Started by
7 comments, last by Icefox 18 years, 9 months ago
this is my code:

#include <stdio.h>
#include <stdlib.h>

void VERSION(void);
void COMMANDS(void);
void win(void);
void help(void);
void comport(void);
void filelist(void);
void fopn(void);

void main()
{
    win();
}

void comport(void)
{
    char com;
    
    printf("\n ZUF 1.0\n");
    com = getchar();
    if (com == '32') win();
    if (com == 'ver') VERSION();
    if (com == 'help') help();
    if (com == 'win') win();
    if (com == 'file') filelist();
    if (com == 'exit') win();
    if (com == 'ofe') fopn();
    comport();

}

void VERSION(void)
{
    printf(" ZUF 1.0\n");
    comport();
}

void filelist(void)
{
    FILE * pFile;
    char c;
    char yn;

    pFile = fopen("myfile.txt","r");
    if (pFile!=NULL)

        {
          do {
            c = getc (pFile);
            if (c == '\\') printf("\n");
            else printf("%d",c);
            } while (c != EOF);
          fclose (pFile);
        }
        printf("\n\nopen file Y\\N\\y\\n ");
        yn = getchar();
        if (yn == 'y') fopn();
        if (yn == 'Y') fopn();
        else comport();

}

void help(void)
{
    printf("32 = Start the visual OS\n");
    printf("win = Start the visual OS\n");
    printf("ver = Version\n");
    printf("file = File list\n");
    printf("exit = Start the visual OS\n");
    printf("ofe = open a bin file DO NOT put .bin at the end\n");
    printf("help = this screen\n");
    comport();
}

void fopn(void)
{
    char c;
    char fi[25];
    int n = 0;
    
    scanf("%s",fi);
    FILE * pFiletwo;
    
    pFiletwo = fopen("%s.fil","rt");
    if (pFiletwo!=NULL)
    {
        do{
            int n = 0;
            do{
                c=getc(pFiletwo);
                if (c == '$') n++;
            } while (c != '$');
            if (n == '0')
            {
                do{
                c=getc(pFiletwo);
                if (c == '\\') printf("\n");
                printf("%d",c);
                } while(c != '^');
            }
            if (n == '1') comport();
            if (n == '2') win();
        } while (c != EOF);
        fclose (pFiletwo);
    }
}

void win(void)
{
    printf("NOT AVALIBLE ");
    comport();
}


:The program will not go to any other void but what was instructed without input! edit: added source tags -SiCrane [Edited by - SiCrane on July 21, 2005 10:35:42 PM]
Advertisement
getchar() does just what it says. It retreives a single character from the input stream. That means that a command like 'a' or '9' would work but not one that is more than one letter long. I dont know what you should use in C, I use C++ myself (in which case you would use std::getline(cin, command))
the first thing that jumps out to me...

com = getchar();
if (com == '32') win();
...

getchar() returns a single character
the following if statements should compare to a single character
eg. '3' or 'v', 'h', 'w' etc.
// From google...
http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/io.html

char *fgets(char *s, int size, FILE *stream)

eg..
#include <stdio.h>#include <stdlib.h>int main(int argc, char*argv[]){    char buffer[1024];    if(fgets(buffer, 1024, stdin) != NULL)    {        if(strcmp(buffer,"hello")==0)        {            printf("HELLO\n");        }        else if(strcmp(buffer,"world")==0)        {            printf("WORLD\n");        }        else if(strcmp(buffer,"!")==0)        {            printf("!!!\n");        }    }}


--Steven Ashley
1) Use [source][/source] tags to post large blocks of code such as yours (or [code][/code] tags for smaller blocks).

2) It looks as though you wish to write some kind of (emulator for an) operating system; this is a poor choice of project to begin with, trust me.

3) Be aware of the difference between single and double quotes, and the concept of a character of text vs. a text string. Single quotes expect a single character between them, and specify a constant of char type - which is also what getchar() reads - a single character. A string is properly specified inside double quotes. This produces a pointer value, pointing at a null-terminated string that gets "built into" the executable. To compare items of text data in C, you will need to read a whole line of text into a buffer, and make use of strcmp() or similar functions. Directly comparing the pointers will not work; that is comparing "are these in the same place in memory", not "do these have the same value".

4) Become familiar with loops. Your current structure uses recursion to come back to the main loop, which may gradually erode the stack and is needlessly complicated (and is a poor fit for the C programming paradigm). Additionally, the recursive calls within the sub-functions are unnecessary; functions implicitly "return" after they are done, so that after (for example) executing VERSION(), the code would return to do the rest of comport(), do the other checks (with the same input, so they'd all pass by harmlessly), and then reach comport()'s own recursive call. But again, get rid of all of that, and just use a while loop.

5) Don't waste effort to prototype functions if it's not needed; first try to arrange functions such that called functions come before their callers. If there is a cycle, *then* a prototype will be required. But otherwise, you are just adding extra typing work for yourself. :)

6) I can't begin to guess how you want the user to work with your file-opening functions.
That is why it printed
ZUF 1.0
the same amount of times as the letters in the com

how would I use IF with SCANF("%s",com); it did not work so well for me the first time I tried.
Quote:Original post by Zahlman
To compare items of text data in C, you will need to read a whole line of text into a buffer, and make use of strcmp() or similar functions. Directly comparing the pointers will not work; that is comparing "are these in the same place in memory", not "do these have the same value".


As I said.
Is this a good mouse driver for C?

int x = 0, y = 0;static volatile int button[3] = {0, 0, 0};unsigned char data[3];void ps2_mouse(){    data[0] = inport60();    data[1] = inport60();    data[2] = inport60();    if ((data[0] & 0x01) != button[0])    {        button[0] ^= 1;        if (button[0]) left_button_down(x, y);        else left_button_up(x, y);    }    if ((data[0] & 0x04) != button[1])    {        button[1] ^= 1;        if (button[1]) middle_button_down(x, y);        else middle_button_up(x, y);    }    if ((data[0] & 0x02) != button[2])    {        button[2] ^= 1;        if (button[2]) right_button_down(x, y);        else right_button_up(x, y);    }    if (data[0] & 0x10)        x += (int)((256 - data[1]) * -1);    else        x += (int)data[1];    if (data[0] & 0x20)        y += (int) (256 - data[2]);    else        y += (int)(data[2] * -1);    if (y > 184) y = 184;    else if (y < 0) y = 0;    if (x > 311) x = 311;    else if (x < 0) x = 0;}void init_ps2_mouse(){    int x;    unsigned char data_read;    for(x = 0; x < 5; x++)    {        outport64(0xA7);                    //reset every thing        outport64(0xA8);        outport64(0xD4);        outport60(0xF5);        data_read = inport60();            //    did it         if (data_read != 0xFA) continue;    //    work ?        outport64(0xD4);        outport60(0xFF);        data_read = inport60();            //    did it        if (data_read != 0xFA) continue;    //    work ?        data_read = inport60();            //    did the self-        if (data_read != 0xAA) continue;    //    test work ?        mouse_type = inport60();        outport64(0xD4);        outport60(0xE6);        data_read = inport60();            //    did it        if (data_read != 0xFA) continue;    //    work ?/*        outport64(0xD4);        outport60(0xF3);        data_read = inport60();            //    did it        if (data_read != 0xFA) continue;    //    work ?        outport60(210);        data_read = inport60();            //    did it        if (data_read != 0xFA) continue;    //    work ?*/        outport64(0x20);                            data_read = inport60();            //        data_read |= 0x02;                  //         outport64(0x60);                    //    get it to report data        outport60(data_read);              //        outport64(0xD4);                    //        outport60(0xF4);        data_read = inport60();            //    did it        if (data_read != 0xFA) continue;    //    work ?        break;    }}void left_button_down(int x, int y){}void left_button_up(int x, int y){}void right_button_down(int x, int y){    reboot();}void right_button_up(int x, int y){}void middle_button_down(int x, int y){}void middle_button_up(int x, int y){}


If not I need one.

[Edited by - BASIC freak on July 22, 2005 12:19:52 PM]
If you need to ask, you probably shouldn't be writing a mouse-driver...

It looks like you're trying to use DOS and/or BIOS functions to talk to the mouse hardware. Do NOT do this. Use the API of whatever platform you're writing for; Windows, OSX, whatever. If you want something cross-platform, use some toolkit like SDL or GTK, depending on what you want to do. It would help if you said what your goal was.

Regardless... it looks like you're making it so you have to poll the main function, then decide what to do depending on what variables are set. A better way might be to use callbacks, so you can just tell a function to be called whenever the mousebutton is pressed. You'd still have to poll the driver, but the results would happen automatically instead of forcing the user to check the variables. Of course, that's without getting into either threads or hardware interrupts...
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192

This topic is closed to new replies.

Advertisement