Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what is the problem of my code?

+6 votes
asked Nov 25 by Tan, Henscyll Marf (180 points)
#include<iostream>
using namespace std;

int main()
{
    int h;
    char choice;
    do
    {
        cout<<"Semi-Final Menu\n";
        cout<<"  [1] Assignment3 - Even or Odd Number\n";
        cout<<"  [2] Seatwork3- Fibonacci Sequence\n";
        cout<<"  [3] Quiz4 - Box Pattern"<<"\n\n";
        cout<<"Please enter your choice: ";
        cin>>h;
        switch(h)
        {
        
        case 1:
        int n;
            cout<<"\nYou picked Assignment3 - Even or Odd.\n";
            cout<<"\nPlease enter any number between 10 and 30: ";
            cin>>n;
        
            while(n < 10 || n > 30){
                cout<<"\nInvalid input. Please enter a number again."<<endl;
                cout<<"\nPlease enter any number between 10 and 30: ";
                cin>>n;
            }             
            if(n > 10 || n < 30){
                cout<<n<<" is an odd number"<<endl;
            }
            cout<<"\nAssignment3 - Semi Final has ended";
            break;
        
        case 2:
            int t,f = 0,s = 1,nxt;
            cout<<"\nYou picked Seatwork3- Fibonacci Sequence.";
            cout<<"\n\nPlease enter the nth term to display the Fibonacci Sequence: ";
            cin>>t;
    
            for(int i = 0; i < t; i++){
                cout<<f<<"  ";
                nxt = f+s;
                f = s;
                s = nxt;
            }
            cout<<"\n\nSeatwork3- Semi Final has ended";
            break;
        case 3:
            int n1 = 0, n2 = 0;
            cout<<"Please enter a number for the rows: ";
            cin>>n1;
            cout<<"Please enter a number for the columns: ";
            cin>>n2;
            cout<<"\n";
    
            for(int rows=0;rows!=n1;rows++){
                for(int cols=0;cols!=n2;cols++){
                    if(rows==0 || rows==n1 || cols==0 || cols==n2){
                        cout<<"*"<<"\t";
                    }else cout<<"*"<<"\t";
                }   
            cout<<endl;
            }
        default:
            cout<<"Illegal Choice. Please try again.";
        }
            cout<<"\n\nGo back to Menu?(y/n): ";
            cin>>choice;
            cout<<"\n";
            
    }while(choice != 'n');
        
         cout<<"\nGoodbye!.";
    return 0;
}

1 Answer

0 votes
answered Nov 26 by Peter Minarik (40,320 points)

You've Got To Please The Compiler :)

If you try to run (compile) your code as a C++ code, you'll get the following error in the Online GDB Compiler:

main.cpp:50:14: error: jump to case label [-fpermissive]

This is a bit cryptic, but what it really means is that your variables defined in one case are visible in another case. This is normally a warning, but it looks like it's been turned into an error in the compiler settings.

Normally you can just search the flag (fpermissive) to find out what it is for. You'll find plenty of explanations, just like this.

Now, that we know what the problem is, we need to solve it. One of the easiest solutions is to have every case in your switch its own scope.

switch (h1)
{
    case 1:
        {
            // Your code comes here...
        }
        break;

    case 2:
        {
            // Your code comes here...
        }
        break;

    case 3:
        {
            // Your code comes here...
        }
        break;

    default:
        {
            // Your code comes here...
        }
        break;
}

It is also a good idea to double-check whether all of your cases (including default) are closed with a break statement. This prevents the execution of code to jump from one case to another.

After this fix, your code compiles and runs.

Incorrect Behaviour

Your solution for "Assignment3 - Even or Odd Number" is wrong. The program suggests you'll tell if the number entered is even or odd, but apparently, you do not know what even and odd numbers are.

Even numbers can be divided by 2 without any remainder. Such as 0, 2, 4, 6, etc (negative numbers work just fine too).

Odd numbers can not be divided by 2 without any reminder. Such numbers are 1, 3, 5, 7, etc (negative numbers work here too).

What your code does is this:

if(n > 10 || n < 30){
    cout<<n<<" is an odd number"<<endl;
}

You have already sanitized your input and at this point, n could be only in the range [10, 30].

But even without this your expression (n > 10 || n < 30) always evaluates to true as a number really is always larger than 10, or less than 30, because if it would be smaller or equal to ten, it would be still less than 30.

And as explained above, being even or odd depends on whether a number can be divided by 2 with/out any reminder.

I'd suggest fixing this.

Funny Way To Say Always

In your "Quiz4 - Box Pattern" you wrote the following code:

for(int rows=0;rows!=n1;rows++){
    for(int cols=0;cols!=n2;cols++){
        if(rows==0 || rows==n1 || cols==0 || cols==n2){
            cout<<"*"<<"\t";
        }else cout<<"*"<<"\t";
    }
    cout<<endl;
}

Your code above really is equivalent to

for (int rows = 0; rows != n1; rows++)
{
    for (int cols = 0; cols != n2; cols++)
        cout << "*\t";

    cout<<endl;
}

, as your if's true and false branch does exactly the same thing. So why complicate things? :) 

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...