A fish like Tuna

Pages: 12
But you are not using a function
I need something to connect back the function to the int mainn
I understand How to use for loops for menus But this time i need that function to connect back to the main menu i made

Lets say I call it after i call it i need go back
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
#include <iostream>
#include <stdlib.h>
using namespace std;

void choice1(),choice2(),choice3();

int main()
{
    b:

    char a;
    cout<<"Choose a topic:\n[A]. topic 1\n[B]. topic 2\n[C]. topic 3"<<endl;
    cin>>a;

    if (a=='a'||a=='A')
    {
        choice1();
goto b;
    }else
    if (a=='b'||a=='B')
    {
        choice2();
    } else
    if (a=='c'||a=='C')
    {
        choice3();
    }else
    {
        system("cls");
        cout<<"Invalid Input Try AGAIN"<<endl;
        goto b;
    }

    return 0;
}
void choice1()
{
    char shape;
    cout<<"What shape do you want to get the area?\n[A]. Triangle\n[B]. Square\n[C]. Rectangle\n[D]. Cube "<<endl;
    cin>>shape;

    if(shape=='A'||shape=='a')
    {

        double tbase,theight,tx;
        cout<<"\t\tTRIANGLE"<<endl;
        cout<<"Formula: Area of triangle = (Base * Height)/2"<<endl;
        cout<<"Input the value of BASE:";
		cin>>tbase;
		cout<<"Input the value of HEIGHT:";
		cin>>theight;
		tx=(tbase*theight)/2;
		cout <<"The area of TRIANGLE = "<<tx<<endl;
		cout<<"\n Back to Main Menu!"<<endl;


    }else
    if(shape=='B'||shape=='b')
    {

    }else
    if(shape=='C'||shape=='D')
    {

    }else
    if(shape=='D'||shape=='d')
    {

    }else

    {

    }


}

void choice2()
{
    cout<<"Topic1;';";

}
void choice3()
{
    cout<<"\topic";
}





OKKKKK I FIGURED IT OUT TNX ALOT BUT THE GOTO SAVE THE DAY


hope you have a look to the code and id be happy if yall run it SO finally you understand what im saying....


TNX everyone THankyou !
STOP USING GOTO.

STOP USING GOTO.

We showed you explicitly how to use loops. If whoever marks your work has any respect for you, this would be a fail with a big "USE LOOPS" written on it.
ITTTT WAS WORKING BRUH , WE instructed to use functions , I could use loops but for now we need to useeeeeeeeeeeeeeee FUNCTION and FOR NOW ALL i need is to CONNECT IT BACK TO INT MAIN after it run the FUNTION OF the user CHOICE , SO whats wrong of having a goto??
Last edited on
You say stop usng goto and it workkk why dont you run the program and try to understanddddd what im saying
i know im not that good Buttttt I fix it and USING the GOTO ,so whats wrong if i use it
Because you'll end up creating monstrosities of code filled with goto statements instead of proper control flow. Your code will be impossible to read, jumping about from one place to another with no way for the reader to know how the code could reach different places. The lack of structure will make the code a soup, in which simply touching it becomes dangerous with effects impossible to predict. Other people will write well-structured code that is easy to understand, easy to fix, easy to build upon and create ever greater programs, while your code will collapse under the weight of its own incomprehensibility and you'll never be able to write any program that isn't super simple. This is the price of choosing to use goto instead of using proper control structures.
Last edited on
Here, without goto.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
do {
    char a;
    cout<<"Choose a topic:\n[A]. topic 1\n[B]. topic 2\n[C]. topic 3"<<endl;
    cin>>a;

    if (a=='a'||a=='A')
    {
        choice1();
    }
    else if (a=='b'||a=='B')
    {
        choice2();
    }
    else if (a=='c'||a=='C')
    {
        choice3();
    }
    else
    {
        system("cls");
        cout<<"Invalid Input Try AGAIN"<<endl;
    }
} while ( true );


goto's are an escape hatch of last resort.

Backward goto's are especially sinful as what you're doing is basically implementing a loop, for which there are plenty of sane programming constructs to do the same.

In some C code, goto's are used for if ( error ) goto end; type code, but that's usually a forward jump to the cleanup code of a function. If used consistently, it's easy to understand and follow.

In C++, the need for that is less, because objects have destructors which are called automatically when going out of scope, and try/catch is a far better way to handle errors and cleanup.

I could use loops but for now we need to useeeeeeeeeeeeeeee FUNCTION


Do you know what a function is? Do you know a loop is? You can have both at the same time. salem above has written code that uses function AND has a loop.

Also, you spelled use incorrectly. It only has one 'e'. "Use". Just one 'e'.
Last edited on
Ive making it to be user friendly, and sorry i found my way to make it WITH the GOTO, and Besides i dont need the user to understand the code , I have my own explanation for the code and own logic , have You already try to run it?? it run bruh

Again thank you for the help !

It seems that Repeater's example was way too complex. Lets strip it to the essentials:
1
2
3
4
5
6
7
8
9
int main()
{
    while ( condition )
    {
       // Here, do something. Triangles and so forth.
       
      // update condition
    }
}

Just in case you fail to see it, I'll repeat it:
1
2
3
4
5
6
7
8
9
int main()
{
    while ( condition )
    {
       goodchoice();
       
      // update condition
    }
}

If you can call functions when you have 'goto', then surely you can call functions when you have a loop. Repeater did not say: "don't use functions". He did say: "use loop to call functions", repeatedly.


The "What is good or bad in goto?" is a very advanced topic. It is hard even for us to comprehend or explain. We do know that with loops we can write correct, clear, and maintainable code with much less effort.


1
2
3
4
5
6
#include <iostream>

int main() {
  std::cout << "Hello world\n";
  return 0;
}

The line 4's std::cout << "Hello world\n"; contains function call.
After the function call the execution returns to main() and next statement (return 0;) is executed.
You shout "CONNECT TO", but that is not what you mean. You want to repeat some statements that are in the main(). A function call is just a statement.
1
2
3
4
5
6
7
8
#include <iostream>

int main() {
  for (int times=0; times<5; ++times ) {
    std::cout << "Hello world\n";
  }
  return 0;
}

The line 5's std::cout << "Hello world\n"; is still a function call.
After the function call the execution returns to main() but the next statement is the loop's condition on line 4.
Hello Feedest,

OKKKKK I FIGURED IT OUT TNX ALOT BUT THE GOTO SAVE THE DAY
. It could also cost you a job.

You say stop usng goto and it workkk why dont you run the program and try to understanddddd what im saying

Those who know do not have to run the program They already know what it will do and what is wrong. I believe that those who have responded understand what you are saying. I feel that you are not understanding what they are saying.

WE instructed to use functions

You say that you need to use functions you are. I do not believe you are understanding them correctly.

FOR NOW ALL i need is to CONNECT IT BACK TO INT MAIN after it run the FUNTION

Using a do/while or while loop in main does not take away from the functions you are using. But it does put what is in "main" in a loop to keep the program running. You do not need to restart "main" every time you return from a function.

Your program without the "goto" statements:
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
#include <cctype>
#include <iostream>
#include <stdlib.h>
using namespace std;

void choice1();
void choice2();
void choice3();

int main()
{
	char choice;
	
	do
	{
		cout << "\n Choose a topic:\n [A]. topic 1\n [B]. topic 2\n [C]. topic 3\n [D]. Exit\n Your choice: ";
		cin >> choice;

		if (std::toupper(choice) == 'A')
		{
			choice1();  // <--- Function called;

		}
		else if (choice == 'b' || choice == 'B')  // <--- Function returns here for choice "A".
		{
			choice2();
		}
                // <--- Function returns here for choice "B".
		else if (std::toupper(choice) == 'D')
			continue;
	} while (std::toupper(choice) != 'D');

	return 0;
}
void choice1()
{
	char shape;
	cout << "\n What shape do you want to get the area?\n [A]. Triangle\n [B]. Square\n [C]. Rectangle\n [D]. Cube\n Your choice: ";
	cin >> shape;

// <--- The rest of your code would be here.
}  // <--- Returns to main or to where it was called from. 

I changed line 16 a bit and added lines 28 and 29. Otherwise the program is unchanged from what you started with.

Hope that clears things up,

Andy

Edit: typo
Last edited on
STOP USING GOTO.

This quote is only a single example -- the louder the tonality the truer the truth (field-tested).

Then, abut GoTo in high level languages (C++ may be one) -- GoTo exist, contraceptives exist, but Pope interdicts to use them. Now how will you do with GoTo?
i dont need the user to understand the code


You're not writing code for the USER to understand. To think you are is a fundamental mistake.

Right now, you are expected to be bad at this. So that's fine. However, you are expected to get better at it; if you choose not to get better at it, you choose to be a terrible programmer. The choice is yours.

Maybe you actually want to go into something else entirely. Maybe you're a history student and this is a compulsory programming course and you'll never touch it again, in which case have fun and enjoy the rest of your studies.

If you actually have intentions of doing more or this, if you can't get it right at this super simple, very basic level, how will you possibly get it right when the code is thousands of times more complicated? This is your opportunity to learn things; you won't have the opportunity to learn basic programming later; you'll just get fired.

The choice is yours. If you actually want to be good at this, put your ego back in the box. If you don't want to be good at this, carry on.
Last edited on
@Repeater,

Good point.

Andy
Topic archived. No new replies allowed.
Pages: 12