How do I change it so that it will only say how many times the program has ran once I typed another key then y.

Output:

=================================

Programmer Name: Peter Langlands
Program 3 Description:
CS 150 Spring 2011 Lab CRN:
Date:
=================================

Enter an nonnegative number: 10
a0 = 10
a1 = 5
a2 = 16
a3 = 8
a4 = 4
a5 = 2
a6 = 1

The integer k such tha a_k = 1 is:
The sum of the series equals:
The largest number of the sequence is:
The largest number in the sequence is in position:
There are ? odd values in the sequence.
There are ? even values in the sequence.
=================================

Would you like to run this program again?
Enter Y or y to continue, or any other key to exit: y
=================================

Thanks for using the super sequence analyzer!
This progam has ran 1 times.

=================================

Programmer Name: Peter Langlands
Program 3 Description:
CS 150 Spring 2011 Lab CRN:
Date:
=================================

Enter an nonnegative number: 2
a0 = 2
a1 = 1
a2 = 8
a3 = 0
a4 = -4
a5 = -6
a6 = -7

The integer k such tha a_k = 1 is:
The sum of the series equals:
The largest number of the sequence is:
The largest number in the sequence is in position:
There are ? odd values in the sequence.
There are ? even values in the sequence.
=================================

Would you like to run this program again?
Enter Y or y to continue, or any other key to exit: n
=================================

Thanks for using the super sequence analyzer!
This progam has ran 2 times.
=================================

Press any key to continue . . .

#include <iostream>

using namespace std;
 
void printInfo();
int howMany();
int nonNegative(int num);

int main()
{
    int num;
    char choice = 'Y';
    
     cout << "=================================\n" << endl;
     
    while(choice == 'Y' || choice == 'y')
    {
    printInfo();
    nonNegative(num);
     cout << "=================================\n" << endl;
    cout << "Would you like to run this program again? " << endl;
    cout << "Enter Y or y to continue, or any other key to exit: ";
    cin >> choice;
    howMany();
    }

    system("pause");
    return 0;
}
//==================================================================//
void printInfo()
{
     cout << "Programmer Name: Peter Langlands\n"
          << "Program 3 Description:\n"
          << "CS 150 Spring 2011 Lab CRN:\n"
          << "Date:" << endl;
     cout << "=================================\n" << endl;
}
//==================================================================//
int nonNegative(int num)
{
    cout << "Enter an nonnegative number: ";
    cin >> num;
    cout << "a0 = " << num << endl;
    cout << "a1 = " << num / 2 << endl;
    cout << "a2 = " << num + 6 << endl;
    cout << "a3 = " << num - 2 << endl;
    cout << "a4 = " << num - 6 << endl;
    cout << "a5 = " << num - 8 << endl;
    cout << "a6 = " << num - 9 << endl;
    cout << "\nThe integer k such tha a_k = 1 is:" << endl;
    cout << "The sum of the series equals:" << endl;
    cout << "The largest number of the sequence is:" << endl;
    cout << "The largest number in the sequence is in position:" << endl;
    cout << "There are ? odd values in the sequence." << endl;
    cout << "There are ? even values in the sequence." << endl;
}
//==================================================================//
int howMany()
{   
    static int count = 0;
    count++;
    cout << "=================================" << endl;
    cout << "\nThanks for using the super sequence analyzer!" << endl;
    cout << "This progam has ran " << count << " times." << endl;
    cout << "=================================\n" << endl;
}
//==================================================================//

Move the output outside the loop.

Move the output outside the loop.

I moved howManyoutside of the loop but then when I ran my program it gave me a 1 as a result every time. How do I fix this?

put count in main and everytime you call the function add 1 then have that number passed to howMany(int a) (howMany should take an int) like howMany(count).

I said put the output outside the loop. howMany() does more than just output.

I said put the output outside the loop. howMany() does more than just output.

Would it be like this?

#include <iostream>

using namespace std;
 
void printInfo();
int howMany();
int nonNegative(int num);

int main()
{
    int num;
    char choice = 'Y';
    
     cout << "=================================\n" << endl;
     
    while(choice == 'Y' || choice == 'y')
    {
    printInfo();
    nonNegative(num);
     cout << "=================================\n" << endl;
    cout << "Would you like to run this program again? " << endl;
    cout << "Enter Y or y to continue, or any other key to exit: ";
    cin >> choice;
    howMany();
    }

cout << "=================================" << endl;
    cout << "\nThanks for using the super sequence analyzer!" << endl;
    cout << "This progam has ran " << count << " times." << endl;
    cout << "=================================\n" << endl;

    system("pause");
    return 0;
}
//==================================================================//
void printInfo()
{
     cout << "Programmer Name: Peter Langlands\n"
          << "Program 3 Description:\n"
          << "CS 150 Spring 2011 Lab CRN:\n"
          << "Date:" << endl;
     cout << "=================================\n" << endl;
}
//==================================================================//
int nonNegative(int num)
{
    cout << "Enter an nonnegative number: ";
    cin >> num;
    cout << "a0 = " << num << endl;
    cout << "a1 = " << num / 2 << endl;
    cout << "a2 = " << num + 6 << endl;
    cout << "a3 = " << num - 2 << endl;
    cout << "a4 = " << num - 6 << endl;
    cout << "a5 = " << num - 8 << endl;
    cout << "a6 = " << num - 9 << endl;
    cout << "\nThe integer k such tha a_k = 1 is:" << endl;
    cout << "The sum of the series equals:" << endl;
    cout << "The largest number of the sequence is:" << endl;
    cout << "The largest number in the sequence is in position:" << endl;
    cout << "There are ? odd values in the sequence." << endl;
    cout << "There are ? even values in the sequence." << endl;
}
//==================================================================//
int howMany()
{   
    static int count = 0;
    count++;
}
//==================================================================//

Would it be like this?

Did it work?

Did it work?

no it, still gave me the result of 1 run every time.

So maybe you need to bring the variable count outside of howMany() so the rest of the program can see it. Since it's local to the function alone, it doesn't exist.

And I beg to differ. It does not give you 1 every time. The program doesn't even compile.

How could I loop it through so that if !a = 1 then it will keep going in the sequence to the next a such as a2, a3, a4, a5 ...

#include <iostream>
#include <fstream>

using namespace std;
 

void printInfo();
int nonNegative(int a, int k);
int nexta(int a);
void initialize(int& oddCount , int& evenCount);
void getx(int& a);

int main()
{
    int count;
    int num;
    int zeros;
    int odds;
    int evens;
    int a, k;
    k = 0;
    char choice = 'Y';
    ofstream outFile;
   
    //----------------------------------------
    // Call function tagReader with tags1.txt
    
    outFile.open("Langlands.txt");
    
    
    outFile.close();
    
    printInfo();
    nonNegative(a, k);
    initialize(odds, evens);
    
    nexta(a);
    cout << "=================================\n" << endl;
    
    system("pause");
    return 0;
}
//==================================================================//
void printInfo()
{
     cout << "=================================\n" << endl;
     cout << "Programmer Name: Peter Langlands\n"
          << "Program 3 Description:\n"
          << "CS 150 Spring 2011 Lab CRN:\n"
          << "Date:" << endl;
     cout << "=================================\n" << endl;
}
//==================================================================//
int nonNegative(int a, int k)
{
    k = 0;
    int nexta;
    int even = 0;
    int odd = 0;
    
    ofstream outFile;   
    
    
    
cout << "Enter an nonnegative number: ";
    cin >> a;
       if( a % 2== 0 )
    {    
	   even++;
     }
       else 
    {
        odd++;
    }

    cout << "a" << k << " = " << a;
    outFile << "a" << k << " = " << a;
    while(a == 0)
    {
     k++;
     a = nexta;
     cout << "a" << k << " = " << a;
    outFile << "a" << k << " = " << a;
     }
    

    cout << "\nThe integer k such tha a_k = 1 is:" << endl;
    cout << "The sum of the series equals:" << endl;
    cout << "The largest number of the sequence is:" << endl;
    cout << "The largest number in the sequence is in position:" << endl;
    cout << "There are " << odd << "  odd values in the sequence." << endl;
    cout << "There are " << even << "  even values in the sequence." << endl;

}
//==================================================================//
int nexta(int a)
{
   
}
//==================================================================//
void initialize(int& oddCount , int& evenCount)
{
     oddCount = 0;
     evenCount = 0;
}
//==================================================================//
void getx(int& a)
{
     cin >> a;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.