Go back to a certain part of the code.

I'm trying to create a program where you have the option to calculate 5 different things, and I'm struggling with the option to go back to the "menu". It starts with the said "menu" where you can choose one of 5 options, and after it calculates the certain thing, I want the user to choose to either go back to the menu or repeat the calculating process. I tried using "while" and "do...while" but it either ends up glitching or repeats just the calculation without being able to 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
#include <iostream>

using namespace std;
float ak,pk, ap,bp,pp,pdt,ht,pt,atp,btp,htp,ptp,e,f,pr;
string input;
int main()
{
    cout << "Witaj uzytkowniku !" << endl;
    cout << "Wybierz pole jakiej figury chcesz obliczyc :" <<endl;
    cout << "1. Kwadrat | 2. Prostokat | 3. Trojkat | 4. Trapez | 5. Romb |" <<endl;
    cout << "Wpisanie 6 poprawnie zakonczy dzialanie programu."<<endl;
    cout << "Wpisz sama cyrfre BEZ KROPKI i zatwierdz enterem." <<endl;
    cin >> input;

    {

        if (input=="1")
        {
            cout << "Podaj bok kwadratu:"<<endl;
            cin >> ak;
            pk=ak*ak;
            cout << "Pole kwadratu o podanym boku wynosi : "<<pk<<endl;
            cin >> lmao;
        }
         if (input=="2")
        {
            cout << "Podaj jeden z bokow prostokata:"<<endl;
            cin >> ap;
            cout << "Podaj drugi bok prostokata:"<<endl;
            cin >> bp;
            pp=ap*bp;
            cout << "Pole prostokata o podanych bokach wynosi : "<<pp<<endl;
        }
         if (input=="3")
        {
            cout << "Podaj podstawe trojkata:"<<endl;
            cin >> pdt;
            cout << "Podaj wysokosc trojkata:"<<endl;
            cin >> ht;
            pt=(pdt*ht)/2;
            cout << "Pole trojkata o podanych wymiarach wynosi : "<<pt<<endl;
        }
         if (input=="4")
        {
            cout << "Podaj pierwsza podstawe trapezu:"<<endl;
            cin >> atp;
            cout << "Podaj druga podstawe trapezu:"<<endl;
            cin >>btp;
            cout << "Podaj wysokosc trapezu:"<<endl;
            cin >> htp;
            ptp=((atp+btp)*htp)/2;
            cout << "Pole trapezu o podanych wymiarach wynosi : "<<ptp<<endl;
        }
         if (input=="5")
        {
            cout << "Podaj jedna z przekatnych rombu :"<<endl;
            cin >> e;
            cout << "Podaj druga przekatna rombu :"<<endl;
            cin >> f;
            pr=(e*f)/2;
            cout << "Pole rombu o podanych przekatnych wynosi : "<<pr<<endl;
        }
    }
    if (input=="6")
    {
        cout << "Dziekuje za skorzystanie z programu !"<<endl;
        return 0;
    }
}
cout is mostly in Polish, but that's just the text for the user so it doesnt matter. I also got rid of my not working while loop but it was right under cin>>input;. I also didn't include the user's choice code but I believe I could handle that.
Last edited on


1
2
3
4
5
6
7
while (keepGoing)
{
  cout << "Menu";
  cin >> userChoice;

  // do something with user choice
}
Hello D3V1LL0,

Welcome to the forum.

Please post what you have done. Not knowing this it is only guess at what is wrong. And anything I can offer is only a guess at what might work.

I you do not know read the following links:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
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.

Andy
@Repeater
While (keepGoing) doesnt seem to be recognized by the program. But when I tried putting the "while" before the menu the program instantly returns the value and stops.

@Handy Andy
I'm sorry for not including the code.I did it now. I'm really fresh to C++ so I'm glad to be a part of this community ! The forums are really helpful and I've already found some solutions to my other problems (reading other people's posts).
Yes. It was more of an example really. keepGoing is a boolean value that you set to true to begin with, and set to false when you want to stop looping.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool keepGoing = true;
while (keepGoing)
{
  cout << "1. Keep looping" << '\n'
         << "2. Stop" << endl;
  
  int input = 0;
  cin >> input;
 
  if (input == 1)
  {
     // we'll keep going
  }
  
  if (input == 2)
  {
    keepGoing = false;
  }
}

Last edited on
@Repeater Thanks a lot ! That was exactly what I was looking for !
Topic archived. No new replies allowed.