A fish like Tuna

Pages: 12
Heyy is there something a syntax i can use for this

Im using function... thenn let say the program inside the function was done ,what syntax can i use to go back to int main()??????
id try goto b; and b: was inside to int main() and it doesnt help


so if you know some plss helppp TNX

Last edited on
it didnt help still exit the program

well Im making a menu like A B C

if i choose A it connects me the function and the function holds the program SOOOO
i want the user to have a choice to go back to main menuu i dont know how to connect or have a link to the global to local( Int main() )


Plssss helppp tnxx
Urgent is not a topic title.
http://www.catb.org/esr/faqs/smart-questions.html#urgent

> if i choose A it connects me the function and the function holds the program
What does "holds the program" mean?

Post some actual code, ideally a small demonstration of the problem you're facing.

dfsagsagaga
Last edited on
i dont know how to post code properly but pls help me
Function holds the progam

its jsut like theee code for the certain topic is inside the function
I dontt know how to usee or the right term for that

Hello Feedest,

Im using function... thenn let say the program inside the function was done ,what syntax can i use to go back to int main()??????

Nothing. When a function it returns to where it was called from. If the function was called from "main" then it will return to main, but to the code just after the function call.

I have not rad this in awhile, but you might want to look at: http://www.cplusplus.com/doc/tutorial/functions/

As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Foo(std::string& msg);

int main()
{
	std::string msg;

	Foo(msg);

	std::cout << "\n " << msg << std::endl;

	return 0;
}

void Foo(std::string& msg)
{
	msg = "Left function";
}

If I am understanding correctly on line 7 when the function is called the address of line 9 is pushed on the stack. When the function reaches the closing brace } it pops the address off the stack and returns back to line 9.

This is basic and I may not have it 100% correct, but it should give you an idea.

"main" is only called once when the program starts and you should never call "main" in a program. There are better and other ways to get back to main.

Using a goto statement is a bad idea to start with. I do not think it will work from a function because the label is in "main" and when you call a function "main" looses scope in favor of the function, so in the function when you use the goto statement it can not find the label in "main" because "main" is out of scope.

Using a goto statement can cause problems and its use should be very rare.

You are having problems with your code, so post what you have so everyone can see what you are trying to do. It is easier to comment on or correct what you have done rather than guess at what you might have done.

Hope that helps,

Andy
Hello Feedest,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Ok tnx

Soo what should I do?

I need to connect it back to the int main()

How to post code correctly??? Im just copy pasting
HOW?
____ [code] then code here???
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();

    }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";
}
COOOOOOOOOOOOOOOOOOOl i know nOW Thank you!
1) You should never use goto.

One day there may come a case for it. But right now, you should never use goto.

2) Looks like you want to learn about loops. There are while loop, do-while loops, and for loops. You need to learn about them all.

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;

int main()
{
    bool topic_has_been_chosen = false;
   char user_input;

    while (topic_has_been_chosen == false)
    {
       cout<<"Choose a topic:\n[A]. topic 1\n[B]. topic 2\n[C]. topic 3"<<endl;
       cin>>user_input;

       // This set of conditions is a bit awkward and clumsy, but it's easy to understand
       if ( user_input != 'a'   &&
           user_input != 'A'   &&
           user_input != 'b'   &&
           user_input != 'B'   &&
           user_input != 'c'   &&
           user_input != 'C' )
       {
          topic_has_been_chosen = false; // not necessary but instructive for reader
          std::cout << "Bad choice. \n";
       }
       else
       {
          topic_has_been_chosen = true;   
       }
          
    }
     
     cout << "You chose " << user_input;
}


https://onlinegdb.com/rkPoj73sV
Last edited on
Prof said we need to use function hahahahaha
Hello Feedest,

Line 5: void choice1(),choice2(),choice3(); I did not think this would work, but it seems to. Generally I see a prototype for a function on separate lines.

I see that you have removed the "continue". "continue" works in for loops,do/while loops and while loops to pass over code to get back to the condition of the loop.

If the format you are using for the else/if statements works for you that is fine, but I most often see them written as:
1
2
3
4
else if (a == 'b' || a == 'B')
{
	choice2();
}

The compiler does not care about the amount ofwhite space between the "else" and the "if".

The goto in "main" can be replaced with a do/while loop.I would add a forth choice to the menu for "Exit" and the while condition might be while (std::toupper(a)!='D');. The "std::toupper()" comes from the header file "cctype". Just a thought. This way you will stay in the loop until you choose "D" and the loop ends. this way you can replace the goto that you are using. The goto may appear to be easy, but you are not using it the way you should.

Hints/suggestions: Use better names for variables and functions. The names make no difference to the compiler, but it does make it easier for someone reading your code. "cin >>a" may work, but "cin >> choice" makes more sense when reading. Someone once said, sorry I do not recall who at the moment, a variable name should be a noun to that I add it should describe what the variable is or does. The same applies to function names. "AreaOfaShape" makes more sense than "choice1". Just little things, but you will find it useful in the future.

Hope that helps,

Andy
Prof said we need to use function


You are using functions. You've got lots of functions in your code. choice1, choice2 and choice3 are all functions.
the reall problem is Theres a way i can connect back to int main after on this part done

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
}




lets say i chose to get the area of the triangle I WANT THE USER TO have A chance to go back to main menu on the code here it is

MAIN MENU
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
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();

    }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;
}




Last edited on
Same thing again. Loops. Same thing we just showed you. We just showed you this. We just showed it to you. Same thing again.


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

using namespace std;

int main()
{
    
    bool user_want_to_end_program = false;
    
    while (user_want_to_end_program == false)
    {
      bool topic_has_been_chosen = false;
      char user_input;

      while (topic_has_been_chosen == false)
      {
        cout<<"Choose a topic:\n[A]. topic 1\n[B]. topic 2\n[C]. topic 3"<<endl;
        cin>>user_input;

        if ( user_input != 'a'   &&
             user_input != 'A'   &&
             user_input != 'b'   &&
             user_input != 'B'   &&
             user_input != 'c'   &&
             user_input != 'C' )
        {
          topic_has_been_chosen = false;
          std::cout << "Bad choice. \n";
        }
        else
        {
          topic_has_been_chosen = true;   
        }
          
      }
     
       cout << "You chose " << user_input << '\n';  
       cout << "Here, do something with that choice. Triangles and so forth. '\n'";
       
       
       char do_another;
       cout << "Want to do another shape? Press 'y' for yes, anything else for no\n";
       cin >> do_another;
       if (do_another != 'y')
       {
           user_want_to_end_program = true;
       }
    }
}


https://onlinegdb.com/H1cpgE3oV
Pages: 12