int r = rand()%2;
 
switch(r)
{
     case 0:
                string x[] = "Hi.";
                break;
 
     case 1:
                string x[] = "Bye.";
                break;
}

Will this code produce a conflict on the string "x"?

Recommended Answers

All 5 Replies

Only one case will be executed.

Tru dat. But the compiler keeps giving error "conflict" or "redeclaration". So I was just double checking here because at this point in my program it would be REALLY inconvenient to fix it!

They are both in the same scope, so yes there is conflict. For block scope, enclose code in { and }.

[edit]And I was going to mention the array thing, but alas I was too slow.

It is illegal to jump past a declaration with an initializer unless the declaration is enclosed in a block.
So, do this

#include <iostream>
#include <string>
using namespace std;
int main ()
{
int r = rand()%2;
switch(r)
{
     case 0:
		 {
		 string x = "Hi.";
		 }
                break;
     case 1:
                string x = "Bye.";
                break;
}
return 0;
}

or

#include <iostream>
#include <string>
using namespace std;
int main ()
{
int r = rand()%2;
string x;
switch(r)
{
     case 0:
		x = "Hi.";
		break;
     case 1:
                x = "Bye.";
                break;
}
return 0;
}

Thanks guys, that did it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.