Thread: what does this error say?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Unhappy what does this error say?

    Hi. I’m trying to develop a program in which I deal with cars as objects of my defined class “Car”. There is a search part in there in which I ask the user according to which category (name, color,…) the search should be done. The user is supposed to enter a number to determine the search type. Then I have used a switch-case statement. The problem is that it draws a red line under the word “switch” with the following error:
    “Transfer of control bypasses initialization of:”
    I have never encountered such an error before. Could you please explain what this means and what the problem is with my program?

    Some further information that could be helpful:
    I have passed Car* CarRecord and int n to my search function. The CarRecord is an array which stores objects of class Car and n is the size of the array. (it’s ok. Isn’t it?)
    Later I have the following statements in one of the cases:

    string Dname; //the search case is according to the name of the desired car
    cin>>Dname;
    for(int i=0 ; i<n ; i++)
    if ( CarRecord[i].Name==Dname )
    cout<< CarRecord[i].Name<<CarRecord[i].color;

    Is it wrong to do so?
    Last edited by Farnaz; 02-27-2011 at 02:47 PM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Consider the following code:
    Code:
    int a = 2;
    switch (a)
    {
        case 1:
            int x = 100;
        case 2:
            cout << x << endl;
            break;
    }
    If a == 2 initialization of x would be bypassed because the "int x = 100" would not be executed. To prevent this you need to create additional instruction block:
    Code:
    int a = 2;
    switch (a)
    {
        case 1: {
            int x = 100;
        }
        case 2:
            cout << x << endl;
            break;
    }
    This code will not even compile, because x is only visible in the created instruction block.

    Btw: if you have problem with switch, why the hell won't you post the switch code?
    Last edited by kmdv; 02-27-2011 at 02:58 PM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    Thank you very much! I did what you said and it worked; but could you please explain a little bit more what this additional instruction block does exactly and whether "break" should be put in the braces or not?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    By instruction block you could also say statement block or scope block. New blocks are usually put there only to control object lifetimes. The integer x, like any object, is created in the case 1 and stays "alive" only while the switch has not fallen to case 2. If you want to break at case 1, I would put the break in the new block, but I don't think it makes a semantic difference where it is.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    thanks!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Farnaz View Post
    ...I have passed Car* CarRecord and int n to my search function. The CarRecord is an array which stores objects of class Car and n is the size of the array. (it’s ok. Isn’t it?)...
    No. Use std::vector.
    Also look into using smart pointers (SourceForge.net: Raw pointer issues - cpwiki)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    it was an assignment actually and we aren't allowed to use vector in it.
    thanks anyway
    Last edited by Farnaz; 02-27-2011 at 04:26 PM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread