Ok.. I have a dumb question -.-

thank u
Last edited on
Do both tasks one after another. Take the contents of the main function of each and put them into the third, "combined" main function.

1
2
3
4
int main () { 
  // program 1's code here
  // program 2's code here
}

If you choose, you can introduce a new lexical scope so that both independent programs get their own "num" variable and you don't have to worry about name collisions (in other words, you can guarantee it compiles on the first try) by writing
1
2
3
4
int main () { 
  { /* program 1's code here */ }
  { /* program 2's code here */ }
}


You'll have to remove the explicit return statement from the bottom of the main function in the first program you put in.
Last edited on
@mbozzi

then the switch statement doesnt work in my second program... i need the numbers in lines of 10 values each , like the first program.

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
70

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int num = 2;

	do{
		cout << num << " ";
		num += 2;

		switch (num)
		{
		case 22:
			cout << endl;
			break;

		case 42:
			cout << endl;
			break;

		case 62:
			cout << endl;
			break;

		case 82:
			cout << endl;
			break;
		}
	} 
	while ((num <= 100) && ( num >= 2));
	cout << endl;
	cout << endl;
	cout << endl;
	cout << endl;
	
	
	



	
	
	int val = 99;
	do{
		cout << val << " ";
		val -= 3;
		switch (num)
		{
		case 9:
			cout << endl;
			break;

		case 39:
			cout << endl;
			break;

		case 69:
			cout << endl;
			break;
		}
	} 
	while ((val <= 99) && (val >= 3));
	cout << endl;
	system("pause");
	return 0;
}
To fix your program you have to re-initialize the num variable before you start with the second task.

add line 45: num = 2; // reset the num variable bef

Alternatively: use the lexical scopes mentioned by mbozzi so that the second task gets its own num variable.
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    {
        int num = 2;

        do{
            cout << num << " ";
            num += 2;

            switch (num)
            {
            case 22:
                cout << endl;
                break;

            case 42:
                cout << endl;
                break;

            case 62:
                cout << endl;
                break;

            case 82:
                cout << endl;
                break;
            }
        }
        while ((num <= 100) && ( num >= 2));
        cout << endl;
        cout << endl;
        cout << endl;
        cout << endl;
    }

    {
        int num = 2;
        int val = 99;
        do{
            cout << val << " ";
            val -= 3;
            switch (num)
            {
            case 9:
                cout << endl;
                break;

            case 39:
                cout << endl;
                break;

            case 69:
                cout << endl;
                break;
            }
        }
        while ((val <= 99) && (val >= 3));
        cout << endl;
    }
	system("pause");
	return 0;
}


If you would like to use functions like WheatFieldOnFire suggested you could simplify your code to something like this:
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
#include <iostream>
#include <iomanip>

using namespace std;

void printTenEvenNumbersInARow(int startValue, int stopValue)
{
    for (int num = startValue; num <= stopValue; num = num+2)
    {
        cout << num << " ";
        if (num>0 && num%20==0) cout << endl;
    }
}

void reversePrintMultiplesOfThree(int startValue, int stopValue)
{
    for (int num = startValue; num > stopValue; num = num-3)
    {
        cout << num << " ";
        if (num>0 && num%30==9) cout << endl;
    }
}

int main()
{
    printTenEvenNumbersInARow(0,100);
    cout << "\n-------\n";
    reversePrintMultiplesOfThree(99,0);
	system("pause");
	return 0;
}


Personally I would use functions and try to generalize them a bit. This would result in something like:
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
#include <iostream>

using namespace std;

void printNumbersInRow(int startValue, int stopValue, int stepSize, int numberOfValuesInSingleRow)
{
    int numberOfValuesToOutput = 0;
    int step;
    if (startValue > stopValue)
    {
        step = 0 - stepSize;                                              // in this case we should count backwards
        numberOfValuesToOutput = (startValue-stopValue)/stepSize;
    }
    else
    {
        step = stepSize;
        numberOfValuesToOutput = (stopValue-startValue)/step;
    }
    for (int counter = 0; counter < numberOfValuesToOutput; counter++)  // this loop now counts up or down depending on what we want
    {
        if (counter > 0 && counter%numberOfValuesInSingleRow==0) cout << endl;                 // after every tenth value, we go to a new line
        if (startValue % stepSize == 0) cout << startValue << " ";      // output the desired values
        startValue = startValue + step;
    }
    cout << endl;
}

int main()
{
    printNumbersInRow(0,100, 2, 10);
    cout << "-------\n";
    printNumbersInRow(99,0, 3, 5);
    system("pause");
    return 0;
}


Using functions is typically a good idea if you have to do things multiple times (re-use your code) and/or if you want to separate the code you use for separate tasks.
Often to make a function more general usable (more re-usable) makes the function itself a bit more difficult, so note that while you are learning this is something to think about but not something that is important. It is far more important that you properly understand the code that you write.
Last edited on
Topic archived. No new replies allowed.