Reputation: 21
I am doing a simple program where I accept the first four digits of a phone number and must generate all the possibilities for the rest of the digits. The last 6 digits must add to equal 33. If the fourth digit is odd, the fifth must be even, and if the fourth digit is even the fifth must be odd.
I have established that it is not adding the numbers together and the for loops in the function plotNumbers() are not actually looping. I am completely lost. Anyone have any ideas?
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void plotNumbers(vector<int> number1, int digit1, int digit2, int digit3, int digit4);
bool findTrueFalse(vector<int> vec);
int _tmain(int argc, _TCHAR* argv[])
{
int digit1, digit2, digit3, digit4;
vector<int> number;
cout << "Enter the first four digits:\n";
cin >> digit1;
cin >> digit2;
cin >> digit3;
cin >> digit4;
if (digit4 == 0 || digit4 == 2 || digit4 == 4 || digit4 == 6 || digit4 == 8)
{
number.push_back(1);
plotNumbers(number, digit1, digit2, digit3, digit4);
}
else if (digit4 == 1 || digit4 == 3 || digit4 == 5 || digit4 == 7 || digit4 == 9)
{
number.push_back(0);
plotNumbers(number, digit1, digit2, digit3, digit4);
}
else
{
cout << "\nYou entered an illegal value. Good-bye.\n\n";
}
system("pause");
return 0;
}
void plotNumbers(vector<int> number1, int digit1, int digit2, int digit3, int digit4)
{
for ( ; number1[0] < 10; number1[0] += 2)
{
number1.push_back(0);
for ( ; number1[1] < 10; ++number1[1])
{
number1.push_back(0);
for ( ; number1[2] < 10; ++number1[2])
{
number1.push_back(0);
for ( ; number1[3] < 10; ++number1[3])
{
number1.push_back(0);
for ( ; number1[4] < 10; ++number1[4])
{
number1.push_back(0);
for ( ; number1[5] < 10; ++number1[5])
{
bool total33 = findTrueFalse(number1);
if (total33)
{
cout << "(" << digit1 << digit2 << digit3 << ") ";
cout << digit4 << number1[0] << number1[1] << " - ";
cout << number1[2] << number1[3] << number1[4] << number1[5] << endl;
}
}
}
}
}
}
}
}
bool findTrueFalse(vector<int> vec)
{
int total = 0;
vector<int>::iterator postIt;
for (postIt = vec.begin(); postIt != vec.end(); ++postIt)
{
total += *postIt;
}
if (total == 33)
{
return true;
}
else
{
return false;
}
}
Upvotes: 2
Views: 795
Reputation: 8006
How did you exactly check if they are looping? Because in my program they are, but I think you don't understand how they actually work (no offense meant).
Consider this snippet:
int i = 0;
int j = 0;
for (; i < 10; i += 1) { //OUTER for
cout << "i == " << i << endl;
for (; i < 10; j += 1;) { //INNER for
cout << "j == " << j << endl;
}
//THIS POINT
}
This program will enter the OUTER loop, print out "i == 0", then enter the INNER loop print 10 lines with "j == ...". Now at THIS POINT j == 10, so when the code does another loop for 'i' and checks the condition for the INNER for, it sees j == 10 so 10 is not lower then 10 so it skips the INNER for.
This is exactly what is happening in your program. When you exit an INNER loop with control variable specified like this you have to remember to change it's value to something that passes the condition, if you want to enter the INNER loop in the next round of OUTER loop.
I've changed your code to do it like that, and it looks like it works fine. All you need to do is add 4 lines of code after the INNER loops in your code to zero their control variables.
EDIT : Also, consider Emabs' answer, as your vector maintenance is quite poor.
Upvotes: 1
Reputation: 3775
First of all, try to use short codes to avoid bugs such as:
if (digit4 % 2 == 0)
instead of
if (digit4 == 0 || digit4 == 2 || digit4 == 4 || digit4 == 6 || digit4 == 8)
Second, you need to do number1.pushback(0)
only once, but you are doing this several times. Do you know what happens When you run number1.pushback(0)
? I guess not:
0
at the end of your vector number1
I suggest the following code (you do the indentation):
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void plotNumbers(vector<int> number1, int digit1, int digit2, int digit3, int digit4);
bool findTrueFalse(vector<int> vec);
int _tmain(int argc, _TCHAR* argv[])
{
int digit1, digit2, digit3, digit4;
vector<int> number;
cout << "Enter the first four digits:\n";
cin >> digit1;
cin >> digit2;
cin >> digit3;
cin >> digit4;
if (digit4 % 2 == 0)
{
number.push_back(1);
for (int i=0; i<5; i++)
number.push_back(0);
plotNumbers(number, digit1, digit2, digit3, digit4);
}
else if (digit4 % 2 == 1)
{
number.push_back(0);
for (int i=0; i<5; i++)
number.push_back(0);
plotNumbers(number, digit1, digit2, digit3, digit4);
}
else
{
cout << "\nYou entered an illegal value. Good-bye.\n\n";
}
system("pause");
return 0;
}
void plotNumbers(vector<int> number1, int digit1, int digit2, int digit3, int digit4)
{
for ( ; number1[0] < 10; number1[0] += 2)
{
number1[1] = 0;
for ( ; number1[1] < 10; ++number1[1])
{
number1[2] = 0;
for ( ; number1[2] < 10; ++number1[2])
{
number1[3] = 0;
for ( ; number1[3] < 10; ++number1[3])
{
number1[4] = 0;
for ( ; number1[4] < 10; ++number1[4])
{
number1[5] = 0;
for ( ; number1[5] < 10; ++number1[5])
{
bool total33 = findTrueFalse(number1);
if (total33)
{
cout << "(" << digit1 << digit2 << digit3 << ") ";
cout << digit4 << number1[0] << number1[1] << " - ";
cout << number1[2] << number1[3] << number1[4] << number1[5] << endl;
}
}
}
}
}
}
}
}
bool findTrueFalse(vector<int> vec)
{
int total = 0;
vector<int>::iterator postIt;
for (postIt = vec.begin(); postIt != vec.end(); ++postIt)
{
total += *postIt;
}
if (total == 33)
{
return true;
}
else
{
return false;
}
}
Upvotes: 1