Dolbyover
Dolbyover

Reputation: 89

How to make an array print zeros when there is no input in that slot

The user inputs a double and string which gets stored into two arrays. Like so :

{
                 double DoC;
                 string Nm;
                 cout << " Please enter the amount charged to your credit card " << endl;
                 cin >> DoC;
                 cout << " Please enter where the charge was made " << endl;
                 cin >> Nm;
                 getline(cin, Nm);
                 cca.doCharge(Nm,DoC);
                 break;
                 }

Then the double and string are passed to this :

{
    if (amount > 0)
    {
        for (int i = 9; i != 0; i--)
        { 
            last10withdraws[i] = last10withdraws[i-1];
            last10charges[i] = last10charges[i-1];
        }

        last10withdraws[0] = amount;
        last10charges[0] = name;

        setBalanceW(amount);  
    }
    else
    {
        cout << " ERROR. Number must be greater then zero. " << endl;
    }
    return 0;
}

This seems to work very well for storing the data into the arrays. However I then use this function to display the data inside of the arrays :

{
    cout << " Account: Creditcard Withdraws " << " " << " Account: Creditcard Deposits " << " " << " Account: Creditcard last 10 charges " << endl;
    cout << "            " << last10withdraws[0] << "                       " << last10deposits[0] << "                       " << last10charges[0] << endl;
    cout << "            " << last10withdraws[1] << "                       " << last10deposits[1] << "                       " << last10charges[1] << endl;
    cout << "            " << last10withdraws[2] << "                       " << last10deposits[2] << "                       " << last10charges[2] << endl;
    cout << "            " << last10withdraws[3] << "                       " << last10deposits[3] << "                       " << last10charges[3] << endl;
    cout << "            " << last10withdraws[4] << "                       " << last10deposits[4] << "                       " << last10charges[4] << endl;
    cout << "            " << last10withdraws[5] << "                       " << last10deposits[5] << "                       " << last10charges[5] << endl;
    cout << "            " << last10withdraws[6] << "                       " << last10deposits[6] << "                       " << last10charges[6] << endl;
    cout << "            " << last10withdraws[7] << "                       " << last10deposits[7] << "                       " << last10charges[7] << endl;
    cout << "            " << last10withdraws[8] << "                       " << last10deposits[8] << "                       " << last10charges[8] << endl;
    cout << "            " << last10withdraws[9] << "                       " << last10deposits[9] << "                       " << last10charges[9] << endl;
    cout << endl;

}

Lets say the user has inputted three doubles into the deposit array. When I call the function to display it I get something like this :

60
30
20
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061

How can I make it so that the -9.25596e+061's are 0's? I have not been able to find anything really helping me. Also with the array that contains strings, when it is called to display it displays nothing. Why is this ?

Upvotes: 0

Views: 103

Answers (2)

Zack
Zack

Reputation: 2497

You are getting the error because you are not initializing the values. Start by saying int last10withdraws[10] = {0}; That will initialize all values to zero.

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361672

Initialize the array as:

int last10withdraws[10] = {0};

Now all elements are zero.

If the user enters three numbers, then first three elements will be non-zero (assuming only non-zero is allowed), and the rest will be zero.

If last10withdraws is a member of a class m(and you're using C++03), then you can use member-initializer list to default-initialize which will initialize all the elements to zero.

class myclass
{
      int last10withdraws[10];
   public:
      myclass() : last10withdraws() 
      {       //^^^^^^^^^^^^^^^^^^^ member initializer list
      }
};

Hope that helps.

Upvotes: 2

Related Questions