array subscripting technique help

E3465662c70bd9bebf3f75223a7a5ce4
0
c0mputer_fr0d 101 Sep 12, 2012 at 03:42

I’m trying to do this assignment for school and Its just about done but I wanted to try something different that I thought would be efficient but doesn’t work as I planned I was wondering if there is a way to do what I’m attempting

void Receipt(char script[50][60],float pricetag[50],int pntr[20][20],float cash[30] ){
     int R = 0;
     while(R < 50){
             if(R > 1){
                  cout << cash[R];}
             cout << "\n" << script[pntr[R]] << "\t" << pricetag[pntr[R]] << "\n";
             R++;      
     }
     }

I’m basically trying to have a nested array where string from script is variable depending on whats inside pntr. I might be just stupid here but it seemed like a good idea maybe I’m just having trouble wrapping my head around my own plan.All these vars Im using are filled with data from different functions that were mostly input from files.

2 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Sep 12, 2012 at 03:56

I don’t fully understand what you’re trying to do, but an immediate problem is that you have a multidimensional array ‘ptr’ that you’re not subscripting properly. Since ‘pntr’ is two-dimensional, you’d have to access it like pntr[X][Y] where X and Y are some ints.

The ‘script’ array can work differently because it’s an array of char, so each row could be interpreted as a null-terminated string, and therefore script[X] is an okay thing to pass into cout. (Of course, it won’t actually do the right thing unless ‘script’ is initialized correctly, with each row actually a null-terminated string.)

Also, why not use a for loop instead of a while loop, since you’re just counting to 50?

E3465662c70bd9bebf3f75223a7a5ce4
0
c0mputer_fr0d 101 Sep 12, 2012 at 04:11

Yea I switched to for loop now I figured I can just do something like this
var = pntr[r][x] where r and x are th for loop ints getting incremented (2 nested for loops)
and then I can just do cout << script[var];
a very simple thing but I could not wrap my head around it.