0
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"?

3
Contributors
5
Replies
7
Views
10 Years
Discussion Span
Last Post by CStallion
0

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!

0

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.

0

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;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.