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.