How do I remove comma after last number/output loop in C++ - c++

My Code:
#include <iostream>
using namespace std;
void result(int test)
{
for (int num = 1; num <= test; num++)
{
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
cout << ",";
}
}
int main()
{
int test = 100;
result(test);
return 0;
}
My Output:
1,2,love ,4,extraextra,love ,7,8,love ,extraextra,11,love ,13,14,extra,16,17,love ,19,extraextra,love ,22,23,love ,extraextra,26,love ,28,29,extra,31,32,love ,34,extraextra,love ,37,38,love ,extraextra,41,love ,43,44,extra,46,47,love ,49,extraextra,love ,52,53,love ,extraextra,56,love ,58,59,extra,61,62,love ,64,extraextra,love ,67,68,love ,extraextra,71,love ,73,74,extra,76,77,love ,79,extraextra,love ,82,83,love ,extraextra,86,love ,88,89,extra,91,92,love ,94,extraextra,love ,97,98,love ,extraextra,
My question:
How to delete only the very last comma?

Add a condition for the last value of num. The full code would be:
#include <iostream>
using namespace std;
void result(int test)
{
for (int num = 1; num <= test; num++)
{
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
if(num < test) cout << ","; // Here you add the condition
}
cout << endl; // Allows for flushing the output (Thanks #bruno)
}
int main()
{
int test = 100;
result(test);
return 0;
}

In case you really do not like the additional test of num visible in
for (int num = 1; num <= test; num++)
{
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
if (num < test)
cout << ',';
}
and
for (int num = 1; num <= test; num++)
{
if (num != 1)
cout << ',';
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
}
you can avoid that explicit test doing :
const char * sep = "";
for (int num = 1; num <= test; num++)
{
cout << sep;
sep = ",";
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
}
but probably this is less readable and a (very) little more expensive at the execution

Your loop runs for the last time at num == test, so just print commas for num less than test or num not equal to test.
i.e. simply replace cout << ","; with:
if(num < test) cout << ",";
or
if(num != test) cout << ",";

After having read all the suggestion and the constructive critics, I modified my answer in the following way:
#include <iostream>
using namespace std;
void singlecheck(int num){
if (num % 3 == 0 && num % 5 == 0)
cout << "extra";
else if (num % 3 == 0 )
cout << "love ";
else if(num %5 == 0)
cout << "extraextra";
else
cout << num;
}
void result(int test)
{
if(test>0){
for (int num = 1; num <test; num++){
singlecheck(num);
cout << ", ";
}
singlecheck(test);
cout << endl;
}
}
int main()
{
int test = 15;
result(test);
return 0;
}
I added one more function, which is doing the "test" job, while the cycle is done in another function. Now the result is correct also with input 0: I added one if at the beginning. In this way, only one if-check has to be done instead of a total of test if-checks, which was my principal concern. Previous version has repeated code which is not a good programming practice.

Related

C++ Program leaving my for loop prematurely

I'm working on a little poker application and i've run into the first problem I just can't seem to comprehend.
while (allplayersGood != 1) { //round table till all decided
cout << "TOP OF WHILE LOOP";
for (int i = 0; i < PLAYER_COUNT; i++) { //for loop for decisions from non button or blinds
int player_decision = 1;
char choice;
if ((players[i].playerhand.card1.value != 'F') && (players[i].playerhand.card1.value != 'C')) {
if ((players[i].blind != 1 && players[i].blind != 2) && players[i].button != true) {
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
if (playerTable->currentBet > players[i].currentBet) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type F for Fold, B for Call, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'F') {
player_decision = 0;
players[i].fold();
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'B') {
player_decision = 0;
players[i].call(playerTable);
}
}
if ((playerTable->currentBet == players[i].currentBet) && player_decision != 0) { //big blind after round table
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type C for Check, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'B') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'C') {
if (players[i].check(playerTable) == true) {
player_decision = 0;
}
}
}
}
}
else if (players[i].blind == 1 || players[i].blind == 2) {
if (players[i].blind == 1) {
players[i].chip_amount -= sblind;
playerTable->currentPot += sblind;
players[i].blind = 0;
players[i].currentBet = sblind;
}
if (players[i].blind == 2) {
players[i].chip_amount -= bblind;
playerTable->currentPot += bblind;
players[i].blind = 0;
players[i].currentBet = bblind;
}
}
}
}
for (int i = 0; i < PLAYER_COUNT; i++) { //seperate loop for button and blinds that were ignored in loop above
int player_decision = 1;
char choice;
if (players[i].button == true || players[i].blind == 1) { //button and small blind
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type F for Fold, B for Call, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'F') {
player_decision = 0;
players[i].fold();
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'B') {
player_decision = 0;
players[i].call(playerTable);
}
}
}
cout << i;
if (players[i].blind == 2) { //big blind
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "C for Check, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'C') {
if (players[i].check(playerTable) == true) {
player_decision = 0;
}
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
}
}
}
int playersBetting = 0;
int playersGood = 0;
int playersChecked = 0;
int playersNot = 0;
for (int i = 0; i < PLAYER_COUNT; i++) {
if (players[i].playerhand.card1.value != 'F') {
playersBetting++;
if (players[i].currentBet == playerTable->currentBet) {
playersGood++;
}
}
}
for (int i = 0; i < PLAYER_COUNT; i++) {
if (players[i].playerhand.card1.value != 'F') {
if (players[i].isChecked == true) {
playersChecked++;
}
else {
playersNot++;
}
}
}
cout << playersBetting << playersGood;
if ((playersBetting == playersGood) || (playersNot == 0)) {
cout << "NEXT ROUND STARTED";
}
}
The issue is, during the second for loop with comment "seperate loop for button and blinds that were ignored in loop above" after the first if statement succeeds because players[0] has button equal to true, the player will make the terminal input as a decision, and the program will exit the for loop and go down to the end with the playersBetting and playersGood loops, then return back to the for loop at index 1 correctly.
I'm sorry if this is a little complicated to understand there is a lot of code that I probably didn't put into context very well, if you need any extra information please let me know.
Thank you.
You seem to have different loops inside one another. This is possible, but in that case, you need to use another loop variable (j instead of i), let me show you what happens:
for i ...
for j ...
This causes the following values to be taken for i and j:
i j
1 1
1 2
1 ...
1 n
2 1
2 2
2 ...
2 n
...
n 1
n 2
...
n n
... and here it stops.
If you keep using i in the inner loop, this is what you get:
i (outside loop) i (inside loop)
1 1
2 2 // indeed: changing i inside also changes i outside
... ...
n n
So you jump out of the outside loop, even after just having looped the inside loop one time.
I figured it out, it was unrelated to the actual loop and actually had to do with a value I changed upstream. Thank you for the few who tried to help with such little context haha
Have a good one

find number of occurances of specific digit with a counter in C++

I have a large number whereby i want to find the number of occurances of a specified digit. I wonder is using a counter will work. My code as follows:
#include <iostream>
using namespace std;
int main( )
{
int number;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int digit;
int digitCounter = 0;
cout << "Please enter a number between 100 to 2000000000." << endl;
cin >> number;
cout << "Please enter a digit that you want to find the number of occurances." << endl;
cin >> digit;
if (number > 0)
{
n1 = number % 10;
n2 = (number/10) % 10;
n3 = (number/100) % 10;
n4 = (number/1000) % 10;
n5 = (number/10000) % 10;
n6 = (number/100000) % 10;
n7 = (number/1000000) % 10;
n8 = (number/10000000) % 10;
n9 = (number/100000000) % 10;
n10 = (number/100000000) % 10;
if (n1 == digit)
{ digitCounter++;}
else if (n2 == digit)
{ digitCounter++;}
else if (n3 == digit)
{ digitCounter++;}
else if (n4 == digit)
{ digitCounter++;}
else if (n5 == digit)
{ digitCounter++;}
else if (n6 == digit)
{ digitCounter++;}
else if (n7 == digit)
{ digitCounter++;}
else if (n8 == digit)
{ digitCounter++;}
else if (n9 == digit)
{ digitCounter++;}
else if (n10 == digit)
{ digitCounter++;}
cout<< "The total number of occurances of " << digit << " in " << number <<" is "<<digitCounter<< endl;
}
else
cout<< "You have entered an invalid number."<<endl;
system("pause");
return 0;
}
However the counter is not working. Can someone advise what wet wrong?
Any help is really appreciated, thanks.
You can cast your number into a string after that you search the digit in the string it's simpler.
Or you can read a character string (number) and a character (digit) and then you do like that :
char number[20], digit;
int count = 0, i;
printf("\nEnter a string : ");
scanf("%s", &number);
printf("\nEnter the character to be searched : ");
scanf("%c", &digit);
for (i = 0; number[i] != '\0'; i++) {
if (number[i] == digit)
count++;
}
if (count == 0)
printf("\nCharacter '%c'is not present", digit);
else
printf("\nOccurence of character '%c' : %d", digit, count);`
Your Else If's need to be If's. As it stands now you only go through one decision statement. As soon as it finds a match you're out.
#include <iostream>
using namespace std;
int main()
{
int number;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int digit;
int digitCounter = 0;
cout << "Please enter a number between 100 to 2000000000." << endl;
cin >> number;
cout << "Please enter a digit that you want to find the number of occurances." << endl;
cin >> digit;
if (number > 0)
{
n1 = number % 10;
n2 = (number / 10) % 10;
n3 = (number / 100) % 10;
n4 = (number / 1000) % 10;
n5 = (number / 10000) % 10;
n6 = (number / 100000) % 10;
n7 = (number / 1000000) % 10;
n8 = (number / 10000000) % 10;
n9 = (number / 100000000) % 10;
n10 = (number / 100000000) % 10;
if (n1 == digit)
{
digitCounter++;
}
if (n2 == digit)
{
digitCounter++;
}
if (n3 == digit)
{
digitCounter++;
}
if (n4 == digit)
{
digitCounter++;
}
if (n5 == digit)
{
digitCounter++;
}
if (n6 == digit)
{
digitCounter++;
}
if (n7 == digit)
{
digitCounter++;
}
if (n8 == digit)
{
digitCounter++;
}
if (n9 == digit)
{
digitCounter++;
}
if (n10 == digit)
{
digitCounter++;
}
cout << "The total number of occurances of " << digit << " in " << number << " is " << digitCounter << endl;
}
else
cout << "You have entered an invalid number." << endl;
system("pause");
return 0;
}
try this
#include <iostream>
int main(int argc, char **argv) {
unsigned long long large(0);
int digitToFind(0);
std::cout << "enter a large number [100 to 2000000000]" << std::endl;
std::cin >> large;
if (large < 100 || large > 2000000000) {
std::cout << "invalid input." << std::endl;
return -1;
}
std::cout << "enter the digit to find" << std::endl;
std::cin >> digitToFind;
if (digitToFind < 0 || digitToFind > 9) {
std::cout << "invalid input." << std::endl;
return -1;
}
std::size_t counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
while (large > 0) {
int rem = large % 10;
counts[rem]++;
large /= 10;
}
std::cout << "number of occurrances of " << digitToFind << " is "
<< counts[digitToFind] << std::endl;
std::cout << "press enter to continue" << std::endl;
std::cin.get();
return 0;
}
Some of the sample codes contain another flaw: if the number is small then we get more zeroes than necessary...we cannot assume that the number is necessarily big.
This works for each number, in Python only because I'm more used to Python at the moment, it is straightforward to convert it to C:
N = 1230533007
digitToFind = 3
digitCount = 0
while N > 0:
d = N % 10
if d == digitToFind:
digitCount += 1
N //= 10
print digitCount

Segmentation fault: 11 c++ Error

this is my first time asking a question on this forum so here it goes. I am creating a tic-tac-toe game for practice and am using enumerators and recursion because I have never really done enumeration and could always get some recursion practice in. Well anyway I just finished coding for the player2 to take a random move and after about 3 turns it gives a segmentation fault and I cannot figure out why... I hope you guys can figure it out and thank you!
#include <iostream>
#include <string>
#include <cstdlib>
const int size = 3;
enum play {none,X,O};
void NPC(play (&board)[size][size],play player2) {
srand(time(NULL));
int tempx = rand() % 3;
int tempy = rand() % 3;
if(board[tempx][tempy] == none)
board[tempx][tempy] = player2;
else
NPC(board,player2);
}
void getBoardState(play (&board)[size][size],int y,int x) {
if(board[x][y] == none) std::cout << " ";
else if(board[x][y] == X) std::cout << "X";
else std::cout << "O";
}
void printboard(play (&board)[size][size]){
int length = 4 * size - 1;
for(int i = 1; i <= length; i++) {
for(int j = 1; j <= length; j++) {
if(i % 4 == 0 && j % 4 == 0) std::cout << "+";
else if(i % 4 == 0) std::cout << "-";
else if(j % 4 == 0) std::cout << "|";
else if(i % 2 == 0 && j % 2 == 0) getBoardState(board,(i - 2)/4,(j - 2)/4);
else std::cout << " ";
}
std::cout << std::endl;
}
}
int main() {
play player = O, player2 = X;
bool over = false;
play board[size][size];
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
board[i][j] = none;
}
}
std::string player1 = "";
std::cout << "What would You like to be? An X or an O?" << std::endl;
while(((player1 != "X") + (player1 != "O")) == 2) {
std::cin >> player1;
if(((player1 != "X") + (player1 != "O")) == 2)
std::cout << "Invalid entry! Please enter X or an 0!" << std::endl;
}
if(player1 == "X") {
player2 = O;
player = X;}
int tempx,tempy;
while(!over) {
std::cout << "Please enter an x and then a y (1 to " << size << ")" << std::endl;
std::cin >> tempx;
std::cin >> tempy;
while(tempx > size || tempy > size || board[tempx-1][tempy-1] != none) {
std::cout << "Invalid entry! Try again!" << std::endl;
std::cin >> tempx;
std::cin >> tempy;
}
board[tempx-1][tempy-1] = player;
NPC(board,player2);
printboard(board);
}
return 0;
}
You're running out of stack space in your recursion because you call srand(time(NULL)) every time. The random number generator should only be seeded once, in main, and not in NPC. time(NULL) returns a number of seconds, so it changes infrequently (compared to how fast your recursive function calls will occur) which will consume all available stack space.

need help to incorporate this 2nd function into my program

hi so i'm making a program that will ask the user to enter an integer and spit it back out in roman numerals. i had it working so the roman numeral part would work but i couldn't loop the program. now i have the program looped but i can't get the roman numeral function to work. the program executes but it's as if function1 doesn't exist and i dont know how to get the main function to use it. i have to keep all user interaction in the main function. i want the cout << roman << endl; line to spit out the integer in roman numerals but it's not working. this is only my 2nd assignment ever so any help would be appreciated! thanks
#include <iostream>
#include <string>
using namespace std;
string roman;
int integer;
int num;
char answer;
int main()
{
while (true)
{
cout << "Enter Integer " << endl;
cin >> integer;
cout << roman << endl;
cout << "convert another (y/n)? " << endl;
cin >> answer;
if (answer == 'n' || answer == 'N')
break;
}
}
int function1()
{
if ((integer >= 4000) || (integer <= 0))
{
cout << endl << "Invalid Integer" << endl;
}
//3286
else
{
if (integer >= 1000)
{
num = (integer / 1000);
for (int i = 0; i < num; i++)
{
roman += 'M';
}
integer %= 1000;
// care ^^^
}
// 286
if (integer >= 100)
{
num = (integer / 100);
if (num == 9)
{
roman += "CM";
}
else if (num >= 5)
{
roman += 'D';
for (int i = 0; i < num - 5; i++)
{
roman += 'C';
}
}
else if (num == 4)
{
roman += "CD";
}
else if (num >= 1)
{
for (int i = 0; i < num; i++)
{
roman += 'C';
}
}
integer %= 100;
}
// 86
if (integer >= 10)
{
num = (integer / 10);
if (num == 9)
{
roman += "XC";
}
else if (num >= 5)
{
roman += 'L';
for (int i = 0; i < num - 5; i++)
{
roman += 'X';
}
}
else if (num == 4)
{
roman += "XL";
}
else if (num >= 1)
{
for (int i = 0; i < num; i++)
{
roman += 'X';
}
}
integer %= 10;
// 6
}
if (integer >= 1)
{
num = integer;
if (num == 9)
{
roman += "IX";
}
else if (num >= 5)
{
roman += "V";
for (int i = 0; i < num - 5; i++)
{
roman += 'I';
}
}
else if (num == 4)
{
roman += "IV";
}
else if (num >= 1)
{
for (int i = 0; i < num; i++)
{
roman += 'I';
}
}
}
cout << "--> " << roman << endl;
}
system("PAUSE");
return 0;
}
You need to actually call function1, for example:
while (true)
{
cout << "Enter Integer " << endl;
cin >> integer;
function1();
cout << roman << endl;
cout << "convert another (y/n)? " << endl;
cin >> answer;
if (answer == 'n' || answer == 'N')
break;
}

I don't have any compile errors but my code won't work. What's wrong?

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;
}
}
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:
it adds a new element with the value of 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;
}
}
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.