enum: run-time failure #3

Hi, I am getting a run-time failure #3: "row being used without being initialized" and I get the same for col.

if I type in "Extra large" and red, I get that error, but if I type in Small and Green, I don't get the error message, which doesn't make any sense. I should either get the error message for both entry or for none.

Thanks for the help.
#include<iostream>
#include<string>

using namespace std;

//declare and initialize array INV along with its column and row respectively
	int INV[3][4]={{10,20,30,40},{15,20,25,30},{35,40,45,50}};
	enum SIZES{S,M,L,XL};
	enum COLORS{RED,GREEN,BLUE};

	//declare functions
	void sizeNColor();

int main(){
	

string st;
string size, color;
while(true){
cout<<"Enter size and color"<<endl;
getline(cin, st);
int n=st.find(' ');
int m=st.length();
size= st.substr(0,n);
color = st.substr(n+1,m-n+1);//works

for(int i=0; i<size.length();++i)
size[i]=toupper(size[i]);
for(int i=0;i<color.length();++i)
color[i]=toupper(color[i]);

COLORS row; SIZES col;
if (color == "RED")  row = RED;
	else if (color == "GREEN")  row =GREEN;
	else if (color == "BLUE")  row = BLUE;

	if (size =="SMALL")  col = S;
	else if (size == "MEDIUM")  col = M;
	else if (size == "LARGE")  col = L;
	else if(size == "EXTRA LARGE" )  col = XL;

	cout<<INV[row][col]<<endl;
}


return 0;}

Open in new window

pgmerLAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Well, if none of the input you are checking for matches, both 'row' and 'col' are uninitialized, thus causing the error. Try introducing an 'invalid' token and test for them, e.g.
#include<iostream>
#include<string>

using namespace std;

//declare and initialize array INV along with its column and row respectively
	int INV[3][4]={{10,20,30,40},{15,20,25,30},{35,40,45,50}};
	enum SIZES{S,M,L,XL,INVALID_SIZE};
	enum COLORS{RED,GREEN,BLUE,INVALID_COLOR};

	//declare functions
	void sizeNColor();

int main(){
	

string st;
string size, color;
while(true){
cout<<"Enter size and color"<<endl;
getline(cin, st);
int n=st.find(' ');
int m=st.length();
size= st.substr(0,n);
color = st.substr(n+1,m-n+1);//works

for(int i=0; i<size.length();++i)
size[i]=toupper(size[i]);
for(int i=0;i<color.length();++i)
color[i]=toupper(color[i]);

COLORS row = INVALID_COLOR; SIZES col = INVALID_SIZE;
if (color == "RED")  row = RED;
	else if (color == "GREEN")  row =GREEN;
	else if (color == "BLUE")  row = BLUE;

	if (size =="SMALL")  col = S;
	else if (size == "MEDIUM")  col = M;
	else if (size == "LARGE")  col = L;
	else if(size == "EXTRA LARGE" )  col = XL;

    if (row != INVALID_COLOR && col != INVALID_SIZE) // <----- here
	cout<<INV[row][col]<<endl;
}


return 0;}

Open in new window

0
phoffric\Commented:
If you make extra large one word (i.e., no space), say EXTRA_LARGE, then do you still have a problem?
extra-large.PNG
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffric\Commented:
In the image, you can see that size is "EXTRA" and color is "LARGE RED". You didn't mean that to happen. (BTW - the breakpoint was on your cout statement.)
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

pgmerLAAuthor Commented:
it works if it's one word.
Thanks phoffric.
0
jkrCommented:
Well, try again with incorrect input - you'll end up with the same error. Just change the order and enter e.g. "SMALL RED".
0
phoffric\Commented:
Yes, as good programming practice, you should program defensively when you cannot guarantee that the input will be precisely as you desire.

off topic comment..
It is interesting that this general rule does not always apply. Since a line of code is money, and money is controlled by the customer, then if the customer says that they will guarantee the input to be precise, and they do not want you to program defensively, then to do so actually would incur a large penalty when it comes time for the quality reward bonus.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.