1

I am trying to convert a char of 2D array to int of 2D array so that I can get ASCII value of each character but whenever I print the output I am getting output in Hexadecimal.But when I tried to convert that hexadecimal to ASCII the value does not equal to any of the ASCII Values

#include <iostream>
using namespace std;
int main() {
    int n;
    cout<<"Enter input"<<endl;
    cin>>n;
    char a [n][50];
   int arr [n][50];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        arr[i][50]=a[i][50];
    }
    int count=0;
    for(int i=0;i<n;i++)
     {
       cout<<arr[i];
       cout<<endl;
     }
return 0;
}

I accept the output 65 but the output I am getting is 0x7ffee224f940. Input that I gave was A

user5474425
  • 115
  • 1
  • 9
  • 1
    `char a [n][50];` in standard `c++` `n` must be a compile time constant. https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – drescherjm Jul 08 '19 at 18:37
  • 2
    `arr[i][50]=a[i][50];` is undefined behavior. You are accessing 1 past the end of the 2d arrays. valid indices are arr[0][0] to arr[n-1][49] – drescherjm Jul 08 '19 at 18:38
  • 2
    What should `cin>>a[i];` do? What is even the purpose of `[50]`? – Quimby Jul 08 '19 at 18:39
  • Not using a number known at compile time in `n`'s position during both array declarations invokes undefined behavior, which, if you're unaware, is really bad and probably part of the source of your crash (although it might depend on your compiler whether or not you can get away with it this time. Since you're crashing, though, I'm assuming you're not.). –  Jul 08 '19 at 18:41
  • 1
    `arr[i]` evaluates as a pointer to the data of `arr[i][0]` which is then printed as a pointer. – Khouri Giordano Jul 08 '19 at 18:51

4 Answers4

2

You are trying to print an integer array. The hexadecimal output that you are getting is the address of the array, not the contents. If you want to get the value of a single character, you should print them one by one.

Aysu Sayın
  • 191
  • 7
1

There are 2 major problems with your code.

The first is this part right here:

    int n;
    cout<<"Enter input"<<endl;
    cin>>n;
    char a [n][50];
    int arr [n][50];

Since n is not known at compile time, char a [n][50]; and int arr [n][50]; are two really bad things you shouldn't do. Some compilers might let you get away with this, but in general, array declarations need to know the size ahead of time. For some examples of how to do this the right way, see the answers to this question.

Problem 2:

arr[i][50]=a[i][50];

Another very bad thing you're doing here. Array subscripts are 0 based, meaning that in your example, there is no such thing as arr[i][50]. The last one is arr[i][49]. The linked question gives some good reasons why it is 0 based, so I won't go over it again. Just know that because the first element of the array starts at arr[i][0], you actually have to access 1 less than your original size to get the last element of the array.

Failure to follow these guidelines can lead to unexpected results at the very least, and crashing at the worst, as you have found out.

1

Though some complilers supports its own language extensions nevertheless variable length arrays

cin>>n;
char a [n][50];
int arr [n][50];

is not a standard C++ feature. So instead them it is better to use the standard container std::vector.

It seems in this statement

arr[i][50]=a[i][50];

you are trying to assign one array to another array. Apart from the expressions are incorrect arrays do not have the assignment operator.

Below is a demonstrative program that shows how to perform your task.

#include <iostream>
#include <string>
#include <limits>
#include <array>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstring>

int main() 
{
    const size_t N = 50;

    std::cout << "Enter the number of strings: ";

    size_t n;

    std::cin >> n;

    std::vector<std::array<char, N>> strings( n );
    std::vector<std::array<int, N>> values( n );

    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    for ( size_t i = 0; i < strings.size(); i++ )
    {
        std::cin.getline( strings[i].data(), N, '\n' );
    }

    for ( size_t i = 0; i < strings.size(); i++ )
    {
        std::cout << strings[i].data() << '\n';
    }

    for ( size_t i = 0; i < strings.size(); i++ )
    {
        size_t len = std::strlen( strings[i].data() ) + 1;
        std::copy( strings[i].data(), strings[i].data() + len, std::begin( values[i] ) );       
    }

    for ( const auto &row : values )
    {
        for ( auto it = std::begin( row ); *it != 0; ++it )
        {
            std::cout << *it << ' ';
        }
        std::cout << '\n';
    }

    return 0;
}

The program output might look the following way

Enter the number of strings: 2
Hello
World
72 101 108 108 111 
87 111 114 108 100 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Let's delve into your code step by step:

  1. To input a two dimensional array you need two dimensions, i.e generally two variables are used to define a single element in the array.Thus requiring nested for loop, a single for loop wont suffice.

  2. The array indices begin from 0, thus the last element of an array can only be n-1, or here 50-1=49. Thus pointing to a variable location indexed at 50 is wrong too.

  3. The assignment arr[i][j]=a[i][j], would be correct, and enough to convert character to integer.

Thus Updated Code:

#include <iostream>
using namespace std;
int main() 
{
    int n;
    cout<<"Enter input"<<endl;
    cin>>n;

    //The 50 in the second parameter, implies that the column width will always be 50
    char a[n][50];
    int arr[n][50];

    for(int i=0;i<n;i++)
    {
        for(j=0;j<50;j++)
        {
            cin>>a[i][j];
            //Direct assignment to obtain the ascii value
            arr[i][j]=int(a[i][j]);
        }
    }


    for(int i=0;i<n;i++)
    {
        for(j=0;j<50;j++)
        {
            cout<<arr[i];
            cout<<endl;
        }
    }

return 0;
}

You can know more about 2D arrays here

<--Upvote if Helpful-->

muditrustagii
  • 743
  • 6
  • 17
  • 1
    Please note the diagnostic [here](https://wandbox.org/permlink/cXp0SkpXydRUmp1H). – Bob__ Jul 08 '19 at 19:08
  • Yes , @Bob__ i missed on the initialisation of loop variables. :( However variable size array has been implemented on c++ since 2011, and is used on many coding websites too. I think its safe to utilize this in programs now. – muditrustagii Jul 08 '19 at 22:28