Is it possible to execute the contents of a switch statement from the bottom-up?

1
2
3
4
5
6
switch (i)
{
    case 1: printf("1");
    case 2: printf("2");
    case 3: printf("3");
}


Is there a way to execute the above switch statement such as when

i = 1, the output is `1`
i = 2, the output is `12`
i = 3, the output is `123`
No simple way that I can see, without making it more than three commands. Sounds like you're trying to push the limits of what case fallthroughs are meant for. At which point, it would be more clear to just use a standard for loop.

1
2
3
4
for (int n = 1; n <= i; n++)
{
	printf("%d", n);
}


Related:
https://stackoverflow.com/questions/188461/switch-statement-fallthrough-should-it-be-allowed
Last edited on
@aumars

Here's one way..

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
int len;
cout << "Type a number, 1 thru 7.."<<endl;
	do
	{
		cout << endl;
		cin >> len;
		for(int x=1;x<=len;x++)
			switch(x)
		{
			case 1: 
				cout << "1";
				break;
			case 2: 
				cout << "2";
				break;
			case 3: 
				cout << "3";
				break;
			case 4: 
				cout << "4";
				break;
			case 5: 
				cout << "5";
				break;
			case 6: 
				cout << "6";
				break;
			case 7: 
				cout << "7";
		}
	}while (len>0 && len < 8);
Are you certain a switch is the right tool for your problem?

This looks like an XY problem ( xyproblem.info ). If this is the case, best ask about your problem rather than your attempted solution.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const string digits = "0123456789";
   int i;
   cout << "Enter i: ";   cin >> i;
   cout << digits.substr( 1, i );
}


I'm inclined to agree with @Ganado and @mbozzi, though:
best ask about your problem rather than your attempted solution.
Last edited on
I'm trying to print three strings in a predetermined order, and the number of strings being printed is i

so

1
2
3
4
5
6
switch (i)
{
    case 1: printf("%s", "A");
    case 2: printf("%s", "B");
    case 3: printf("%s", "C");
}


So the resulting string is either A, AB, or ABC.

I apologise for the confusion, this has nothing to do with printing numbers, and everything to do with printing strings in a particular order.
Last edited on
Do we actually care what is printed? No. You want something printed "in order".

You can do that with a switch: EDIT: not easily.
1
2
3
4
5
6
7
using std::cout;
switch (i)
{
    case 3: cout << "A";
    case 2: cout << "B";
    case 1: cout << "C";
}


However, a loop is good too:
1
2
3
4
5
std::vector<std::string> words { "A", "B", "C" };

for ( int w=0; w < i and w < words.size(); ++w ) {
  std::cout << words[w];
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>

int main()
{
    puts( "enter a number in [1,9]: " ) ;
    unsigned int i = 0 ;
    scanf( "%u", &i ) ;

    char text[] = "123456789" ;
    if( i < 9 ) text[i] = 0 ;
    puts(text) ;
}
I solved the issue using std::deque:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <deque>
#include <string>

std::deque<std::string> d;
int i;

switch (i)
{
    case 3: d.push_front("C");
    case 2: d.push_front("B");
    case 1: d.push_front("A");
}

for (auto &it: d) std::cout << it;
Last edited on
Does a recursive function meet your idea, like this:

string func(int n) {
return n>0 ? func(n-1)+char('A'+n-1) : "";
}
OP is working with an XY problem.
Registered users can post here. Sign in or register to post.