That's somewhat of a tricky formatting scheme. If you do it with straight printing, it would end up looking like this:
Code:
#include <iostream>
using namespace std;
int main()
{
int input_address[10] = {0,1,2,4,8,16,32,64,128};
int input = 4;
bool high = false;
for (int i = 1; i < 9; i++)
{
if (input > input_address[i])
{
high = true;
// Not the first item
if (i != 1) {
if (i == 8 || (i + 1 < 9 && input == input_address[i + 1]))
cout << " and ";
else
cout << ", ";
}
cout << i;
}
}
if (!high)
cout << "No items";
cout << " are high" << endl;
}
Not very pretty. A better way is to save the high values and then pretty print them after the processing is done:
Code:
#include <iostream>
using namespace std;
int main()
{
int input_address[10] = {0,1,2,4,8,16,32,64,128};
int high[8] = {0};
int n = 0;
int input = 4;
for (int i = 1; i < 9; i++)
{
if (input > input_address[i])
high[n++] = i;
}
if (n == 0)
{
cout << "No items are high" << endl;
}
else {
cout << (n == 1 ? "Input " : "Inputs ");
for (int i = 0; i < n; i++)
{
cout << high[i];
if (i + 1 == n - 1)
cout << " and ";
else if (i != n - 1)
cout << ", ";
}
cout << (n == 1 ? " is " : " are ") << "high" << endl;
}
}
It might not be as concise, but it's easier to get right, and localizing/separating operations will greatly simplify your code. The nice part is that now you can modularize the pretty print into a function:
Code:
#include <iostream>
using namespace std;
void print(int high[], int n)
{
if (n == 0)
{
cout << "No items are high" << endl;
}
else {
cout << (n == 1 ? "Input " : "Inputs ");
for (int i = 0; i < n; i++)
{
cout << high[i];
if (i + 1 == n - 1)
cout << " and ";
else if (i != n - 1)
cout << ", ";
}
cout << (n == 1 ? " is " : " are ") << "high" << endl;
}
}
int main()
{
int input_address[10] = {0,1,2,4,8,16,32,64,128};
int high[8] = {0};
int n = 0;
int input = 4;
for (int i = 1; i < 9; i++)
{
if (input > input_address[i])
high[n++] = i;
}
print(high, n);
}
>every 2 digits to get the output to look like this 12:01:01 instead of 120101
Is thetime and integer or a string? If it's a string then the task is a simple matter of printing two characters then a colon:
Code:
#include <iostream>
using namespace std;
int main()
{
char thetime[] = "120101";
cout << "The time is "
<< thetime[0] << thetime[1] << ':'
<< thetime[2] << thetime[3] << ':'
<< &thetime[4] << endl;
}
C++ strings make it much cleaner:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string thetime = "120101";
cout << "The time is "
<< thetime.substr(0, 2) << ':'
<< thetime.substr(2, 2) << ':'
<< thetime.substr(4) << endl;
}
Though if you're using strings, you should have formatted them correctly to begin with, so I suspect you're using an integer. That's harder, but because times are well formatted, you can do something like this:
Code:
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int thetime = 120101;
cout << "The time is "
<< setw(2) << setfill('0') << thetime / 10000 << ':'
<< setw(2) << setfill('0') << (thetime / 100) % 10 << ':'
<< setw(2) << setfill('0') << thetime % 10 << endl;
}
Too bad it's not a good one-liner.