Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a problem print function


int display(int** a, int r,int c){
for(int i=0; i<r; i++){
for(int="" j="0;" j<c;="" j++){
cout="" <<="" a[i][j]="" '="" ';
}
endl;
}

can faxied this because something goes wrong and don't know why;

What I have tried:

What I have tried:

int display(int** a, int r,int c){
    for(int i=0; i<r; i++){
for(int="" j="0;" j<c;="" j++){
cout="" <<="" a[i][j]="" '="" ';
 }
 endl;
 }





int main(){

int="" rows,cols;
="" cols="4;
" string**="" array;
="" cout<<"="" hello="" a.r’s="" owner";
="" \n="" enter="" the="" number="" of="" rows="" :="" \t";
="" cin="">>rows;
    array = new string*[rows];
    for(int i=0; i<rows; i++){
="" array[i]="new" string[cols];}
="" for(int="" i="0;" i<rows;="" i++){="" {
="" cout<<"="" information="" "<<"(="" "<<i+1<<"="" )"<<"\n";
="" j="0;" j<cols;="" j++)
="" cin="">> array[i][j];}}
    
  display(array,rows,cols)
Posted
Updated 7-Jan-22 5:49am
v2
Comments
User-15430256 7-Jan-22 10:37am    
How do I convert a string to an int
Rick York 7-Jan-22 11:09am    
The function atoi can do it.
Richard MacCutchan 7-Jan-22 10:58am    
What exactly is the problem (apart from all those extraneous sets of ="") in your code
Stefan_Lang 8-Jan-22 9:36am    
Please make sure that the code you post is actually and literally the same as the one you have problems with. If you want help, the least you can do is show us the problem as is, rather than a heap of garbage that no one can understand.

I'm pretty sure that isn't your code as you are compiling it, but just removing the extraneous ="" rpodeces code that won't compile:
C++
int display(int** a, int r,int c){
    for(int i=0; i<r; i++){
for(int j="0;" j<c; j++){
cout << a[i][j] ' ';
 }
 endl;
 }





int main(){

int rows,cols;
 cols="4;
" string** array;
 cout<<" hello a.r’s owner";
 \n enter the number of rows : \t";
 cin>>rows;
    array = new string*[rows];
    for(int i=0; i<rows; i++){
 array[i]="new" string[cols];}
 for(int i="0;" i<rows; i++){ {
 cout<<" information "<<"( "<<i+1<<" )"<<"\n";
 j="0;" j<cols; j++)
 cin>> array[i][j];}}
    
  display(array,rows,cols)
The display function is missing a terminating "}", it's for loop is badly formed, the cout is missing stuff, the endl is all on it's own ... and then we get to main which if just all over the place.

So I suspect that your actual code looks nothing like that, if it runs at all!

And then there is this:
Quote:
How do I convert a string to an int
As stated by Rick York, C library function - atoi()[^] will do it, but why are you reading integers as strings anyway? cin will read an integer perfectly happily.

Perhaps this will help: Put values into an array from cin - C++ Forum[^]
 
Share this answer
 
Quote:
How do I convert a string to an int

You do not need to, but you do need to understand error messages from the compiler.

You have declared your display function as:
C++
int display(int** a, int r,int c){

But you are passing an array of strings not ints, and it never returns (or needs to return) any value. So its declaration should be:
void display(string** a, int r,int c){

This is obviously a continuation of your previous question at Create a dynamic 2D array to store products[^], and it is still going to be difficult to make it do what you want. However many initial 'products' you enter, you will still not be able to add any new ones.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900