Jump to content


array subscripting technique help


2 replies to this topic

#1 c0mputer-fr0d

    Member

  • Members
  • PipPip
  • 30 posts

Posted 12 September 2012 - 03:42 AM

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 Reedbeta

    DevMaster Staff

  • Administrators
  • 5340 posts
  • LocationSanta Clara, CA

Posted 12 September 2012 - 03:56 AM

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?
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 c0mputer-fr0d

    Member

  • Members
  • PipPip
  • 30 posts

Posted 12 September 2012 - 04:11 AM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users