enum error using functions

Hi,
I am having issue with my program.

If i comment out one of the two function :totalSize() orsizeNColor(), then the program works fine, but if I leave them both out, then I get  a run time check failure #3; about row and col not being initialized.

I can't use COLORS row = INVALID_COLOR; SIZES col = INVALID_SIZE;
as we haven't used that in class.

Thanks for your 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();
	void totalInv();
	void totalSize();

int main(){

	
	//Total inventory
	totalInv();

	//Total number of a given size
	totalSize();
	
	//Total number of a given color and size
	sizeNColor();

	


return 0;}

void totalInv()
	{
	int totalInv =0;
	for (COLORS row=RED; row<=BLUE;row=COLORS(row+1))
		{for (SIZES col=S; col<=XL;col=SIZES(col+1))
			{totalInv +=INV[row][col];}
	}
	cout<<"Total inventory is: "<<totalInv<<endl;

	}

void totalSize(){
	char ch;
	SIZES col;
	cout<<"Enter a size(S,M,L,XL): ";
	cin>>ch;
	ch=toupper(ch);
	switch(ch)
	{case 'S': col =S;break;
	case 'M': col = M;break;
	case 'L':col = L; break;
	case 'XL':col= XL; break;}
	int totalSize=0;
	for (COLORS row=RED;row<=BLUE;row=COLORS(row+1))
	{totalSize+=INV[row][col];}
	cout<<"The total number of "<<ch<<" shirts is "<<totalSize<<endl;

	}

void sizeNColor(){
string st;
string size, color;

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);

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;
}

Open in new window

pgmerLAAsked:
Who is Participating?
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.

phoffricCommented:
Unable to reproduce the problem due to my misunderstanding your instructions. How about posting a program where the problem is built in?
0
pgmerLAAuthor Commented:
what do you mean by built in?
0
pgmerLAAuthor Commented:
For instance, when I comment out: sizeNColor() in the Snippet ID=8118282, the code runs without trouble.

then if I comment out : totalSize() , the code stills runs ok Snippet ID=8118283
,
but if I run both functions in Snippet ID=8118284 , then error appear about row and col not being intialized.
It doesn't make sense since I was able to use the program when one function was commented out.

I am totally confused :(
#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();
	void totalInv();
	void totalSize();
	void totalColor();

int main(){

	//Total inventory
	totalInv();

	//Total number of a given size
	totalSize();

	
	//Total number of a given color and size
	sizeNColor();

	return 0;}

void totalInv()
	{
	int totalInv =0;
	for (COLORS row=RED; row<=BLUE;row=COLORS(row+1))
		{for (SIZES col=S; col<=XL;col=SIZES(col+1))
			{totalInv +=INV[row][col];}
	}
	cout<<"Total inventory is: "<<totalInv<<endl;

	}

void totalSize(){
	char ch;
	SIZES col;
	cout<<"Enter a size(S,M,L,XL): ";
	cin>>ch;
	ch=toupper(ch);
	switch(ch)
	{case 'S': col =S;break;
	case 'M': col = M;break;
	case 'L':col = L; break;
	case 'XL':col= XL; break;}
	int totalSize=0;
	for (COLORS row=RED;row<=BLUE;row=COLORS(row+1))
	{totalSize+=INV[row][col];}
	cout<<"The total number of "<<ch<<" shirts is "<<totalSize<<endl;

	}




void sizeNColor(){
string st;
string size, color;

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);

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;
}

Open in new window

#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();
	void totalInv();
	void totalSize();
	void totalColor();

int main(){

	//Total inventory
	totalInv();

	//Total number of a given size
	//totalSize();

	
	//Total number of a given color and size
	sizeNColor();

	return 0;}

void totalInv()
	{
	int totalInv =0;
	for (COLORS row=RED; row<=BLUE;row=COLORS(row+1))
		{for (SIZES col=S; col<=XL;col=SIZES(col+1))
			{totalInv +=INV[row][col];}
	}
	cout<<"Total inventory is: "<<totalInv<<endl;

	}

void totalSize(){
	char ch;
	SIZES col;
	cout<<"Enter a size(S,M,L,XL): ";
	cin>>ch;
	ch=toupper(ch);
	switch(ch)
	{case 'S': col =S;break;
	case 'M': col = M;break;
	case 'L':col = L; break;
	case 'XL':col= XL; break;}
	int totalSize=0;
	for (COLORS row=RED;row<=BLUE;row=COLORS(row+1))
	{totalSize+=INV[row][col];}
	cout<<"The total number of "<<ch<<" shirts is "<<totalSize<<endl;

	}




void sizeNColor(){
string st;
string size, color;

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);

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;
}

Open in new window

#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();
	void totalInv();
	void totalSize();
	void totalColor();

int main(){

	//Total inventory
	totalInv();

	//Total number of a given size
	totalSize();

	//Total number of a given color
	//totalColor();
	
	//Total number of a given color and size
	//sizeNColor();

	return 0;}

void totalInv()
	{
	int totalInv =0;
	for (COLORS row=RED; row<=BLUE;row=COLORS(row+1))
		{for (SIZES col=S; col<=XL;col=SIZES(col+1))
			{totalInv +=INV[row][col];}
	}
	cout<<"Total inventory is: "<<totalInv<<endl;

	}

void totalSize(){
	char ch;
	SIZES col;
	cout<<"Enter a size(S,M,L,XL): ";
	cin>>ch;
	ch=toupper(ch);
	switch(ch)
	{case 'S': col =S;break;
	case 'M': col = M;break;
	case 'L':col = L; break;
	case 'XL':col= XL; break;}
	int totalSize=0;
	for (COLORS row=RED;row<=BLUE;row=COLORS(row+1))
	{totalSize+=INV[row][col];}
	cout<<"The total number of "<<ch<<" shirts is "<<totalSize<<endl;

	}

void totalColor(){
	string color;
	 COLORS row;
	 cout<<"Enter the color:";
	 getline(cin,color);
if (color == "RED")  row = RED;
	else if (color == "GREEN")  row =GREEN;
	else if (color == "BLUE")  row = BLUE;
	int totalColor;
	for (SIZES col=S; col<=XL; col =SIZES(col+1))
		totalColor+=INV[row][col];
	cout<<"The number of "<<color<<" shirts is "<<totalColor<<endl;
}


void sizeNColor(){
string st;
string size, color;

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);

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;
}

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

phoffricCommented:
Those snippet id's are not readily available to me without looking at the underlying html. So, which of the 3 programs causes a problem - top, middle, or bottom?
0
pgmerLAAuthor Commented:
sorry about that.
the first prgm doesn't work

but if: I comment out totalSize() line 25 in the second prgm, then I don't the error message.
if : I comment the other function, in the third pgm, sizeNColor() line 31, then I don't get the error message either.

but If I try to use both function in the program (first program), then I get the error messge.

0
phoffricCommented:
Common problem. In one function, you use cin >> to obtain the string token, and then in 2nd function you use getline.
But, after the cin >>, there is still left over stuff in the input stream (e.g., any whitespaces, newlines).

Then your getline doesn't even have to wait for you to enter data.

You can verify this by setting a breakpoint on the getline; and do not enter any data. Then step over the getline. Instead of waiting for an input, it just continues, and you have no valid token strings.

To illustrate, if you replace the getline with
cin >> size;
cin >> color;
then the cin skips the leading whitespaces/newlines and waits for a valid token string.

I strongly recommend that you start using the debugger often and more often. You will be finding more quickly solutions to the problems you encounter. It allows you to be stymied by only the more difficult problems, and you will have to ask less questions.

Using a good debugger is essential for development. To quickly get started on using the Visual Studio 2010 debugger (which is an excellent choice for your types of problems, then you can read:

   C/C++ Beginner's Debugging Guide

This beginner's guide should take only about 15 minutes to walk through using your code as an example.

After becoming familiar with the basics, move onto these two articles:
   Breakpoint Tips for C/C++

   Watch, Memory, Stack Tips: C/C++
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
phoffricCommented:
I see you closed the question already, so I guess the debugger tip helped.

I'd like to emphasize for you sake that you take the time to really learn how to use the VS C++ debugger. Even those who may criticize Microsoft, will invariably say that this debugger is one of the best there is.

I'll often have four memory windows open concurrently to analyze some software bug.

The benefit to you is that you will gain more insight into code where you are still a little shaky. From this inside (literally, inside the program) information, you may be able to experiment a little and get the program to work. But if not, then you can ask questions where you explain what you are seeing in the debugger (even a snapshot of one or two of the debugger panes) will help us understand your problem better.
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.

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.