What's wrong with my coding?

This is a discussion on What's wrong with my coding? within the C Programming forums, part of the General Programming Boards category; If I entered "run.exe x.txt" in the following code, y does the if statement is always true? Code: int main(int ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    21

    What's wrong with my coding?

    If I entered "run.exe x.txt" in the following code, y does the if statement is always true?

    Code:
    int main(int argc, char *argv[])
    {
     if(argv[1]!="x.txt")
     {
      printf("\nPlease enter run x.txt!!!\n");
      exit(1);
     }
    In the following code, wat's wrong that causes it to loop infinitely when I entered a character instead of a integer?

    Code:
    int choice;
    
     while(choice!=2)
     {
      printf("\n1?");
      printf("\n2?");
    
      printf("\n\nChoice: ");
      scanf("%d", &choice);
    
      switch(choice)
      {
       case 1: 1(); break;
       case 2: break;
       default: printf("\nPlease enter 1 or 2 only!!!\n"); break;
      }
     }
    Pls enlighten a newbie, thx a million....
    Last edited by EeeK; 10-06-2003 at 06:17 PM.

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    I'm not sure about #1, I'm kinda new myself. #2 is answered in the faq though, right here
    -gunder

    if (problem)
    postcount++;

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    Code:
    while (cout << "Enter a number: " && !(cin >> input))
      {
        cout << "\nInvalid input" <<endl;
        cin.clear();
        cin.ignore(std::numeric_limits < int >::max(), '\n');
      }
      
      cout <<"You entered " <<input;
    Went to the FAQ and it's in C++, what is the equivalent functions in C for clear() and ignore(std::numeric_limits <int>::max())?

    Is it fflush(stdin) for clear()?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >If I entered "run.exe x.txt" in the following code, y does the if statement is always true?

    >if(argv[1]!="x.txt")

    Because this does not do a string comparison. Use strcmp.
    Code:
    if(strcmp(argv[1],"x.txt")!=0)
    >Is it fflush(stdin) for clear()?

    No. Try this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    Y does I get a passing arg 2 of 'fputs' from incompatible pointer type error at the fputs() line?

    Code:
    define FILENAME "x.txt"
    .
    .
    .
    fp = fopen(FILENAME, "a");
     if(fp==(FILE*) NULL)
     {
      printf("\nError opening %s!!!\n", FILENAME);
      exit(1);
     }
     fputs("TEST", FILENAME);
     fclose(fp);

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    fputs() expects a FILE * as its second argument, so use fputs("TEST", fp) instead.
    $ENV: FreeBSD, gcc, emacs

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    Yup, found out abt that but u post b4 I want to delete the post. Nonetheless thx for the reply.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    What is the difference btn these 3:
    Code:
    char* text;
    char *text;
    char text[99];
    The second is a char pointer and third is an array, so what is first? Can I use all of them for fgets() or only the third can be use on fgets() as fgets(text, 99, stdin)?

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    First and second are examples of the different styles of using '*' operator, but are otherwise equivalent.
    $ENV: FreeBSD, gcc, emacs

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    So they are basically the same. So can I use the first and the second for fgets()? Because the middle arguments of fgets() is the buffer size. which is not define if I'm using the first and second.

    If not what are the ways in accepting a string input?

    Code:
    ...
    printf("Enter your name:");
    ...

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >So they are basically the same.

    I'd better point here again.

    >So can I use the first and the second for fgets()?

    Not unless you allocate memory to where these pointers point.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       char automatic[10];
       char *dynamic;
       size_t size = 42;
    
       puts("Enter 'automatic' string:");
       fflush(stdout);
       if(fgets(automatic, sizeof automatic, stdin) != NULL)
       {
          printf("automatic: %s\n", automatic);
       }
    
       dynamic = malloc(size);
       if(dynamic != NULL)
       {
          puts("Enter 'dynamic' string:");
          fflush(stdout);
          if(fgets(dynamic, size, stdin) != NULL)
          {
             printf("dynamic: %s\n", dynamic);
          }
          free(dynamic);
       }
    
       return 0;
    }
    
    /* my output
    Enter 'automatic' string:
    All work and no play makes Jack a dull boy.
    automatic: All work 
    Enter 'dynamic' string:
    dynamic: and no play makes Jack a dull boy.
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    21
    Ok what is the difference btn
    Code:
    char *x;
    x = (char*)malloc(sizeof(char)*9);
    and
    Code:
    char x[9];

  13. #13
    Registered User Moni's Avatar
    Join Date
    Oct 2002
    Location
    Dhaka, Bangladesh.
    Posts
    104
    As far as I know

    using malloc dynamically allocate memory...while running
    and arrays set aside fixed sized memory before using it....
    We all are the components of a huge program...... the programmer is always debugging us with His debugger.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    A C compiler completely ignores most whitespaces and linebreaks. i.e. it doesn't make any difference if you write

    Code:
    char* text;
    
    or
    
    char *text;
    
    or
    
    char*text;
    
    or
    
    char
    
    *
    
       text
    
    ;

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Moni
    As far as I know

    using malloc dynamically allocate memory...while running
    and arrays set aside fixed sized memory before using it....
    Arrays also allocate memory while running. And malloc is beeing used to set aside a fixed size memory before using it.

    The difference is that for arrays this allocation is done automatically with malloc you do it manually. Also the size of an array can not be changed. You can not assign another piece of memory to an array variable as you can do with a pointer variable.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  2. coding
    By rick1984 in forum C Programming
    Replies: 8
    Last Post: 07-29-2003, 04:23 AM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  4. coding done. Two minor bugs. where did I go wrong?
    By mastering_c in forum C Programming
    Replies: 2
    Last Post: 06-25-2002, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21