Fix C++ Compiling Error - Need Urgent Homework Help | CreditAccount

In summary, the conversation is about a person who is having trouble compiling their program for an assignment and is seeking help from others. They have written the entire program but are getting an error message about expected primary-expression before ']' token. The conversation also includes code for a CreditAccount class and methods for changing account information such as name, expiration date, and credit limit. The expert summarizer points out that the compiler is probably giving information about incorrect usage of arrays and suggests looking at examples in a textbook.
  • #1
Joe_K
33
0

Homework Statement



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!

Homework Equations





The Attempt at a Solution



Here is my code:

Code:
#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;
}
 
Physics news on Phys.org
  • #2
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:
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
}
 

1. What is a compiling error in C++?

A compiling error in C++ is an error that occurs during the process of converting human-readable code into machine-readable code. It can be caused by syntax errors, logical errors, or missing components in the code.

2. Why do I need urgent help with a compiling error in C++?

Compiling errors can prevent your code from running properly, and if left unresolved, can lead to further errors down the line. It is important to address them as soon as possible to ensure the proper functioning and efficiency of your code.

3. How do I troubleshoot a compiling error in C++?

To troubleshoot a compiling error in C++, start by carefully reading the error message to identify the specific line and type of error. Then, check for any syntax errors, missing components, or logical errors in that line and the surrounding code. If needed, consult online resources or seek help from experienced programmers.

4. What are some common causes of compiling errors in C++?

Some common causes of compiling errors in C++ include incorrect syntax, missing or misplaced semicolons, mismatched data types, and logical errors such as infinite loops or uninitialized variables. It is also important to ensure that all necessary libraries and header files are included.

5. Can I prevent compiling errors in C++?

While it is not always possible to prevent all compiling errors, there are steps you can take to reduce the likelihood of encountering them. These include writing clean and organized code, testing your code frequently, and consulting resources and experienced programmers for guidance and troubleshooting.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
10
Views
960
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top