displaying ascii codes in 2D array

hi,
this is my original code:
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
string display(string);
main()
{
string word;
ifstream infile;
infile.open ("text.txt");
while(!infile.eof())
{
getline(infile,word);
cout<<word<<endl;

}
display(word);
infile.close();

system ("pause");
}
string display(string word) {
int x = 0, len=0;
len=word.length();
cout << "\n\nLength of sentence is " << len;
cout << "\n\nThe ASCII for each character are as follows :\n\n";
for (x=0;x < len;x++) {

cout << word[x] << " : "<< int(word[x]) << "\n";

}
cout << "\n";

cout << "Reversing ASCII codes back to characters\n\n ";
for (x=0;x <len;x++) {
cout << char(word[x]);
}
cout << "\n\n";
return word;
}
--------------------------------------------
here is what it does;it reads from a text file and converts the characters to ascii numbers and stores them in 1D array.what I am trying to is storing these ascii codes in 2D array.
could you help me please?!
Just a reformat of your code makes for more readable code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
 #include<fstream>
 #include <string>
 using namespace std;
 string display(string);
 main()
 {
 string word;
 ifstream infile;
 infile.open ("text.txt");
 while(!infile.eof()) 
 {
 getline(infile,word); 
 cout<<word<<endl; 

 }
 display(word);
 infile.close();

 system ("pause");
 }
 string display(string word) {
 int x = 0, len=0;
 len=word.length();
 cout << "\n\nLength of sentence is " << len;
 cout << "\n\nThe ASCII for each character are as follows :\n\n";
 for (x=0;x < len;x++) { 

 cout << word[x] << " : "<< int(word[x]) << "\n"; 

 }
 cout << "\n";

 cout << "Reversing ASCII codes back to characters\n\n ";
 for (x=0;x <len;x++) {
 cout << char(word[x]);
 }
 cout << "\n\n";
 return word;
 }
Last edited on
alright I have reformatted the codes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
string display(string);
main()
{
string word;
ifstream infile;
infile.open ("text.txt");
while(!infile.eof()) 
{
getline(infile,word); 
cout<<word<<endl; 

}
display(word);
infile.close();

system ("pause");
}
string display(string word) {
int x = 0, len=0;
len=word.length();
cout << "\n\nLength of sentence is " << len;
cout << "\n\nThe ASCII for each character are as follows :\n\n";
for (x=0;x < len;x++) { 

cout << word[x] << " : "<< int(word[x]) << "\n"; 

}
cout << "\n";

cout << "Reversing ASCII codes back to characters\n\n ";
for (x=0;x <len;x++) {
cout << char(word[x]);
}
cout << "\n\n";
return word;
}

here is what it does;it reads from a text file and converts the characters to ascii numbers and stores them in 1D array.what I am trying to is storing these ascii codes in 2D array.
could you help me please?!
here is what it does;it reads from a text file and converts the characters to ascii numbers and stores them in 1D array.what I am trying to is storing these ascii codes in 2D array.
could you help me please?!


This depends entirely on how you wish the 2d array to be structured.

You could create a char array[i][j];.

Or you could create a string array[i] where array[i] points to another array of chars.

A description of the intended use may help in determining this.
ok I have managed to store the numbers in 2D array.but when I want to to multiply this array by another 2D array it doesn't give the write answer.here is what I have done so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
string display(string);
main()
{
	string word;
    ifstream infile;
	infile.open ("text.txt");
        while(!infile.eof()) 
        {
	        getline(infile,word); 
	        cout<<word<<endl; 
	        
	        }
	        display(word);
	infile.close();
	
	system ("pause");
}
string display(string word) {
   int x = 0, len=0;
   len=word.length();
   int a[2][2];
   int b[2][2]={{1,2},{0,1}};
   int c[2][2];
   int count=0;
   cout << "\n\nLength of sentence is " << len;
   cout << "\n\nThe ASCII for each character are as follows :\n\n";
   for (x=0;x < len;x++) {  
     
       //int(word[x]);   
	  for(int i=0;i<2;i++){
	  	for(int j=0;j<i;j++){
	  		a[i][j]=int(word[x]);//it assigns 1D array to 2D array
	  		if(count%2==0){
	  			cout<<"\n\n";
	  		}
	  		cout<<a[i][j]*b[j][i]<<"\t";//it should multiply 2 arrays
	  		count++;
	  	}
	  } 
          
    }
    cout << "\n";

    cout << "Reversing ASCII codes back to characters\n\n ";
    for (x=0;x <len;x++) {
        cout << char(word[x]);
    }
    cout << "\n\n";
return word;
}

what can I do to handle this?
here is what it does;it reads from a text file and converts the characters to ASCII numbers and stores them in 1D array.what I am trying to is storing these ASCII codes in 2D array.
could you help me please?!


if you are planning on using the numbers stored to perform math you don't want the ASCII characters. You need to first convert you characters into a numeric value (when a program prints a value, it prints the character/s of the associated value).

For example : The byte value of the ASCII character '1' in binary is 0011 0001.
The byte value of the numeric 1 is 0000 0001.

If you perform math on the ASCII character '1' it is likely going to be interpreted as 49 which has the same binary value as the character, 0011 0001.

I think your misunderstanding lies in thinking that the ASCII numeral characters, are the same as the numeric ones. This is not the case.

I am sure if you search google "converting chars to numerals" or something like that you will find an appropriate answer.



Topic archived. No new replies allowed.