Problem with Switch statement

Can someone explain to me what is wrong with this switch and how I can go about fixing it?

class getVehicleType : public Vehicle
{
string v = "";

switch(Number)
{
case 0001: v = "motor"; break;
case 0002: v = "Car trailer"; break;
case 0003: v = "TwoAxleLong"; break;
case 0004: v = "Buses"; break;
case 0005: v = "TwoAxleSixTire"; break;
case 0006: v = "ThreeAxleSingle"; break;
case 0007: v = "FourAxleDouble"; break;
case 0008: v = "FourAxleDouble"; break;
case 0009: v = "TwoAxleLong"; break;
case 0010: v = "SixAxle double"; break;
case 0011: v = "SixAxle double"; break;
case 0012: v = "SixAxleMulti"; break;
case 0013: v = "Bicycle"; break;
case 0014: v = "Not Classified"; break;
}
return v;
};
Two things:
1) The switch statement must be included in a function.
2) Number is not defined.

Additionally, integer literals which begin with zeros specify numbers in octal (base-8). So the literal 0008 isn't valid. Get rid of the leading zeros, leaving something like
1
2
3
4
5
6
case    1: ...
case    2: ... 
...
case    8: ...
case    9: ...
case   10: ...

instead.
Last edited on
If I'm specifically looking for 0001, 0002, etc. within a text file, is there a way to do that or should I avoid using a switch statement?
"0002" inside a text file is a string. You can't use switch statements on strings in the first place.

LUCKILY, C++'s >> stream operator does NOT think that leading zeroes implies octal.
This will work, replace the istringstream with your file stream, and open it:
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
// Example program
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::istringstream iss("0007 0008 0009 0010 0011");

    int n;
    while (iss >> n)
    {
        std::cout << n << std::endl;  

        std::string v; 
        switch (n) {
                case  1: v = "motor"; break;
                case  2: v = "Car trailer"; break;
                case  3: v = "TwoAxleLong"; break;
                case  4: v = "Buses"; break;
                case  5: v = "TwoAxleSixTire"; break;
                case  6: v = "ThreeAxleSingle"; break;
                case  7: v = "FourAxleDouble"; break;
                case  8: v = "FourAxleDouble"; break;
                case  9: v = "TwoAxleLong"; break;
                case 10: v = "SixAxle double"; break;
                case 11: v = "SixAxle double"; break;
                case 12: v = "SixAxleMulti"; break;
                case 13: v = "Bicycle"; break;
                case 14: v = "Not Classified"; break;
        }
        std::cout << v << std::endl;
   }
}


PS: Who the hell thought it was a good idea to make leading zeroes act as octal notation...
Last edited on
https://blogs.msdn.microsoft.com/oldnewthing/20140116-00/?p=2063

Apparently it was the fault of the B language!


Anyway:
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
#include <iostream>
#include <sstream>
#include <string>
#include <map>
using namespace std;

int main()
{
   map<string,string> carMap = { { "0001", "motor"           },
                                 { "0002", "Car trailer"     },
                                 { "0003", "TwoAxleLong"     },
                                 { "0004", "Buses"           },
                                 { "0005", "TwoAxleSixTire"  },
                                 { "0006", "ThreeAxleSingle" },
                                 { "0007", "FourAxleDouble"  },
                                 { "0008", "FourAxleDouble"  },
                                 { "0009", "TwoAxleLong"     },
                                 { "0010", "SixAxle double"  },
                                 { "0011", "SixAxle double"  },
                                 { "0012", "SixAxleMulti"    },
                                 { "0013", "Bicycle"         },
                                 { "0014", "Not Classified"  } };

   istringstream iss( "0003 0004 0012" );

   string s;
   while ( iss >> s ) cout << s << ": " << carMap[s] << '\n';
}


0003: TwoAxleLong
0004: Buses
0012: SixAxleMulti
Last edited on
Topic archived. No new replies allowed.