hi
i want to change the static table to a dynamic table using pointer
what should i change in this case ?

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>

using namespace std; 

int i ,j , l , k ,x ;
float b;
const int maxElements = 10000;
float Bt[maxElements] ;
string  **T;


int main()
{
ifstream file("file.txt");

  
T = new string*[maxElements];
string *word;
i=0;
 while( file >> word) 

 {
	 word.size();

	T[i]=word;
	cout<<"aaaaaaaa"<<word.size()<<endl;
	i=i+1;

 }
  cout<<"i=   "<<i<<endl;
 
 return 0;
}

because this code doesn't work

Recommended Answers

All 3 Replies

#include <iomanip>
#include <iostream>

using namespace std;

namespace {
    const int MAX_ELEMENTS = 11;
}

int main()
{
    // Step 1) Allocate the rows
    int **table = new int*[MAX_ELEMENTS];
    
    // Step 2) Allocate the columns
    for (int i = 0; i < MAX_ELEMENTS; i++)
        table[i] = new int[MAX_ELEMENTS];
        
    // Step 3) Use the table
    for (int i = 0; i < MAX_ELEMENTS; i++) {
        for (int j = 0; j < MAX_ELEMENTS; j++)
            table[i][j] = i * j;
    }
    
    for (int i = 0; i < MAX_ELEMENTS; i++) {
        for (int j = 0; j < MAX_ELEMENTS; j++)
            cout << setw(5) << table[i][j];

        cout << '\n';
    }
    
    // Step 4) Release the memory
    for (int i = 0; i < MAX_ELEMENTS; i++)
        delete[] table[i];
        
    delete[] table;
}

If you don't have all four steps in some fashion (though step 3 is technically optional), your code is broken.

i try to compile my program but i have this error

main.cpp(28) : error C2440: '=' : cannot convert from 'std::string' to 'std::string

this is my code

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>

using namespace std; 

int i ,j , l , k ,x ;
float b;
const int maxElements = 10000;
float Bt[maxElements] ;
string  **T;


int main()
{
ifstream file("visage.txt");

  
T = new string*[maxElements];
string word;
i=0;
 while( file >> word) 

 {
	 word.size();
	 T[i]= new string[word.size()];
	 T[i]= word;
		i=i+1;

 }
  cout<<"i=   "<<i<<endl;
 for (j=0;j<i;++j)
	  delete[] T[j];
  delete[] T;
 return 0;
}

how i can fix this problem

T is a pointer, word is a string. The error explicitly says this.

how i can fix this problem

I'd suggest learning how to use pointers as a general fix, but the specific fix is to dereference the pointer:

*(T[i]) = word;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.