Help with what might be a ""switch statement""

Hey y'all.

So basically, I'm very new to C++ but I was working on a project for class and I got stuck. What I need is a program that can check if one number's components can be added together to one another to get another number (the user inputs both numbers).

I have all the coding down fine, like all of my calculations run fine when run independently but I can't figure out how to make it so that if the computer checks one scenario and then REGARDLESS of whether or not that one works out, then checks the next scenario (I don't know if this enough information but I could provide more if need be).

I've been using if statements but what happens is that the program just automatically goes with the first scenario if it works and doesn't check any of the other ones. Would anyone happen to know if there is a better command that would accomplish what I need?

Edit: I should clarify that the program needs to check all of the scenarios.
Last edited on
It's best to post the code so that we can advise.
Gotcha, I will:
#include <iostream>
#include <cmath>

using namespace std;


int main() {

for (int i = 1; i <= 5; ++i) {

// Variable Declarations
int number1, goalieNumber, placeHolder, gamerNumber, secretNumber, secreterNumber;
int ones = 0, tens = 0, hundreds = 0, thousands = 0;
int first2 = 0, last2 = 0, first3 = 0, last3 = 0;
int otht = ones + tens + hundreds + thousands, f2l2 = first2 + last2, f3o = first3 + ones, tl3 = thousands + last3;

// Input
cout << "Hit me with a funky number that do be less than 10,000: ";
cin >> number1;

cout << "Goal number please: ";
cin >> goalieNumber;

// Values
ones = number1 % 10;
tens = number1 / 10 % 10;
hundreds = number1 / 100 % 10;
thousands = number1 / 1000 % 10;

first2= thousands * 10 + hundreds;
last2 = tens * 10 + ones;

first3 = thousands * 100 + hundreds * 10 + tens;
last3 = hundreds * 100 + tens * 10 + ones;

// Calculations
if (otht <= goalieNumber)
placeHolder = true;

if (f2l2 <= goalieNumber && f2l2 > otht)
placeHolder = false;
gamerNumber = true;

if (f3o <= goalieNumber && f3o > otht && f3o > f2l2)
placeHolder = false;
gamerNumber = false;
secretNumber = true;

if (tl3 <= goalieNumber && tl3 > f3o && tl3 > otht && tl3 > f2l2)
placeHolder = false;
gamerNumber = false;
secretNumber = false;
secreterNumber = true;


// Output
if (placeHolder == true)
cout << "The numbers you need are " << thousands << " + " << hundreds << " + " << tens << " + " << ones << endl;

else if (gamerNumber == true)
cout << "The numbers you need are " << first2 << " + " << last2 << endl;

else if (secretNumber == true)
cout << "The numbers you need are " << first3 << " + " << ones << endl;

else if (secreterNumber == true)
cout << "The numbers you need are " << thousands << " + " << last3 << endl;
}

return 0;

So the problem I'm having is in the calculations part where there are currently 4 if statements because it doesn't seem to be working right.
}
If you had used code tags
[code] ... [/code]
we would be able to see the indenting that you did. Plus, there would be line numbers that we could reference.

I'm guessing that you tried to indent all of the code associated with an if statement (like in Python). It doesn't work that way in C++. If you want multiple lines of code in a specific case, you need to create a "compound statement" by placing it inside { }.

for instance:

1
2
3
4
5
6
7
if (tl3 <= goalieNumber && tl3 > f3o && tl3 > otht && tl3 > f2l2)
{
    placeHolder = false;
    gamerNumber = false;
    secretNumber = false;
    secreterNumber = true;
}
Ayy thanks Doug, but like does it make sense for me to use 4 if statements there because it seems like it doesn't count the last three after the first one proves to be correct.
Formatting, using compound-if statements and slightly re-arranging the variable definitions gives:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	for (int i = 1; i <= 5; ++i) {
		// Variable Declarations
		int number1 {}, goalieNumber {};

		// Input
		cout << "Hit me with a funky number that do be less than 10,000: ";
		cin >> number1;

		cout << "Goal number please: ";
		cin >> goalieNumber;

		// Values
		const int ones = number1 % 10;
		const int tens = number1 / 10 % 10;
		const int hundreds = number1 / 100 % 10;
		const int thousands = number1 / 1000 % 10;
		const int first2 = thousands * 10 + hundreds;
		const int last2 = tens * 10 + ones;
		const int first3 = thousands * 100 + hundreds * 10 + tens;
		const int last3 = hundreds * 100 + tens * 10 + ones;
		const int otht = ones + tens + hundreds + thousands;
		const int f2l2 = first2 + last2;
		const int f3o = first3 + ones;
		const int tl3 = thousands + last3;

		bool placeHolder {}, gamerNumber {}, secretNumber {}, secreterNumber {};

		if (otht <= goalieNumber)
			placeHolder = true;

		if (f2l2 <= goalieNumber && f2l2 > otht) {
			placeHolder = false;
			gamerNumber = true;
		}

		if (f3o <= goalieNumber && f3o > otht && f3o > f2l2) {
			placeHolder = false;
			gamerNumber = false;
			secretNumber = true;
		}

		if (tl3 <= goalieNumber && tl3 > f3o && tl3 > otht && tl3 > f2l2) {
			placeHolder = false;
			gamerNumber = false;
			secretNumber = false;
			secreterNumber = true;
		}

		// Output
		if (placeHolder == true)
			cout << "The numbers you need are " << thousands << " + " << hundreds << " + " << tens << " + " << ones << endl;
		else
			if (gamerNumber == true)
				cout << "The numbers you need are " << first2 << " + " << last2 << endl;
			else
				if (secretNumber == true)
					cout << "The numbers you need are " << first3 << " + " << ones << endl;
				else
					if (secreterNumber == true)
						cout << "The numbers you need are " << thousands << " + " << last3 << endl;
	}
}


However, there seems to be some issues with the logic. When I'm got some time I'll have a further look.


Hit me with a funky number that do be less than 10,000: 1234
Goal number please: 10
The numbers you need are 1 + 2 + 3 + 4
Hit me with a funky number that do be less than 10,000:

Last edited on
Hey!!!! Thank you so much!!! I think I just forgot the bool variable type. Thanks man!
What I need is a program that can check if one number's components can be added together to one another to get another number (the user inputs both numbers).


Based upon this requirement, and not being restricted to numbers < 10,000 consider which will give the lowest number of required digits:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

bool comb(size_t K, const std::vector<size_t>& arr, size_t goal)
{
	std::string bitmask(K, 1);
	bitmask.resize(arr.size(), 0);

	do {
		std::vector<size_t> nos;

		for (size_t i = 0; i < arr.size(); ++i)
			if (bitmask[i])
				nos.push_back(i);

		size_t sum {};

		for (const auto& n : nos)
			sum += arr[n];

		if (sum == goal) {
			for (size_t i = 0; i < nos.size(); ++i)
				std::cout << arr[nos[nos.size() - i - 1]] << ((i < nos.size() - 1) ? " + ": "\n");

			return true;
		}
	} while (std::prev_permutation(bitmask.begin(), bitmask.end()));

	return false;
}

int main()
{
	size_t number1 {}, goalieNumber {};

	std::cout << "Hit me with a funky number: ";
	std::cin >> number1;

	std::cout << "Goal number please: ";
	std::cin >> goalieNumber;

	std::vector<size_t> nos;

	for (; number1 > 0; number1 /= 10)
		nos.push_back(number1 % 10);

	bool got {};

	for (size_t i = 1; i <= nos.size(); ++i)
		if ((got = comb(i, nos, goalieNumber)))
			break;

	if (!got)
		std::cout << "Sorry, doesn't compute\n";
}



Hit me with a funky number: 1234
Goal number please: 10
1 + 2 + 3 + 4

Hit me with a funky number: 1234
Goal number please: 9
2 + 3 + 4

Hit me with a funky number: 12345678
Goal number please: 15
7 + 8

Hit me with a funky number: 123456789
Goal number please: 19
2 + 8 + 9



Last edited on
Topic archived. No new replies allowed.