Thread: help woth cout 3d string array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Angry help woth cout 3d string array

    Hi All
    im new to c++
    i got an assignment to make a grade sheet. using 3d arrays
    but now i am having problem with displaying result.
    when only 1 subject it works fine but it doesn't work for 2 or more subjects
    need help fast.
    here's my attempt
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    float str2flt(string str)		
    {
    	float i;
    	stringstream iss(str);		
    	iss >> i;					
    	return i;
    }
    string flt2str(float no)		
    {
    	stringstream iss;			
    	iss << no;					
    	return iss.str();			 
    }
    
    /* Making an 3d array of string type all the input paramters are given by user (no of grade sheets, Students, and no. of evaluations) */
    void main ()
    {
    	string ***array={0};		 
    	int *data={0};				// for remembering individual input for no. of students of diffrent subjects
    	float j,u,n,i,ax,a=0;
    	int sub,std,ppr;
    	cout<<"Enter No. Of Subjects : ";
    	cin>>sub;
    	data=new int [sub];
    	for(int su=0;su<sub;su++)
    	{
    		array=new string **[sub]; //x
    		cout<<"Enter No. Of Students for subject no. "<<su+1<<" : ";
    		cin>>std;
    		data[su]=std;
    		array[su]=new string *[std+1];				//y   +1 because of in 0 0 0 name of subject will be stored
    		cout<<"Enter No. Of Evaluations for subject no. "<<su+1<<" : ";
    		cin>>ppr;
    		for(int p=0;p<=std;p++)
    		{
    			array[su][p]=new string [ppr+3];		/* z   +3 cuz on location [o][o][n] name of students will be stored,
    													on [x][y][ppr+1] avrg will b stored and on [x][y][ppr+2] grade will be stored */
    		}
    		// declartion completed
    		cout<<"Enter Name Of The Subject no. "<<su+1<<" (no Spaces) : ";
    		cin>>array[su][0][0];
    		cout<<"Please Input Evaluation Weightages for Subject "<<array[su][0][0]<<" (ex:0.3 for 30%)\n";
    		for(int w=1;w<=ppr;w++)
    		{
    			do{ 
    			cout<<"Enter Weightage for evaluation no. "<<w<<" : ";
    			cin>>array[su][0][w];
    			}while(str2flt(array[su][0][w]) > 1 || str2flt(array[su][0][w]) <0); 
    		}
    		for(int s=1;s<=std;s++)
    		{
    			cout<<"Enter Name of Student no. "<<s<<" For Subject of "<<array[su][0][0]<<" (0-100): ";
    			cin>>array[su][s][0];
    			for(int no=1;no<=ppr;no++)
    			{
    				do{
    				cout<<"Enter Score of "<<array[su][s][0]<<" for evaluation no "<<no<<" : ";
    				cin>>array[su][s][no];
    				}while(str2flt(array[su][s][no]) >100 || str2flt(array[su][s][no]) <0);
    			}
    		}
    		//data input complete
    		for(int x=1;x<=ppr;x++) // on z
    		{
    			a+=str2flt(array[su][0][x]);
    		}
    		array[su][0][ppr+1]=flt2str(a);// w(normalizer) calculated and stored on [0][0][ppr+1]
    		
    		for(int avg=1;avg<=std;avg++) // on y
    		{
    			a=0;
    			for(int sn=1;sn<=ppr;sn++) // on z
    			{
    				j=str2flt(array[su][avg][sn]);
    				u=str2flt(array[su][0][sn]);
    				n=j*u;
    				i=str2flt(array[su][0][ppr+1]);
    				ax=n /i;
    				a+=ax;
    			}
    			array[su][avg][ppr+1]=flt2str(a);
    		}
    		//average calculate wid proper weights and normalized
    		for(int g=1;g<=std;g++)
    		{
    			int chk;
    			chk=str2flt(array[su][g][ppr+1]);
    			if(chk < 60)
    				array[su][g][ppr+2]='F';
    			else if(chk <70)
    				array[su][g][ppr+2]='D';
    			else if(chk<80)
    				array[su][g][ppr+2]='C';
    			else if(chk<90)
    				array[su][g][ppr+2]='B';
    			else
    				array[su][g][ppr+2]='A';
    		}
    			
    		} 
    	/* OutPut Code */
    		for(int t=0;t<sub;t++)
    		{
    			cout<<array[t][0][0]<<"\n";
    			int ou=1;
    			while(ou<=data[t])
    				{
    					cout<<array[t][ou][0]<<"\t";
    					cout<<array[t][ou][ppr+1]<<"\t";
    					cout<<array[t][ou][ppr+2]<<endl;
    					ou++;
    				}
    		}
    	cout<<"\n\n>------------------------------------------------------------------------------<";
    
    }//main end

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Any one ?

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Triple indirection is nasty. Do you have to use a 3D array? Can you use any other data structure to simplify this? A 3D array is almost never the correct approach.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I see (barely in the poor indentation) two big problems

    > array=new string **[sub]; //x
    You're reallocating your main array EVERY time around the loop. Do this outside the loop, not inside.

    I also see a lot of
    for(int g=1;g<=std;g++)

    subscripting begins at 0, not 1
    This usually means you're stepping off the ends of any array you only allocated 'std' elements to.


    Not to mention the whole void main thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Thumbs up

    Thanks brother
    and sry am new to all this o
    thanks again
    Quote Originally Posted by Salem View Post
    I see (barely in the poor indentation) two big problems

    > array=new string **[sub]; //x
    You're reallocating your main array EVERY time around the loop. Do this outside the loop, not inside.

    I also see a lot of
    for(int g=1;g<=std;g++)

    subscripting begins at 0, not 1
    This usually means you're stepping off the ends of any array you only allocated 'std' elements to.


    Not to mention the whole void main thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM