LATEST ADDITIONS:



 

C++:trouble with 2D array?

Home > Computers & Internet > Programming & Design > C++:trouble with 2D array?
ChristopherChristopher


This is a piece of my code and I am having trouble passing by the variable col....was wondering why it wont work or unless i would have to do several if...else and place for loops inside that.

Issue is col is undeclared and I don't understand why BUT when I put the for loop inside each if else statement and making col a constant of the each sizes S,M,L,XL. I want to use one for loop.

Please provide tips, pointers, errors that I may have done. Thanks

#include <iostream>

#include <string>

#include <cctype>

using namespace std;

int Inv[3][4]={{10,20,30,40},{15,20,25,30},{¦

int TotalSize=0,

string size;

enum COLOR {RED,GREEN,BLUE};

cout<<"Enter the size:";cin>>size;size.erase(1);

size[0]=toupper(size[0]);

if(size=="S"){//total for small

SIZE col=S;}

else if(size=="M"){//total for medium

SIZE col=M;}

else if(size=="L"){//total for large

SIZE col=L;}

else //total for xtra-large

{SIZE col=XL;}

for(COLOR row=RED;row<=BLUE;row=COLOR(row+1))//tot¦ inv

{

TotalSize+=Inv[row][col];

}

cout<<"\tTotal number of shirts="<<endl;

The Phlebob
The Phlebob

It's because each of those declarations of col is local to the {...} it's in. In the for-loop, they're not defined. This is called "scoping".

Do this:

SIZE col;

if(size=="S"){//total for small

col=S;}

else if(size=="M"){//total for medium

col=M;}

else if(size=="L"){//total for large

col=L;}

else //total for xtra-large

{col=XL;}

Now col is declared outside the if-clauses and will stick around for the for-loop.

Hope that helps.


Other Questions & Answers

Why is my Laptop running slower?
Hi! I'm having a little problem with my laptop. It used to play games very smoothly months ago when it was new, but now everything moving a lot s ... Read more (1 answers)
Xh a1s firewire to computer with USB? HELP?
I have a canon xh a1s and a computer with no firewire ports, only usb. How can I get my footage onto the computer? What are my options? Thank you so m ... Read more (1 answers)
Do Apple Keyboards/Mouses work on Windows?
I plan on building a PC in the near future and was wondering if i could use these on a windows, whenever i go into my local apple store, i always find ... Read more (2 answers)
I just bought a brand new hard drive and its making noises did i get a bad drive?
It makes noises every time data transfers should i replace it or is this normal? ... Read more (2 answers)
Good youtube username? 10 points best answer?
Im making a youtube account right now. Its gonna be a channel that is made of vlogs beauty tuorials and just stuff like that. Can anyone think of some ... Read more (8 answers)