-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?

4
  • What do you mean? what the arr is? a list of strings? Commented Oct 9, 2019 at 11:11
  • every string in c++ has a special character at the end which is automatically put by the compiler i.e. ϕ (Phi) Commented Oct 9, 2019 at 11:12
  • I want to create an array of strings, consisting of 'n' members. Commented Oct 9, 2019 at 11:12
  • 1
    cin>>n; string arr[n]; is not valid in C++. Commented Oct 9, 2019 at 11:14

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

std::array<std::string> arr(n); is completely wrong. You need to provide the length of the array at compile time.
@Adrian That would change behavior of the program. With getline "Hello world, how are you\n" would be one string, with >> that would be five strings.
I am using getline because I am inputting strings having spaces
@GameX Oops! I was thinking out-of-space!
Here is my input:2 hellomecian MelCow . The program is not reading the very first letter of the first string , that is, h. The rest is fine. Does this has to do something with getline as well?
|
1

Use a std::vector

A vector is dynamic array. So change:

string arr[n];

to

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

Comments

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;
    }

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.