mardi 31 janvier 2017

C++ program utilizing pointers branching and loops having output issues

This is a homework assignment that required the use of pointers to out put messages when the number input is divisible 3, by 5, and by 3 & 5. I have gotten the code to compile and run but I'm not getting my desired output and am not certain what needs changed. Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;
using std::istream;

int main()
{
    const int i{3};                      // Variable initialization and declaration
    long* pnumber{};                     // Pointer definition and initialization
    long number1 {};                     // Declaration and initialization of variable number1
    long number2 [i];                    // Declaration and initialization of variable containing array of 3L numbers
    char indicator{ 'n' };               // Continue or not?

    pnumber = &number1;                  // Store address in a pointer

    for (;;)
    {
        cout << "Please enter any number less than 2 billion, "
             << "then press the Enter key: ";
        cin  >> number1;

        if (*pnumber % 3 == 0)           // Test if remainder after dividing by 3 is 0  
        {
            cout << endl
                << "Number: " << number1
                << "-Fizz" << endl
                << endl << "Do you want to enter another value (please enter y or n? ";
            cin >> indicator;           // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                  // Exit from loop
        }

        if (*pnumber % 5 == 0)         // Test if remainder after dividing by 5 is 0  
        {
            cout << endl
                << "Number: " << number1
                << "-Buzz" << endl
                << endl << "Do you want to enter another value (please enter y or n? ";
            cin >> indicator;         // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                // Exit from loop
        }

        if ((*pnumber % 3 == 0) && (*pnumber % 5 == 0))    // Test if remainder after dividing by 5 and 3 is 0 
        {
            cout << endl
                << "Number: " << number1
                << "-FizzBuzz" << endl
                << endl << "Do you want to enter another value (please enter y or n? ";
            cin >> indicator;         // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                // Exit from loop
        }

        else
        {
            cout << endl              // Default: here if not divisible by 3 or 5  
                << "Please enter another number."


                << endl << "Do you want to enter another value (please enter y or n? ";
            cin >> indicator;         // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                // Exit from loop
        }
    }

    pnumber = &number2 [i];               // Change pointer to address of number2

    for (;;)
    {
        cout << endl << endl 
             << "Please enter an array of number(s), where the number is less than 2 billion, "
             << "then press the Enter key: ";
        cin >> number2 [i];

        if (*pnumber % 3 == 0)           // Test if remainder after dividing by 3 is 0  
        {
            cout << endl
                << "Number: " << number2
                << "-Fizz" << endl
                << endl << "Do you want to enter another array (please enter y or n? ";
            cin >> indicator;           // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                  // Exit from loop
        }

        if (*pnumber % 5 == 0)         // Test if remainder after dividing by 5 is 0  
        {
            cout << endl
                << "Number: " << number2
                << "-Buzz" << endl
                << endl << "Do you want to enter another array (please enter y or n? ";
            cin >> indicator;         // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                // Exit from loop
        }

        if ((*pnumber % 3 == 0) && (*pnumber % 5 == 0))    // Test if remainder after dividing by 5 and 3 is 0 
        {
            cout << endl
                << "Number: " << number2
                << "-FizzBuzz" << endl
                << endl << "Do you want to enter another value (please enter y or n? ";
            cin >> indicator;         // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                // Exit from loop
        }

        else
        {
            cout << endl              // Default: here if not divisible by 3 or 5  
                << "Please enter another number."


                << endl << "Do you want to enter another value (please enter y or n? ";
            cin >> indicator;         // Read indicator
            if (('n' == indicator) || ('N' == indicator))
                break;                // Exit from loop
        }
    }

    cout << endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire