CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2008
    Posts
    9

    finalized assignment - does this look right?

    Hi,

    I'm new to C++ and wrote the following program for an assignment where we were supposed to put all of our code in functions and then call each one to complete a specific ATM-related task. Briefly, the program asks the user to select from 3 accounts (checking, savings, and treasury), reads the balance from a pre-written text file, shows the balance, gets the withdrawal amount, dispenses the money in hundreds with a remainder amount, shows the new balance, and writes the new balance to the text file. Each of these tasks has a corresponding function. As far as I can tell it looks good and runs properly in Visual Studio, but I was hoping that this could be confirmed. If there are any errors and you are kind enough to write me back, please just point them out, I'm not looking for answers or rewritten code.

    Thanks for your time---cordelia


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    char getAccount(char)
    {
    	do
    	{
    		system("cls");
    		cout << "Greetings, Mr. Paulson." << endl << endl;
    
    		cout << "Checking"    << endl;
    		cout << "Savings"     << endl;
    		cout << "US Treasury" << endl;
    
    		cout << endl;
    		cout << "From which of the above accounts would you like to make a withdrawal (c/s/t)? ";
    		cin >> account;
    		cout << endl;
    	}while(toupper(account) != 'C' && toupper(account) != 'S' && toupper(account) != 'T');
    }
    
    long long getBalance(long long)
    {	
    	char getAccount(char);
    	{	
    		ifstream inputFile;
    		switch(account)
    		{
    		case 'c':
    		case 'C': inputFile.open("CheckingBalance.txt");
    				inputFile >> balance;
    				inputFile.close();
    				break;
    		case 's':
    		case 'S': inputFile.open("SavingsBalance.txt");
    			    inputFile >> balance;
    				inputFile.close();
    				break;
    		case 't':
    		case 'T': inputFile.open("TreasuryBalance.txt");
    				inputFile >> balance;
    				inputFile.close();
    				break;
    		}
    	}
    }
    
    void showBalance()
    {
    	long long getBalance(long long balance);
    	{
    		switch(account)
    		{
    		case 'c':
    		case 'C': cout << "You have $" << balance << " in your checking account." << endl;
    				break;
    		case 's':
    		case 'S': cout << "You have $" << balance << " in your savings account." << endl;
    				break;
    		case 't':
    		case 'T': cout << "You have $" << balance << " in your treasury account." << endl;
    				break;
    		}
    	}
    }
    
    long long getWithdrawal(long long withdrawal)
    {
    	long long getBalance(long long balance);
    	{
    		do
    		{
    			cout << endl;
    			cout << "How much would you like to withdraw? $";
    			cin >> withdrawal;
    		}while(withdrawal > balance);
    	}
    }
    
    void dispense()
    {
    	long long getWithdrawal(long long withdrawal);
    	{	
    		if(withdrawal > 0)
    		{
    			int hundreds = 0;
    			int remainder = 0;
    			cout << endl;
    			hundreds = withdrawal / 100;
    			remainder = withdrawal % 100;
    			for (int i = 1; i <= hundreds; i++)
    				cout << "$100 ";
    			if(remainder)
    				cout << "$" << remainder;
    				cout << endl;
    		}
    	}
    	balance = balance - withdrawal;
    }
    
    void writeBalance()
    {
    	ofstream outputFile;
    	switch(account)
    	{
    	case 'c':
    	case 'C': outputFile.open("CheckingBalance.txt");
    			  outputFile << balance;
    			  outputFile.close();
    			  break;
    	case 's':
    	case 'S': outputFile.open("SavingsBalance.txt");
    			  outputFile << balance;
    			  outputFile.close();
    			  break;
    	case 't':
    	case 'T': outputFile.open("TreasuryBalance.txt");
    			  outputFile << balance;
    			  outputFile.close();
    			  break;
    	}
    
    int main()
    {
    	char account = ' ';
    	long long balance = 0;
    	long long withdrawal = 0;
    
    	char getAccount(char account); 
    
    	long long readBalance(long long balance);
    
    	void showBalance();
    
    	long long getWithdrawal(long long withdrawal);
    
    	void showBalance();
    
    	void dispense();
    
    	void writeBalance();
    
    	cout << endl << endl;
    	system("pause");
    	return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: finalized assignment - does this look right?

    One thing:

    The toupper() function is defined in <cctype>, but you forgot to include it.
    Code:
    #include <cctype>
    Another thing: your functions do not need another set of braces. Let's look at one of them:
    Code:
    long long getBalance(long long)
    {	
    	char getAccount(char);
    That line of code declares the getAccount() function. It doesn't call the getAccount() function. I don't know what your intention was here, but if you expected that getAccount() would be called, then you're wrong -- nothing was called.

    Another problem in the getBalance() function is that first, you are not passing anything to is, so declaring it with (long long) as a parameter makes no sense. If the function takes no arguments, declare it with an empty argument list.

    Second, the getBalance() function is supposed to return a long long value, but you don't return anything. If the getBalance() function isn't supposed to return anything, the return type should be void.
    As far as I can tell it looks good and runs properly in Visual Studio,
    Given the problems with getBalance, you should have received a few warnings (if not errors) that you've declared a function that doesn't return a value.

    Not only that you should have seen that your program doesn't call any functions whatsoever. It does nothing -- look at the main() program:
    Code:
    char getAccount(char account); 
    
    	long long readBalance(long long balance);
    
    	void showBalance();
    
    	long long getWithdrawal(long long withdrawal);
    
    	void showBalance();
    None of these are function calls -- all they do is declare functions, not call them. You need to read up on how to call functions:
    Code:
    getAccount();   // this calls the function
    readBalance(100);  // this calls the readBalance function
    // etc.
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2008
    Posts
    9

    Re: finalized assignment - does this look right?

    Thanks for the kind advice. I realize how off-track I was....went back and did some work and although I haven't addressed all the issues yet, I don't know why I'm getting the following two errors:

    error C2601: 'main' : local function definitions are illegal
    end of file found before the left brace '{'

    Any advice as I continue to fix this program?

    Thanks again.
    PS - my Visual Studio seems to stop working at times so I wasn't getting compiler errors until I restarted it.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    char getAccount()
    {
    	char account = ' ';
    	do
    	{
    		system("cls");
    		cout << "Greetings, Mr. Paulson." << endl << endl;
    
    		cout << "Checking"    << endl;
    		cout << "Savings"     << endl;
    		cout << "US Treasury" << endl;
    
    		cout << endl;
    		cout << "From which of the above accounts would you like to make a withdrawal (c/s/t)? ";
    		cin >> account;
    		cout << endl;
    	}while(toupper(account) != 'C' && toupper(account) != 'S' && toupper(account) != 'T');
    	return account;
    }
    
    long long readBalance(char account)
    {	
    	//char account = ' ';
    	long long balance = 0;
    	//getAccount();
    	{	
    		ifstream inputFile;
    		switch(account)
    		{
    		case 'c':
    		case 'C': inputFile.open("CheckingBalance.txt");
    				inputFile >> balance;
    				inputFile.close();
    				break;
    		case 's':
    		case 'S': inputFile.open("SavingsBalance.txt");
    			    inputFile >> balance;
    				inputFile.close();
    				break;
    		case 't':
    		case 'T': inputFile.open("TreasuryBalance.txt");
    				inputFile >> balance;
    				inputFile.close();
    				break;
    		}
    	}
    	return balance;
    }
    
    void showBalance(char account, long long balance)
    {
    	//char account = ' ';
    	//long long balance = 0;
    	//readBalance();
    	{
    		switch(account)
    		{
    		case 'c':
    		case 'C': cout << "You have $" << balance << " in your checking account." << endl;
    				break;
    		case 's':
    		case 'S': cout << "You have $" << balance << " in your savings account." << endl;
    				break;
    		case 't':
    		case 'T': cout << "You have $" << balance << " in your treasury account." << endl;
    				break;
    		}
    	}
    }
    
    long long getWithdrawal(long long balance)
    {
    	//long long balance = 0;
    	long long withdrawal = 0;
    	//readBalance();
    	{
    		do
    		{
    			cout << endl;
    			cout << "How much would you like to withdraw? $";
    			cin >> withdrawal;
    		}while(withdrawal > balance);
    	}
    	return withdrawal;
    }
    
    void dispense(long long withdrawal)
    {
    	//getWithdrawal();
    	//long long balance = 0;
    	//long long withdrawal = 0;
    	{	
    		if(withdrawal > 0)
    		{
    			int hundreds = 0;
    			int remainder = 0;
    			cout << endl;
    			hundreds = withdrawal / 100;
    			remainder = withdrawal % 100;
    			for (int i = 1; i <= hundreds; i++)
    				cout << "$100 ";
    			if(remainder)
    				cout << "$" << remainder;
    				cout << endl;
    		}
    	}
    	//balance = balance - withdrawal;
    }
    
    void writeBalance(char account, long long balance)
    {
    	//char account = ' ';
    	//long long balance = 0;
    	//readBalance();
    	ofstream outputFile;
    	switch(account)
    	{
    	case 'c':
    	case 'C': outputFile.open("CheckingBalance.txt");
    			  outputFile << balance;
    			  outputFile.close();
    			  break;
    	case 's':
    	case 'S': outputFile.open("SavingsBalance.txt");
    			  outputFile << balance;
    			  outputFile.close();
    			  break;
    	case 't':
    	case 'T': outputFile.open("TreasuryBalance.txt");
    			  outputFile << balance;
    			  outputFile.close();
    			  break;
    	}
    
    int main()
    {
    	//char account = ' ';
    	//long long balance = 0;
    	//long long withdrawal = 0;
    
    	account = getAccount(); 
    
    	balance = readBalance(account);
    
    	showBalance(account, balance);
    
    	withdrawal = getWithdrawal(balance);
    
    	showBalance(account, balance);
    
    	dispense(withdrawal);
    
    	balance = balance - withdrawal
    
    	writeBalance(account, balance);
    
    	cout << endl << endl;
    	system("pause");
    	return 0;
    }

  4. #4
    Join Date
    Nov 2008
    Posts
    9

    Re: finalized assignment - does this look right?

    Oh, and I did include #include <cctype> at the top. It is missing from what I just posted.

  5. #5
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: finalized assignment - does this look right?

    Code:
    void writeBalance()
    {
      ofstream outputFile;
      switch(account)
      {
      case 'c':
      case 'C': outputFile.open("CheckingBalance.txt");
            outputFile << balance;
            outputFile.close();
            break;
      case 's':
      case 'S': outputFile.open("SavingsBalance.txt");
            outputFile << balance;
            outputFile.close();
            break;
      case 't':
      case 'T': outputFile.open("TreasuryBalance.txt");
            outputFile << balance;
            outputFile.close();
            break;
      }
    }

    You need the last "}"


  6. #6
    Join Date
    Nov 2008
    Posts
    9

    Re: finalized assignment - does this look right?

    Thank you! Thank you!
    I think I solved all my problems with that code---seems to be running fine now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured