Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,315 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,085 people online right now. Registration is fast and FREE... Join Now!




Mixing char's and int's in arrays

 
Reply to this topicStart new topic

Mixing char's and int's in arrays

dewjunkie
post 13 Mar, 2008 - 12:36 PM
Post #1


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 49

Hey all!

Ok. I know that I can't mix char's and int's in the same array.

However, the HW problem I have is I have to create the table below. I have to allow a user to choose between any of the choices on this grid. (i.e A4 = 50 or E2 = 45). Two other noteworthy items are: 1. I have to allow those capital letters to be interchangeable with the lower case equivalent.(i.e a = A or E = e) 2. The order of the letter and number must also be interchangeable (i.e A1 = 1A or d2 = 2d).

I think the fact that I have to make things interchangeable is screwing with my head a little. I guess I just don't see the solution in my head yet. Once I know what I need to do I usually don't have many problems.

So should I create a separate array for A-E? Or is that totally wrong? Does anyone have any suggestions on where I should go with this problem?

1 2 3 4 5
+-------+-------+-------+-------+-------+
A | 35 | empty | 90 | 50 | 75 |
+-------+-------+-------+-------+-------+
B | 55 | 10 | empty | empty | 20 |
+-------+-------+-------+-------+-------+
C | empty | 85 | 40 | 95 | 60 |
+-------+-------+-------+-------+-------+
D | 15 | empty | 65 | 5 | 100 |
+-------+-------+-------+-------+-------+
E | 70 | 45 | 25 | 80 | 30 |
+-------+-------+-------+-------+-------+

I am not really looking for the exact answer here. Just need a little direction.
I have more code, but I can tell that it would do more harm than good to put those fragments here.

CODE


#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
            

int grid[5][5] =
{
{ 35, 0, 90, 50, 75},
{ 55, 10, 0, 0, 20 },
{ 0, 85, 40, 95, 60 },
{ 15, 0, 65, 5, 100 },
{ 70, 45, 25, 80, 30 }
};

system("PAUSE");
return 0;
}



Thanks to anyone who helps me out here!!!!
User is offlineProfile CardPM

Go to the top of the page

dewjunkie
post 13 Mar, 2008 - 01:10 PM
Post #2


New D.I.C Head

*
Joined: 6 Mar, 2008
Posts: 49

I kind of solved my problem.

Although my solution is too simple I think. I think I need to use an array to accompliish what these if statements do. Any thoughts?

THANKS!!

CODE


#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
            

int grid[5][5] =
{
{ 35, 0, 90, 50, 75},
{ 55, 10, 0, 0, 20 },
{ 0, 85, 40, 95, 60 },
{ 15, 0, 65, 5, 100 },
{ 70, 45, 25, 80, 30 }
};



char let;

int numb;

cout << "Letter: ";
cin >> let;
cout << "#: ";
cin >> numb;


if (let == 'A'|| let == 'a')
X=0;

else if (let == 'B'|| let == 'b')
X = 1;

else if (let == 'C'|| let == 'c')
X = 2;

else if (let == 'D'|| let == 'd')
X = 3;

else if (let == 'E'|| let == 'e')
X = 4;

else
cout << "invalid letter!";

cout << grid[X][numb-1] <<" ";
cout << "\n";

}
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 15 Mar, 2008 - 11:47 AM
Post #3


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


QUOTE
Ok. I know that I can't mix char's and int's in the same array.

Actually, you can.

Characters are internally stored as single byte integers.
Since integers take up 2/4/8 bytes on a 16/32/64 bit system, it is not possible to mix each and every number into a character array.
So if your integers are in the range of 0-255, they can be mixed in the character array.

All Keyboard characters (including A-E wink2.gif ) fall in the 0-255 range, so you can add them in a character array.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 02:24AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month