find the total amount of frabjous numbers (any number containing a "7") within the specified range.

Any tips on how to add up the total frabjous numbers in my code?


#include<iostream>
#include<string>
using namespace std;

const int FRABJOUS_NUMBER = 7;

int main(void)
{
while (true)
{
// input two non negative numbers w/ loop
///////////////////////////////////////////
int firstValue, secondValue;
while (true)
{
cout << "Enter low and high bounds for a range of integer values: ";
cin >> firstValue >> secondValue;
cin.ignore(999,'\n');
if ( cin.fail() )
{
cin.clear();
cin.ignore(999,'\n');
cout << "Non-numeric input, try again." << endl;
continue;
}
if (firstValue < 0 || secondValue < 0)
{
cout << "Negative input, try again." << endl;
continue;
}
break;
}


// sort the minimum and maximum in range
////////////////////////////////////
int rangeMinimum, rangeMaximum;
if (firstValue > secondValue)
{
rangeMaximum = firstValue;
rangeMinimum = secondValue;
}
else
{
rangeMinimum = firstValue;
rangeMaximum = secondValue;
}

// introduce inbetween integers of desired range
////////////////////////////////
int inbetweenInteger = rangeMinimum;
while (inbetweenInteger < rangeMaximum)
{
++inbetweenInteger;
}


// checks for every value within range minimum and maximum
// decide if integer is frabjous
//////////////////////////////////////////////////////////
int totalFrabjous_numbers = rangeMinimum + inbetweenInteger + rangeMaximum;
while (true)
{
if ( (rangeMinimum /= 10) == FRABJOUS_NUMBER)
{
totalFrabjous_numbers++;
}
else
if ( (rangeMinimum % 10) == FRABJOUS_NUMBER)
{
totalFrabjous_numbers++;
}


if ( (inbetweenInteger /= 10) == FRABJOUS_NUMBER)
{
totalFrabjous_numbers++;
}
else
if ( (inbetweenInteger % 10) == FRABJOUS_NUMBER)
{
totalFrabjous_numbers++;
}


if ( (rangeMaximum /= 10) == FRABJOUS_NUMBER)
{
totalFrabjous_numbers++;
}
else
if ( (rangeMaximum % 10) == FRABJOUS_NUMBER)
{
totalFrabjous_numbers++;
}
break;
}

// output total
cout << totalFrabjous_numbers << endl;

// end stuff
cout << endl;
cout << "Press ENTER to finish...";
cin.ignore(999,'\n');
return 0;
}
}
Never heard the term "frabjous number" for this.

If you want to know whether a number contains a '7', stream it into a string (use stringstream), then test for the character '7'. If it fulfils this criterion add the original number to a rolling sum.

Your range of integers can easily be dealt with by a for loop. No idea why your code above is so complex.
[In everything that follows I'm assuming that correct header files are included]

Declare 3 vector<int> and 3 ints:
1
2
vector<int> v_rangeMinimum, v_inbetweenInteger, v_rangeMaximum;
int sum_rangeMinimum, sum_inbetweenInteger, sum_rangeMaximum;


then keep pushing back the vectors with any FRABJOUS_NUMBER found, so for rangeMinimum:
1
2
3
4
5
6
7
8
9
10
11
12

if ( (rangeMinimum /= 10) == FRABJOUS_NUMBER)
{
v_rangeMinimum.push_back(rangeMinimum);
totalFrabjous_numbers++;
}
else
if ( (rangeMinimum % 10) == FRABJOUS_NUMBER)
{
v_rangeMinimum.push_back(rangeMinimum);
totalFrabjous_numbers++;
}


Then to sum up the elements of each vector you can use:

1. the classic for loop:
1
2
3
for (vector<int>::iterator itr = v_rangeMinimum.begin(), itr != v_rangeMinimum.end(), ++itr){
		sum_rangeMinimum += *itr;
	}

2. the algorithm accumulate:
 
sum_rangeMinimum = accumulate(v_rangeMinimum.begin(), v_rangeMinimum.end(),0);


3. if you (and your compiler) are familiar with C++11 and higher:
1
2
3
4

for (auto& itr : v_rangeMinimum){
	sum_rangeMinimum += itr;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <sstream>
using namespace std;

bool isFrabjous( int n );

int main()
{
   int n1, n2;
   int sum = 0;

   cout << "Input range (min max): ";
   cin >> n1 >> n2;

   for ( int n = n1; n <= n2; n++ ) if ( isFrabjous( n ) ) sum += n;

   cout << "Sum is " << sum;
}


bool isFrabjous( int n )
{
   stringstream ss;

   ss << n;
   return ( ss.str().find( "7" ) != string::npos );
}
Topic archived. No new replies allowed.