1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Urgent help, compiling error, Assignment due in 1 hour!

  1. Apr 25, 2012 #1
    1. The problem statement, all variables and given/known data

    Well I have written my entire program for my assignment, but I cannot get it to compile without multiple instances of the same error: "expected primary-expression before ']' token. Can anybody see a mistake I am making? I would greatly appreciate your help, thank you!

    2. Relevant equations



    3. The attempt at a solution

    Here is my code:

    Code (Text):

    #include <iostream>
    #include <iomanip>

    using namespace std;

    class CreditAccount
    {
    public:
        CreditAccount(char[],char[],char[],int,int,double,double);  //constructor
       
        void showAccount();
        void setAcctNum(char[]);
        void setName(char[], char[]);
        void setExpiration(int, int);
        void setLimit(double);
        void changeBalance(double); //method prototypes
       

    private:
        char accountNumber[20];
        char lastName[21];
        char firstName[21];
        int expMonth;
        int expYear;
        double creditLimit;
        double currentBal; // data members
    };
    CreditAccount::CreditAccount(char temp_a1[], char temp_a2[], char temp_a3[], int temp_i1, int temp_i2, double temp_d1, double temp_d2)
    {
        strcpy(accountNumber, temp_a1);
        strcpy(lastName, temp_a2);
        strcpy(firstName, temp_a3);
       
        expMonth = temp_i1;
        expYear  = temp_i2;
        creditLimit= temp_d1;
        currentBal = temp_d2;
    }

    //***********************************


    /*
    Method: showAccount
     
    Use: This method will display the information in a CreditAccount object via cout.
     
    Arguments: This method takes no arguments.
     
    Returns: Void, returns nothing.
     
     */
    void CreditAccount::showAccount()
    {
        cout<<fixed<<setprecision(2)
            <<"Account Number:  " << accountNumber <<endl<<endl
            <<"Expiration Date: " << expMonth <<"/"<<expYear<<endl<<endl
            <<"Name:            " << lastName <<","<<firstName<<endl<<endl
            <<"Credit Limit:    " <<"$"<< creditLimit<<endl<<endl
            <<"Current Balance: " <<"$"<< currentBal;
       
    }

    /*
     Method: setAcctNum
     
     Use: This function will change the account number. No error checking is needed.
     
     Arguments: This method takes a character array.
     
     Returns: Nothing
     
     */
    void CreditAccount::setAcctNum(char newAcctNum[])
    {
        accountNumber[] = newAcctNum[];
       
    }

    /*
     Method: setName
     
     Use: This function will change either the first name, last name, or both.  If the null string is passed for either argument, the corresponding data member will not be altered.
     
     Arguments: Takes two arguments, two character arrays for first and last name.
     
     Returns:  Void, returns nothing.
     
     */
    void CreditAccount::setName(char newFirst[], char newLast[])
    {
        if (newFirst[0] != '\0' && newLast[0] != '\0')
        {
           
            firstName = newFirst[];
            lastName = newLast[];
        }
       
        if (newFirst[0] != '\0')
        {
            lastName= newLast[];
           
        }
        if (newLast[0] != '\0')
        {
            firstName= newFirst[];
        }       //check for null terminator
    }

    /*
     Method: setExpiration
     
     Use: This function will change the expiration date for the account.
     
     Arguments: Takes two arguments, both are integers to store the data entered.  
     
     Returns: Nothing.
     
     */
    void CreditAccount::setExpiration(int newMonth, int newYear)
    {
        if (newMonth <1 && newMonth >12)
        {
            cout<<endl<<"Invalid entry.  The entered month value must be between 1 and 12.";
        }
        if (newYear <=2012)
        {
            cout<<"Invalid entry.  The enter year value must be greater than the year 2012.";
        }
        if (newMonth >0 && newMonth <13)
        {
            expMonth = newMonth;
        }
        if (newYear > 2012)
        {
            expYear = newYear;
        }  // check for validity
    }

    /*
     Method: setLimit
     
     Use:  This function will change the credit limit for the account.  
     
     Arguments: Takes a double, which is the value of the new credit limit.
     
     Returns: Nothing.
     
     */
    void CreditAccount::setLimit(double newCreditLimit)
    {
        if (newCreditLimit > 0.00)
        {
            creditLimit= newCreditLimit;
        }
        if (newCreditLimit <= 0.00)
        {
            creditLimit = 500.00;
            cout<<endl<<"The credit limit value must be greater than $0.00.  Please re-enter a valid value.";
        } //check for validity
    }

    /*
     Method: changeBalance
     
     Use: This function will update the balance for the account.
     
     Arguments: Takes a double, which stands for the change in account balance.
     
     Returns: Nothing, void.
     
     */

    void CreditAccount::changeBalance(double balanceChange)
    {
        currentBal = currentBal + balanceChange;
       
        if(currentBal<0)
        {
            cout<<endl<<"An over-payment was made.  A credit has been added to your account.";
        }
        if (currentBal > creditLimit)
        {
            cout<<"You have exceeded your credit limit.  A $25.00 over-the-limit fee has been applied to your balance.";
           
            currentBal= currentBal - 25;
        }  // apply fee to balance, and display message to user
       
    }


    int main()
    {

        CreditAccount Account1 = CreditAccount("1234-9876-2468-1357", "K", "Joe", 7, 2013, 1500.00, 1130.77);
        CreditAccount Account2 = CreditAccount("3453-1232-6445-0014", "TA Last name", "Ta First name", 14, 2010, -1.00, 1234.50);
       
        //call show account on Account1 object to display info
        Account1.showAccount();
       
        //call setName on Account1 object to change the name
        Account1.setName("Joe", "K");
       
        //call showAccount again to re-display with new name
        Account1.showAccount();
       
        //call setName again and change both names
        Account1.setName("J", "K");
       
        //call showAccount again to re-display with both new names
        Account1.showAccount();
       
        //call set AcctNum() to change the account number
        Account1.setAcctNum("3312-2342-6786-2342");
       
        //call showAccount again to show the changed data
        Account1.showAccount();
       
        //call setExpiration to change the date to another valid date
        Account1.setExpiration(9,2014);
       
        //call showAccount again to show the changed data
        Account1.showAccount();
       
        //call setExpiration to change the date to an invalid data
        Account1.setExpiration(4,2009);
       
        //call showAccount again to show the changed data
        Account1.showAccount();
       
        //call changeBalance 5 times to apply 3 purchases and 2 payments made on the account
        Account1.changeBalance(20.00);
        Account1.changeBalance(45.00);
        Account1.changeBalance(12.00);
        Account1.changeBalance(1700.00);
        Account1.changeBalance(1520.00);
       
        cout<<endl<<endl<<"Account 2: "<<endl;
        Account2.showAccount();
       

    cout << endl;
    system("pause");

    return 0;
    }


     
     
  2. jcsd
  3. Apr 25, 2012 #2

    Mark44

    Staff: Mentor

    I found four lines that the compiler is probably complaining about. The compiler probably is also giving you information about the line number where it's finding a problem. You are not using arrays correctly. For example, an expression such as newAcctNum[] is not allowed in a statement. (It is allowed in a declaration, such as where you declare the formal parameters of a function.)

    Take a look at some examples in your textbook on working with arrays.

    Code (Text):

    void CreditAccount::setAcctNum(char newAcctNum[])
    {
        [color="red"]accountNumber[] = newAcctNum[];[/color]
       
    }

    /*
     Method: setName
     
     Use: This function will change either the first name, last name, or both.  If the null string is passed for either argument, the corresponding data member will not be altered.
     
     Arguments: Takes two arguments, two character arrays for first and last name.
     
     Returns:  Void, returns nothing.
     
     */
    void CreditAccount::setName(char newFirst[], char newLast[])
    {
        if (newFirst[0] != '\0' && newLast[0] != '\0')
        {
            [color="red"]
            firstName = newFirst[];
            lastName = newLast[];[/color]
        }
       
        if (newFirst[0] != '\0')
        {
            [color="red"]lastName= newLast[];[/color]
           
        }
        if (newLast[0] != '\0')
        {
            [color="red"]firstName= newFirst[];[/color]
        }       //check for null terminator
    }

     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ Urgent help, compiling error, Assignment due in 1 hour!
  1. Urgent help (Replies: 1)

  2. Urgent help please? (Replies: 1)

Loading...