What should I use instead of goto?

Hi, what should I use, instead of goto? I read some about functions and loops, but can't see any simple solution for 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

int main()

{
 int i,j;

 function:

 cout<<"how many numbers? ";
 cin>>i;
 cout<<"\n Are you sure you want "<<i<<" numbers?\n 1=yes; 2=no ";   
 cin>>j;

 if(j<2)

    {
 
    int tablica[i];    
  
    
    int k;
       for (k=0; k<i; k++)
          {
           cout<<"enter "<< k <<". number of array ";
           cin>>tablica[k];
           cout<<k<< ". number= "<<tablica[k]<<" \n";
          }
    }
    
    else goto function;
    {}
     





 cin.get();
 system("pause"); 

}
Last edited on
1
2
3
4
5
6
7
8
9
    int i,j;

    do {
        cout << "how many numbers? ";
        cin >> i;
        cout << "\n Are you sure you want " << i
             << " numbers?\n 1=yes; 2=no ";
        cin >> j;
    } while (j != 1);
Last edited on
Instead of
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function:

cout<<"how many numbers? ";
cin>>i;
cout<<"\n Are you sure you want "<<i<<" numbers?\n 1=yes; 2=no ";   
cin>>j;

if(j<2)
{
	int tablica[i];    


	int k;
	for (k=0; k<i; k++)
	{
		cout<<"enter "<< k <<". number of array ";
		cin>>tablica[k];
		cout<<k<< ". number= "<<tablica[k]<<" \n";
	}
}
else goto function;
{}
you can do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do
{
	cout<<"how many numbers? ";
	cin>>i;
	cout<<"\n Are you sure you want "<<i<<" numbers?\n 1=yes; 2=no ";   
	cin>>j;
} while (j>=2);

int tablica[i];

int k;
for (k=0; k<i; k++)
{
	cout<<"enter "<< k <<". number of array ";
	cin>>tablica[k];
	cout<<k<< ". number= "<<tablica[k]<<" \n";
}
.
thank you both :) works fine
closed account (zb0S216C)
...or use a function ( "function" leads me to believe you want a function ):

1
2
3
4
5
6
7
8
9
10
11
12
13
int /* assumed */ Function( /* Params */ )
  {
    // ...
  }

int main( )
  {
    // ...

    ::Function( /* Args */ );

    // ...
  }

Wazzak
Last edited on
Framework, i've read some about functions, will try to use them later.

Got another problem now. Should I continue writing the program in 'while' loop, or should I exit it somehow?

And another one: after executing code below, i've got some strange numerical values before last cout's. Why?

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
#include <iostream>

using namespace std;

int main()

{
 int i,j;

 do
 {
 cout<<"how many numbers? ";
 cin>>i;
 cout<<"\n Are you sure you want "<<i<<" numbers?\n 1=yes; 2=no ";   
 cin>>j;
 }
 while(j>=2);


    
 
 int array[i];    
  
    
    int k=0, l=0;
    
	       while(k<i)
  	       {
           cout<<"enter "<< k <<". number of array ";
           cin>>array[k];
           k++;
		   }
		   
		   while(l<i)  
		   
		   {
	       cout.fill('.');
		   cout.width(30);
		   cout<<l<< ". number= "<<array[l]<<" \n\n";
		   l++;
	       }
	  
cout<<"variable k is equal to k= "<<k<<"\n\n"<<	  
cout<<"variable l is equal to l= "<<l<<"\n\n"<<	       
cout<<"array has "<<i<<" elements \n\n"<<
cout<<"sum of all elements is equal to S= \n"<< //will work on this now

		   
cout<<"variable k is equal to k= "<<k<<"\n\n"<<
		   

 cin.get();
 system("pause"); 

}





Should DevC++ align brackets and text in every new loop etc...? It doesn't :/
Last edited on
i've got some strange numerical values before last cout's. Why?

Rather then ending a line of code with a semicolon, a trailing << is used. That in itself is ok -but if you do that, there's no need to type "cout <<" again afterwards.

The unexpected values are the address of the cout stream itself.

Regarding alignment of text, brackets etc. in the editor, I'd just regard it like the "autocorrect" feature in a word processor. Sometimes it will do what you want, other times it fights against you. Ultimately the responsibility is with the programmer to align the code, rather than depend on automation.

Another comment on formatting, it is probably easier to read the code if a space is left before and after operators such as <<.
Here's my formatting of your code - and the cout problem corrected.

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
#include <iostream>

using namespace std;

int main()

{
    int i,j;

    do
    {
        cout << "how many numbers? ";
        cin >> i;
        cout << "\n Are you sure you want " << i << " numbers?\n 1=yes; 2=no ";   
        cin >> j;
    }
    while (j>=2);

    int array[i];    
    int k=0, l=0;

    while (k<i)
    {
        cout << "enter " <<  k  << ". number of array ";
        cin >> array[k];
        k++;
    }

    while (l<i)  
    {
        cout.fill('.');
        cout.width(30);
        cout << l <<  ". number= " << array[l] << " \n\n";
        l++;
    }

    cout << "variable k is equal to k= " << k << "\n\n" 
         << "variable l is equal to l= " << l << "\n\n" 
         << "array has " << i << " elements \n\n" 
         << "sum of all elements is equal to S= \n"  
         << "variable k is equal to k= " << k << "\n\n"; 

    cin.get();
    system("pause"); 

}
Last edited on
Thanks for hints Chervil.

What about writing the program in a one, big loop, as in the code above?

I've tried to do the same thing by creating menu and using functions (code below), but got some problems here. I want to go back to main menu after executing each Case of the switch. How? I've tried to go directly to main function, but it doesn't seem to work well.

Also got a problem with second case, connected with initalizing of the array i'm using. I've initialized it in function HMANY, and want to use it in function SHOWNUMBERS. Is it possible? Or is it some local array or sth...?

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>

using namespace std;

int hmany(int numbers);
int shownumbers(int m);
int i=0, k=0, l=0, m=0;

//*******************************

int main()
{
 	
 	int choice;
 	cout <<"My first menu, make your choice\n"
 		 <<"1. Choose how many numbers\n"
 		 <<"2. Show chosen numbers\n"
 		 <<"3. Check size of array\n"
 		 <<"4. Calculate sum of elements\n"
		 <<"5. Which element is the highest one?\n";
		 
 	cin >> choice;

 	switch(choice)
 	{
  	         case 1:
		           cout << "1. Choose how many numbers ";
			   cin >> i;
	                   hmany(i);
			    main();
				  	   
				  	   
				  	   
                  case 2:
                  	   cout << "2. Show chosen numbers\n";
                  	   shownumbers(i);
                  	   break; 
	}


cin.get();
system("pause");
}


//******************** case1 function

int hmany(int numbers)
{
 	cout << numbers << " numbers chosen\n";
 	
    int array[numbers];    
    
        
	       while(k<i)
  	       {
           cout<<"enter "<< k <<". number of array ";
           cin>>array[k];
           k++;
	      }
}

//**************** case 2 function
int shownumbers(int m)
 {
     	 
           while(m<i)
	       cout<<m<< ". number= "<<array[m]<<" \n\n";
		   m++;
 }
Tax wrote:
What about writing the program in a one, big loop, as in the code above?

I've tried to do the same thing by creating menu and using functions (code below), but got some problems here. I want to go back to main menu after executing each Case of the switch. How? I've tried to go directly to main function, but it doesn't seem to work well.

You should never attempt to call function main(). Although some compilers may accept this, in the ISO C++ standard it is not permitted, and some compilers will reject it as an error.

The answer is to simply use a loop, most often a while loop. You just need to consider some way to exit from the loop, for example as option 0 on the menu.


Also got a problem with second case, connected with initalizing of the array i'm using. I've initialized it in function HMANY, and want to use it in function SHOWNUMBERS. Is it possible? Or is it some local array or sth...?

The array is indeed local to that function, and is no longer available after the function has ended.

One solution is to use a global variable for the array. Another is define it in function main() and to pass it as a parameter to the other functions.

Either way, there is a problem, the size of the array is dynamically chosen depending upon the user input. To deal with this, you'd need to use the new and delete operators. I'll illustrate this below.

Another way to deal with an array of variable size, and generally considered the better option, is to use a vector.
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>

using namespace std;

void hmany(int * &array, int howmany);
void shownumbers(int * array, int howmany);

//*******************************

int main()
{
    int * array = 0;
    int howmany = 0;

    int choice;

    do
    {
        cout << "My first menu, make your choice\n"
             << "1. Choose how many numbers\n"
             << "2. Show chosen numbers\n"
             << "3. Check size of array\n"
             << "4. Calculate sum of elements\n"
             << "5. Which element is the highest one?\n"
             << "0. Exit the program\n";

        cin >> choice;

        switch (choice)
        {
            case 1:
                cout << "1. Choose how many numbers ";
                cin >> howmany;
                hmany(array, howmany);
                break;

            case 2:
                cout << "2. Show chosen numbers\n";
                shownumbers(array, howmany);
                break;

            case 3:
            case 4:
            case 5:
                cout << "Not yet implemented.\n";
                break;

            case 0:
                cout << "Exiting...\n";
                break;

            default:
                cout << "invalid option. Please try again\n";

        }

    } while (choice != 0);

    delete [] array;

    cin.get();
    system("pause");
}


//******************** case1 function

void hmany(int * &array, int howmany)
{
    cout << howmany << " numbers chosen\n";

    if (array != 0)
        delete [] array;

    array = new int[howmany];

    for (int k=0; k<howmany; k++)
    {
        cout << "enter " << k <<". number of array ";
        cin >> array[k];
    }
}

//**************** case 2 function
void shownumbers(int * array, int howmany)
{
    for (int m=0; m<howmany; m++)
        cout << m << ". number= " << array[m] << " \n\n";
}
Now a version using a vector, which can be considered as an array which can vary in size as required.
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <vector>

using namespace std;

void hmany(vector<int> &array, int howmany);
void shownumbers(const vector<int> &array);

//*******************************

int main()
{
    vector<int> array;

    int howmany;
    int choice;

    do
    {
        cout << "My first menu, make your choice\n"
             << "1. Choose how many numbers\n"
             << "2. Show chosen numbers\n"
             << "3. Check size of array\n"
             << "4. Calculate sum of elements\n"
             << "5. Which element is the highest one?\n"
             << "0. Exit the program\n";

        cin >> choice;

        switch (choice)
        {
            case 1:
                cout << "1. Choose how many numbers ";
                cin >> howmany;
                hmany(array, howmany);
                break;

            case 2:
                cout << "2. Show chosen numbers\n";
                shownumbers(array);
                break;

            case 3:
            case 4:
            case 5:
                cout << "Not yet implemented.\n";
                break;

            case 0:
                cout << "Exiting...\n";
                break;

            default:
                cout << "invalid option. Please try again\n";

        }

    } while (choice != 0);


    cin.get();
    system("pause");
}


//******************** case1 function

void hmany(vector<int> &array, int howmany)
{
    cout << howmany << " numbers chosen\n";

    array.clear();
    int number;

    for (int k=0; k<howmany; k++)
    {
        cout << "enter " << k <<". number of array ";
        cin >> number;
        array.push_back(number);
    }
}

//**************** case 2 function
void shownumbers(const vector<int> &array)
{
    for (unsigned int m=0; m<array.size(); m++)
        cout << m << ". number= " << array[m] << " \n\n";
}
Topic archived. No new replies allowed.