Thread: if else statements

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    Question if else statements

    CODE:

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define fname "a.f"

    main()
    {
    FILE *fp;
    int x,len=0;
    char str[100],file[20];

    printf("Enter file name: ");
    gets(file);

    if (file == fname)
    {
    fp=fopen(fname,"w");
    printf("file opened!\n\n");
    printf("\nEnter the sentence to count:\n");
    scanf("%s",&str);
    len=strlen(str);
    printf("lenght: %d",len);
    for (x=0;x<=len;x++)
    {
    fputc(str[x], fp);
    }
    fclose(fp);
    printf("\nString Entered: %s",str);

    printf("File Was Successfully Created!");

    }
    else
    {
    printf("Invalid filename!");
    }

    getchar();
    getchar();

    }

    ok , so i want the user to input the filename and if he/she enters it "a.f" it will create the file else it will exit program.

    my problem is that it doesnt see if statement but runs else statement when i enter filename.

    wat am i doing wrong!


  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >wat am i doing wrong!
    You're using == to compare strings. This is notorious for not working, you need to use strcmp.

    -Prelude
    My best code is written with the delete key.

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    is it
    strcmp (fname, "a.f");

    ???

  4. #4
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    if (file == fname)

    ought to be:

    if (strcmp(file, filename) == 0)

    ....................
    strcmp(str1,str2) returns
    <0 if str1 <str2
    0 if str1=str2
    >0 if str1>str2
    Steve

  5. #5
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    so basically u said:
    if (strcmp(gets, fname) == 0)
    {
    //here my code
    }
    else
    {
    //code
    }


    if yes it doesnt work, it says illegal conversion

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    try defining fname without #define. maybe something like this...

    >const char fname[5] = "a.f";

    You maybe getting the error because it doesn't realize that fname is the char[] type.

  7. #7
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    thx it worked
    but it doesnt count everything
    for ex: i put
    adas af

    it will printout that it has 4 characters(ie adas) it is unable to detect af
    y ?
    Last edited by dayknight; 05-02-2002 at 07:51 PM.

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    shouldn't the define constant be:
    Code:
    #define FNAME "a.f"
    ?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    it works both ways.
    it was my bad
    i did
    if (strcmp(gets, fname) == 0)
    gets << lol instead of >> file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 08:18 AM