Thread: Logic flaws

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Logic flaws

    Hello,

    I have been learning c for about two weeks. I have completed the tutorials on this website as well as spending more time in the MSDN library than what should be needed in the early stages. My approach to learning has pretty much been to dive in, write a project and address issues as they come. Formalities aside, I've run into a few logical issues in my attempt at writing a project. You will note that I am taking a ridiculous approach in writing a simple project. Everything is being done to solidify understanding of basic concepts ( Using multiple source files , file I/O, etc. ) Ok, beginning with the main() function.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
     void Deposit();  //From Deposit.c
     void Withdraw(); //From Withdraw.c
            
    
    int main(int argc, char *argv[])
    
    {
        char w = 'w';
        char d = 'd';
        char q = 'q';
        
    /* If there are not at least two aurguments , print usage. Not exactly the best error checking out there. */
      
      if ( argc != 2){   
      printf ( " Usage:  ATM Machine [w] [d] [q]  \n " );
      printf ( " w = Withdraw funds \n " );
      printf ( " d = Deposit funds \n " );                       
      printf ( " q = Get account balance \n " );
      system("PAUSE");	
      return 0;
    }
    
    /* And here's the fun part. Use some IF statements to test the value of second aurgument in main() and act accordingly. Herein lies the problem */
    
      if ( argv[2] = 'd' )  // Line 28
      Deposit();
    
      else if ( argv[2] = 'w' )
      Withdraw();
    
      else 
      printf ( "Not a valid character" );
      return 0;
    
    }

    I understand there are probably quite a few issues in main() beyond logical ones, but such is the price of being a beginner at pretty much anything. Yes, I intend to clean it up. Now for the next two files which make up the rest of the program.

    Starting with the future Deposit() function.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void Deposit()
    
     {
     
     int i;
     /* Standard file I/O , I am simply testing the concept which I will end up modifying to accept user input and drop it into the Account.txt file */
     FILE *acct;
     acct = fopen ( "Account.txt" , "w+" );
     
     /* Printing my silly test string to ensure I have the slightest grasp of file I/O before trying to do anything wreckless. */
     fprintf ( acct , "Testing this" );
     fclose ( acct );  
     
     /* Opening back up to read from it , printing each character until the end of file is reached. Not a future part of Deposit(), but once again just a concept test */
     acct = fopen ( "Account.txt" , "r" );
     printf ( "\n Your new balance is: " );
     
     while ( (i = fgetc (acct) ) != EOF )
     printf ( "%c" , i );
     fclose ( acct );
     
    
    }

    And the future Withdraw() function, which failed my QA and caused me to finally bite the bullet and get a second opinion.


    Code:
    #include <stdio.h>
    
    /* At some point this will hold the withdrawal function, I simply use a printf() to test my IF statement, which obviously fails. */
    
    void Withdraw()
    
    {
         printf ( "We have a winner\n" );
         
    }

    Some might say " Read tutorials, MSDN, Google , etc. "
    Well I am doing that, but I want to actually understand what I am doing, which tutorials and Google help with but in practice it is not translating. If possible I would like to avoid " Fix - it " code. What I am asking for is where my logic is off and what I can look into to fix it myself. For example , in my IF statements I am trying to test for a single character , and if the user inputs one of the allowed characters, it will execute the code located under the IF statement ( Which is located in different source files, part of my attempt at practical application ) What ends up happening is I enter any character as a switch, and it executes the deposit function. If I enter no aurguments, it prints my usage which is expected. Despite what I thought to be proper checking for a particular character, it is just taking whatever character is thrown at it and executing the first IF statement without checking to ensure it is an expected character. This is obviously not letting me finish writing the other functions since my program cannot differentiate between characters, so I cannot call unique functions based on input. It looks right but is wrong. I know it is incomplete, basic , and most likely a simple oversight. Where am I going wrong, and what should I be doing more of to work past the trivial issues that I am bound to run into with this project? I appreciate input and apologize for the lengthy monologue. If there are any " wtf are you doing noob, thats not how to do it " type questions, I will be glad to explain my logic as I thought out every line to the best of my understanding before writing it. Thanks in advance guys, I look forward to one day being a contributor.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I have completed the tutorials on this website
    O_o

    Code:
    if ( argv[2] = 'd' )
    Lies!

    Soma

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Quote Originally Posted by phantomotap View Post
    O_o



    Lies!

    Soma
    Thank you for the input. If you look at the c tutorial, there was not alot of information regarding creating and properly checking for command line args. The 'd' part was a typo as I already declared char d = 'd' so I should have just wrote d as the argv[] aurgument. Although that still doesn't help. I made d = 'd' since I assumed that 'd' would be a character constant and checking for that as the value put into argv[] would be the correct way to write a command line switch and check for it. ( I have been piecing this together with assumptions based on logic as I understood it.) So your input tells me that I have done it the wrong way, which I figured or I would not have posted. Better to write crappy code and ask for help then to copy/paste complex code with 10,000 bugs and expect valuable input. I will re read the command line input tutorial to see what I have missed.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Maggot View Post
    I will re read the command line input tutorial to see what I have missed.
    In case you don't see it along the way:

    = is to assign a value.
    == is to compare values.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    I thought about that as well. My first approach was to write

    Code:
    if (argv[2] == d)
    // function
    Because it made logical sense. To me that would mean that if the value of argv[] was "equal" to d then it would carry out the function contained in the if statement. Instead it goes down to my else statement, printing the " Not a valid character" string. When I used the "=" sign in place of it, it executed Deposit(). It seemed wierd, since I am essentially "Assigning" the user input to argv[] and executing the function, rather than comparing the value entered to what should be inputted to execute the function. But at least it executed the function. This is where I knew I was missing something, and why I was asking for help. I am trying to understand the "why" to my malfunctioning code as opposed to the correct way to write it. My understanding is, argv[] will take input, and my IF statement would check to see that the input is d, which I declared earlier ( incorrectly I assume ). If the value put into argv[] is d then it should , by what I understood, accept the input and execute the function within that if statement. If it was anything else, it should move along to the next if statement. I get that I am making an idiodic mistake, but that is what I consider to be the best method of actually understanding something. I am still working on it and I do appreciate the feedback I have been given thus far.

    Thanks

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    argv is an array of arrays of characters. Therefore, argv[2] is a character array which, because arrays are indexed starting from 0, is the second command line argument to the program. In other words, if you ran a program with the arguments myprogram.exe hello world, the resulting argv array would be

    Code:
    argv[0] -> myprogram.exe (the program name is always the first element in the array)
    argv[1] -> hello
    argv[2] -> world
    Now, if you're only looking for a single character in your argument, you need to add another [0] to argv[2], which says "go to argv[2] and get the first character in the string (character array)". So you'd have

    Code:
    argv[1][0] -> 'h'
    argv[1][3] -> 'l'
    argv[2][0] -> 'w'
    argv[2][4] -> 'd'

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Maggot View Post
    Hello,
    I have been learning c for about two weeks. I have completed the tutorials on this website as well as spending more time in the MSDN library than what should be needed in the early stages.

    Some might say " Read tutorials, MSDN, Google , etc. " Well I am doing that, but I want to actually understand what I am doing, which tutorials and Google help with but in practice it is not translating.
    Of course it's not translating ... or sinking in.

    First of all MSDN is for windows... While a lot of windows stuff is in C windows is NOT C. It's a program written in C and MSDN is it's documentation. If there is anything that's going to frustrate a beginning programmer... that's it.

    Second, merely reading tutorials isn't going to get the job done. What you need to do is sit down with a recently published book or e-book on C and go through it page by page, actually type up the exercises, compile them, play with the code, make changes then, once you understand the exercise... move on to page 2...

    If you are are like most people your reading retention is someplace in the area of 60% meaning that 40% of what you read is simply gone in a few hours... Unfortunately that's not going to work for programming. Typing up the exercises, playing with them in an up-to-date compiler, making mistakes and fixing them is how you actually learn programming.

    Believe me... reading a couple of pages of a quicky tutorial over your cornflakes at breakfast isn't how you learn programming.

    Also don't make the mistake of thinking you have to have all this information wedged between your ears... It is far more important that you develop problem solving skills to figure things out as you go and build a library of information so you can look stuff up when you need it. Of particular importance here is full documentation of whatever compiler you use, it's libraries, and the language itself.

    I've been working in C since mid 2004 and even today I still look stuff up all the time. It's not uncommon for me to have multiple help files open, a couple of browser windows and perhaps a tutorial onscreen... even on relatively simple projects.

    It's all about memory leakage... of the human kind.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As the previous posters have already said I think the source of your confusion is that you are not differentiating between learning a language and learning how to use that language to develop on a particular operating system. So stop studying code libraries and APIs and start understanding language concepts. This site has plenty of resources to get you started and sticking around reading forum threads is a good way to improve. Heck, you learn new stuff here everyday even if you are an experienced programmer.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with logic
    By happyclown in forum C Programming
    Replies: 5
    Last Post: 02-11-2009, 05:12 AM
  2. Need a logic!
    By ssharish2005 in forum Tech Board
    Replies: 2
    Last Post: 01-31-2008, 10:56 AM
  3. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  4. Logic???
    By planet_abhi in forum Game Programming
    Replies: 1
    Last Post: 08-08-2003, 07:59 AM
  5. Pcx? What's the logic????
    By kaygee in forum C++ Programming
    Replies: 1
    Last Post: 01-14-2002, 10:18 AM