Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6246923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:46:39+00:00 2026-05-24T12:46:39+00:00

Today, while I was trying to write code to just add and subtract the

  • 0

Today, while I was trying to write code to just add and subtract the two 2*2 matrices, in which I used a switch statement, I got an error:

case bypass initialization of local variable in function main()

Code

#include <iostream.h>
#include <conio.h>
#include <string.h>

int
main()
{
    int mat1[2][2], mat2[2][2], mat3[2][2];

    cout << "Enter the elements in the first matrix";
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cin >> mat1[i][j];
        }
    }

    cout << "\n\nEnter the elements of the second matrix";

    for (int k = 0; k < 2; k++) {
        for (int l = 0; l < 2; l++) {
            cin >> mat2[k][l];
        }
    }

    cout << "\n\nsaved......";

    int choice;
    cout << "\n\n\nFor adding these two matrices,press 1";
    cout << "\nFor subtracting these two matrices,press 2";
    cin >> choice;

    switch (choice) {
    case 1:

        cout << "The addition of the two matrices will yield";
        for (int a = 0; a <= 1; a++) {
            for (int b = 0; b <= 1; b++) {
                mat3[a][b] = mat1[a][b] + mat2[a][b];
            }
        }
        break;

    case 2:
        cout << "The subtraction of the two matrices will yield";
        for (int c = 0; c <= 1; c++) {
            for (int d = 0; d <= 1; d++) {
                mat3[c][d] = mat1[c][d] - mat2[c][d];
            }
        }
        break;
    }
    getch();
    return 0;
}

I also found that I can take the rid of this error by placing the code of case(s), into braces, NOW,

  1. my confusion is about the error…
  2. & the requirement of braces in case….

(I know I haven’t used the new coding conventions, like <iostream>, std namespace, etc., etc. as I have written it in the Turbo C++ compiler, so a to-the-point answer is humbly requested.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:46:40+00:00Added an answer on May 24, 2026 at 12:46 pm

    A switch statement is just a bunch of labels and a goto done by the compiler depending on the value of the thing inside the switch test.

    When you have a local variable in a function, anywhere past the declaration of that variable you can use it. For instance:

    int a;
    // can use a now
    

    However, in a switch statement, if you have a local variable:

    case a:
        int a;
        break;
    case b:
        // we can use a here because these cases are just labels used in a goto
        // i.e. the cases do *not* create a new scope
    

    So when you have a variable in a case, the variable exists in cases below it but the variable won’t exist because the code that initialized it got skipped by the case statement. It’s hard for me to explain, maybe someone else can do a better job.

    The braces fix this problem because they make the variable local, so that it doesn’t exist in subsequent cases. It only gets created if that particular case gets entered, and if you forget a break and control falls through to the next case, the ending } ends the scope and causes the variable to be destroyed so it’s not accessible from the next case, and the initialization can’t be skipped.

    So just remember that all the cases share scope. That might help you understand this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got an error today while trying to do some formatting to existing code.
Today while writing some Visual C++ code I have come across something which has
I'm trying to write a little CLI Hangman game, just to create something in
I've been trying to find the answer to this for a while today and
Today I had a coworker suggest I refactor my code to use a label
Today I stumbled about a Problem which seems to be a bug in the
Today at work we came across the following code (some of you might recognize
While trying a sem-complex query to display some ListView content on the page I
I am trying to learn how to use Abaqus Scripting. I just downloaded Eclipse
i've been using xampp 1.7.3 for a while now and today i've decided to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.