-1

I want to create a string array using loop inputs and then give ASCII based output for each character of the string.

Here is my code

int n;
cin >> n;
string arr[n];
for(int i=0;i<n;++i){
    getline(cin,arr[i]);
}
for(int i = 0;i < n; ++i){
    for(int j = 0;j < arr[i].length(); ++j){
        cout << to_string(arr[i].at(j)) << " ";
    }
    cout << endl;
}

the program takes n : number of string inputs but allows me to input only n-1 strings.

What is the problem here?

Hu Xixi
  • 1,799
  • 2
  • 21
  • 29
GameX
  • 57
  • 8

4 Answers4

4

You are reading n strings, but the first one doesn't contain what you expect.

When you write input in the console, you write something like:

3\nstring1\nstring2

, where \n is the newline character (when you press Enter).

When you do cin >> n, you parse this input string, and you get the integer. Meaning that you remain with

\nstring1\nstring2

in the buffer. And when you do a getline, you parse everything up to the first newline (including the newline). That's why you get the first string empty.

A quick and dirty fix is to read the newline too:

int n;
char newline;
cin >> n >> newline;

, and then loop as you do now.


Some remarks about your code.

string arr[n] is not valid C++. In C++, there is no official support for arrays with variable size like this (some compilers support it, but this doesn't mean it's standard). You should use a std::vector:

std::vector<std::string> arr(n);

(the rest of the code remains the same). An even better way would be to declare it empty, and then use push_back to populate it.

Also, when comparing j < arr[i].length(), you are comparing a variable of type int with a variable of type size_t, which might be bigger than an int and create issues for very long strings. Use size_t as the type for j.

Paul92
  • 8,827
  • 1
  • 23
  • 37
1

Use a std::vector

A vector is dynamic array. So change:

string arr[n];

to

std::vector<std::string> arr(n);
darune
  • 10,480
  • 2
  • 24
  • 62
1

According to your code snippet what you want to read is a number of strings and then output for each of them the ASCII value of all of its characters:

If you have an input like the following

2
abc
def

you would like to obtains as a result something like the following:

97 98 99 
100 101 102 

In order to achieve that I see at least a couple of problems with your code.

  1. C++ does not support variable size arrays by default. It means you cannot create an array whose size is not known at compile time (see this answer for more info about it)

  2. In order to output the ASCII of the char you simply need to cast the char to int and that is it.

  3. using getline in this case complicates the code because of the way getline works with newline. Simply use cin>>arr[i] to read each string.

Here is it a version of your code that does what you expect:

int n;
   cin>>n;
   vector<string> arr(n);

   for(int i=0;i<n;++i)
        cin>>arr[i];

    for(const auto& s : arr){
        for(const auto& c : s){
            cout<<(int)c<<" ";
        }
        cout<<endl;
    }
Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
0

If you need a list of strings, you can use vector:

#include<vector>
#include<string>

int n;
cin >> n;
vector<string> arr(n);
for(int i = 0; i < n; ++i){
    getline(cin, arr[i]);
}
for(auto& str : arr){
    for(auto& c : str){
        cout << to_string(c) << " ";
    }
    cout << endl;
}
Hu Xixi
  • 1,799
  • 2
  • 21
  • 29