Daniel Iacob
Daniel Iacob

Reputation: 13

How to get back to switch in c++

Hello I am trying to go back to switch after every case and I can't figure it out. I've tried with return but I can return just to main and it doesn't save the values from v[i]. Here is the code. The code itself should represent how RAM works on 8 bit. Also, v[0] should be between 0 and 19 and v[0]+v[1] should be less than 19 and I don't know how to implement that.

#include < iostream >

using namespace std;

int main() {
    char v[20];
    int pc,
    functie,
    n,
    i,
    loop;
    do {
        cout << "Este pornit calculatorul? (0/1) ";
        cin >> pc;
    }
    while ( pc != 1 );
    cout << "Ce functie selectati? 1-4 ";
    cin >> functie;
    switch (functie) {
    case 1:
        cout << "Citire din memorie" << endl;
        cout << "Cate numere cititi din memorie: ";
        cin >> n;
        for (i = 0; i < n; i++) {
            cout << v[i];
        }
        break;
    case 2:
        cout << "Scriere in memorie" << endl;
        cout << "Cate numere scrieti ";
        cin >> n;
        for (i = 0; i < n; i++) {
            cout << "v[" << i << "]= ";
            cin >> v[i];
        }

        break;
    case 3:
        cout << "Golirea memoriei";
        for (i = 0; i < n; i++)
        v[i] = '\0';
        break;
    case 4:
        cout << "Oprirea calculatorului";
        exit(0);
        break;
    default:
        cout << "Nu ati selectat nici o functie";
    }
    return 0;
}

Upvotes: 1

Views: 873

Answers (3)

Rohan Bari
Rohan Bari

Reputation: 7726

You can use goto to jump at starting point of switch or any function for sake of simplicity. Consider the following code:

loop:
    switch(...) {
        case ...
        break;
    }

    goto loop;

Note that it's not always good to use the goto, it may throw serious problems in programs in some situations. Alternatively, you can do the same with for in a very convenient way. The example:

for(; ;)  
{  
    // body of the for loop.  
}

Hope you understand.

Upvotes: 0

Daniel Iacob
Daniel Iacob

Reputation: 13

Update: I managed to do the code but i still need to have v[0] between 0 and 19 and v[0]+v[1] <=19 and i don't know how to do it.

#include <iostream>

using namespace std;

int main() {
char v[20];
        int pc, functie, n, i,loop;
 do {
                cout << "Este pornit calculatorul? (0/1) ";
                cin >> pc;
        }
        while (pc != 1);

       while(loop=1)
       {
            cout << "Ce functie selectati? 1-4 ";
            cin >> functie;
          switch (functie) {
        case 1:
                cout << "Citire din memorie" << endl;
                cout << "Cate numere cititi din memorie: ";
                cin >> n;
                for (i = 0; i < n; i++)
                    {
                        cout << "v[" << i << "]="<<v[i]<<" ";
                        cout<<endl;
                    }
                break;
        case 2:
                cout << "Scriere in memorie"<<endl;
                cout << "Cate numere scrieti ";
                cin >> n;
                for (i = 0; i < n; i++) {
                        cout << "v[" << i << "]= ";
                        cin >> v[i];
                }
               break;
        case 3:
                cout << "Golirea memoriei";
                for(i=0;i<n;i++)
                    v[i]='\0';
                    cout<<endl;
                break;
        case 4:
                cout << "Oprirea calculatorului";
                exit(0);
                break;
        default:
                cout << "Nu ati selectat nici o functie";
        }
       }


}

Upvotes: 0

Mare Sorin-Alexandru
Mare Sorin-Alexandru

Reputation: 174

The switch should be included in a loop if you want to iterate over it multiple times.

Also you should not generally include vulgar language in your code, even if it is not in English (6th variable you declared).

Upvotes: 2

Related Questions