Thread: weird

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    weird

    this is weird

    when i go into the 3rd menu and enter in the aperture value it spits something out really weird. like i enter in f1.2 and instead of spitting out "f1.2" it says "é159.187317"

    does neone know wats going on with this and how i could fix it?

    Code:
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    int vname=0, viso=0, vaperture=0, error=0;
    
    float iso, apertureno;
    
    char selection[2], name[20], aperture[1];
    
    while (vname + viso + vaperture != 3 &&  strcmp(selection,"Q") != 0)
            {
            fprintf(output,"\n1. First name\n2. ISO setting\n3. Aperture setting\n\n"
            "Please enter a menu number or '?' for help\n");
            fscanf(input,"%s[2]",selection);
            if (strcmp(selection,"1")==0)
                    {
                    fprintf(output,"\nPlease enter your first name\n");
                    fscanf(input,"%s[20]",name);
                    error=1;
                    vname=1;
                    }
            else if (strcmp(selection,"2")==0)
                    {
                    fprintf(output,"\nAcceptable ISO settings include:\n\n6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64, 80, 100,"
                    " 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250, 1600, 2000, 2500,3200, 4000, 5000 and 6400.\n\n"
                    "Please enter your ISO setting\n");
                    fscanf(input,"%f",&iso);
                    if (iso!=6.0 && iso!=8.0 && iso!=10.0 && iso!=12.0 && iso!=16.0 && iso!=20.0 && iso!=25.0 && iso!=32.0 &&
                    iso!=40.0 && iso!=50.0 && iso!=64.0 && iso!=80.0 && iso!=100.0 && iso!=125.0 && iso!=160.0 && iso!=200.0 &&
                    iso!=250.0 && iso!=320.0 && iso!=400.0 && iso!=500.0 && iso!=640.0 && iso!=800.0 && iso!=1000.0 &&
                    iso!=1250.0 && iso!=1600.0 && iso!=2000.0 && iso!=2500.0 && iso!=3200.0 && iso!=4000.0 && iso!=5000.0 &&
                    iso!=6400.0)
                            {
    if (error==1)
                                    {
                                    fprintf(output,"\n%s, the ISO setting you entered was not valid\n",name);
                                    }
                            else
                                    {
                                    fprintf(output,"\nThe ISO setting you entered was not valid\n");
                                    }
                            }
                    else
                            {
                            viso=1;
                            }
                    }
            else if (strcmp(selection,"3")==0)
                    {
                    fprintf(output,"\nAcceptable camera aperture values include:\n\nf1.2, f1.4, f1.8, f2, f2.8, f4, f5.6, f8,"
                    " f11, f16, f22, f32\n\nPlease enter you camera aperture\n");
                    fscanf(input,"%c%f",&aperture,&apertureno);
                    fprintf(output,"%c%f",aperture,apertureno);
                    }
            }
    }
    sorry bout the inconsistent indenting that starts halfway through

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    I think problem is you are using
    Code:
    char aperture[1]
    If you want to have a single character as a string then I think it needs to be
    Code:
    char aperture[2]
    Something to do with null character at the end
    Last edited by ICool; 09-23-2007 at 11:20 PM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    that didnt fix it

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    You may want to do something like this to get rid of the ugly iso if:
    Code:
    float valid_iso_table[ ] = 
    { 
        6.0, 8.0, 10.0, 12.0, 16.0, 20.0, 25.0, 32.0, 40.0,
        50.0, 64.0, 80.0, 100.0, 125.0, 160.0, 200.0,
        250.0, 320.0, 400.0, 500.0, 640.0, 800.0, 1000.0,
        1250.0, 1600.0, 2000.0, 2500.0, 3200.0, 4000.0, 5000.0, 6400.0
    };
    
    bool IsValidIso(float iso)
    {
        for(int i = 0; i < 31; i++)
        {
            if(valid_iso_table[i] == iso)
                return true;
        }
        return false;
    }
    Seems much cleaner to me.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fscanf(input,"&#37;s[2]",selection);
    What's this supposed to be?

    "%1s" would be the correct way to say this to prevent buffer overflow.

    Also, your use of the 1-character array and resulting attempt to scan into it is also wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    ok i changed it to &#37;1s and i got rid of the 1 character array.. just set it as char aperture
    I think its not storing either the float nor the char properly because i initialized the float to 1.0 and now instead of printing out &#233;159.187317 its just printing out 1.0..

    how come it doesnt seem to be storing it properly??

    neone??

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post the code, and the transcript of what you typed.

    > fscanf(input,"&#37;f",&iso);
    Quite possibly, fscanf() returned 0, which means it failed to perform any conversion at all, and thus left the variable in it's current state.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Code:
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    int vname=0, viso=0, vaperture=0, error=0;
    
    float iso, apertureno=1.0;
    
    char selection[2], name[20], aperture='f';
    
    while (vname + viso + vaperture != 3 &&  strcmp(selection,"Q") != 0)
            {
            fprintf(output,"\n1. First name\n2. ISO setting\n3. Aperture setting\n\n"
            "Please enter a menu number or '?' for help\n");
            fscanf(input,"&#37;s",selection);
            if (strcmp(selection,"1")==0)
                    {
                    fprintf(output,"\nPlease enter your first name\n");
                    fscanf(input,"%s",name);
                    error=1;
                    vname=1;
                    }
            else if (strcmp(selection,"2")==0)
                    {
                    fprintf(output,"\nAcceptable ISO settings include:\n\n6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64, 80, 100,"
                    " 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250, 1600, 2000, 2500,3200, 4000, 5000 and 6400.\n\n"
                    "Please enter your ISO setting\n");
                    fscanf(input,"%f",&iso);
                    if (iso!=6.0 && iso!=8.0 && iso!=10.0 && iso!=12.0 && iso!=16.0 && iso!=20.0 && iso!=25.0 && iso!=32.0 &&
                    iso!=40.0 && iso!=50.0 && iso!=64.0 && iso!=80.0 && iso!=100.0 && iso!=125.0 && iso!=160.0 && iso!=200.0 &&
                    iso!=250.0 && iso!=320.0 && iso!=400.0 && iso!=500.0 && iso!=640.0 && iso!=800.0 && iso!=1000.0 &&
                    iso!=1250.0 && iso!=1600.0 && iso!=2000.0 && iso!=2500.0 && iso!=3200.0 && iso!=4000.0 && iso!=5000.0 &&
                    iso!=6400.0)
                            {
                            if (error==1)
                                    {
                                    fprintf(output,"\n%s, the ISO setting you entered was not valid\n",name);
                                    }
                            else
                                    {
                                    fprintf(output,"\nThe ISO setting you entered was not valid\n");
                                    }
                            }
                    else
                            {
                            viso=1;
                            }
                    }
            else if (strcmp(selection,"3")==0)
                    {
                    fprintf(output,"\nAcceptable camera aperture values include:\n\nf1.2, f1.4, f1.8, f2, f2.8, f4, f5.6, f8,"
                    " f11, f16, f22, f32\n\nPlease enter you camera aperture\n");
                    fscanf(input,"%c%f", &aperture, &apertureno);
                    fprintf(output,"%c%f", aperture, apertureno);
                    }
            }
    }
    and this is the part where it doesnt seem to be storing either the float or the character because if i type in f1.2 at the input it just reads back 1.000000

    Code:
                    fscanf(input,"%c%f", &aperture, &apertureno);
                    fprintf(output,"%c%f", aperture, apertureno);

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well &#37;c is that awkward exception to the rule.
    Every other format skips leading white space / tabs / newlines. %c doesn't.

    But had you been checking return results, you would have seen that happening.

    I suppose the easy answer is in the FAQ, the one about not using fflush(stdin) and what the practical alternatives are.

    Long term, avoid fscanf and scanf functions, and learn to read ALL input with fgets(), then parse the resulting buffer with sscanf(), or other functions of your choice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 11:23 PM
  2. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  3. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM
  4. Weird
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-24-2001, 08:03 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM