Once I was trying to create an object inside a switch statement. It looked something like this.

cout >> "Enter how many objects you want to create: ";
int nofobjects;
cin << nofobjects;
switch(numberofobjects) {
    
case 1:
objectclass object1;
break;

case 2:
objectclass object1;
objectclass object2;
break;

case 3:
cout << "Whoa, thats too many!"
break;

but when I compiled the program it said that I DID NOT declare the objects.
This is probably because if they user types 3 the objects will not be created and they will be created only if they type 1 or 2. So I was thinking of a workaround for this.
I couldn't find one. This just popped up in my head and I still can't think of the solution. So what do you supposed to do when this happens. Is there like a workaround or trick like using if-else form (by the way I tried it and it did not work either).

You don't need a switch statement for that.

objectclass* objects = new objectclass[numberfobjects];

or use a vector of objects

vector<objectclass> objects;
objects.resize(numberofobjects);

I have no idea what a vector is lol. But the first way does make sense, thanks

A vector is an array of objects which handles most of the memory allocation and deallocation for you. There are several tutorials for vectors, just use google and you will easily find them.

But what If I don't want to create an array of objects but separate objects? The first method will not work.

You can create locally-scoped variables within a specific case statement similar to

switch (x) {
   case 1:
   {
      /* Things created here are local to the case 1 statement */
   }
   // ...
}

However, you should always include the { } and, as you have found out, you can not access these variables after the scope of the case label is exited - this includes after the switch statement and within other case labels.

But what If I don't want to create an array of objects but separate objects? The first method will not work.

An array is a convenient way to reason about a group of separate objects (of the same type). A vector is another way to do so. Each of the elements of your array is it's own entity and can be used as an individual object as needed. Consider:

object obs[] = {object(1), object(2), object(3)};
object * obj = &obs[1]; // obj is now pointing to the second object in the array (object(2))
obj->do_something (); // do something with the second object, not the entire array

How about

cout >> "Enter how many objects you want to create: ";
int nofobjects;
cin << nofobjects;
//CORRECTION BELOW
//switch(numberofobjects) {
   
//case 1:
//objectclass object1;
//break;
//case 2:
//objectclass object1;
//objectclass object2;
//break;
//case 3:


if(nofobjects == 3) cout << "Whoa, thats too many!";

objectclass object[nofobject];
//you can then work with it in a loop or any how you want to use it
//e.g

objectclass object[1] = //..... some code here
objectclass object[2] = //..... some code here   etc

break;

I hope you understand

This question has already been answered. Start a new discussion instead.