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?
 
phoffricCommented:
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
 
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
 
phoffricCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
 
phoffricCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.