1

I am new to C++ and was trying a lot to take strings from the user and add those inside an empty array. I read my book but did not get proper explanation. I understand my code is not right but what I am trying to do is, asking a user to give a string, and until the user types "Quit", the program should take the strings and add that to my empty array named listt. After that I declared variable named len and tried to get the length of the array that I just made. However I am getting several errors and looked for resources to fix my problems. Still I could not do it. It will be genuinely helpful if someone can please help me to solve this problem. Thank you very much. My code is:

#include <iostream>

using namespace std;
int main()
{
    string listt[];

    string word;
    cout<< "Enter word: ";

    while (word != "Quit" ){

        cin >> word;
        listt.push_back(word);
    }
    cout << listt;

    int len ;

    len = listt.size();
    cout << len;
    for (int i=0; i < len; i++){
        cout << i;
        cout << endl;
    
    }
    return 0; 
}
3
  • 1
    You're clearly used to another language, and are incorrectly assuming that code constructs/syntax in that language will work the same way in C++. That approach almost never works for any combination of language you know and language you wish to learn. Focus on learning C++, rather than on programming in the language you know. Commented Aug 2, 2020 at 10:20
  • Do not use another programming language as a model in writing C++ code. Even if you did get the code to compile, writing code using another language as a model will either create buggy programs, inefficient programs, or programs that just look plain weird to a C++ programmer. Also, you shouldn't just be "looking around for resources" when it comes to C++. The best resources are actual good C++ books. C++ is a very difficult language to learn properly, and you can't learn it by having a few cheat sheets of instructions here and there, like some other languages. Commented Aug 2, 2020 at 10:44
  • What are the error messages You are unable to resolve? Commented Aug 2, 2020 at 10:45

3 Answers 3

1

You should use vector instead of array:

So instead of:

string listt[]; // shouldn't compile anyway

use:

vector<string> listt;

Also, this is not ok:

cout << listt; // you can't print array or vector directly, so loop and print each item

Also, use for-each instead of C-style for

for (const auto &v : listt)
{
    cout << v << endl;
}

The rest of your code should be fine.

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

Comments

1

In your code you use methods for class vector. You need to include library vector in your programm. Also you need to declare vector of strings as:

vector<string>listt;

If you want to output each element of the vector you need to use for loop instead.

for ( int i = 0; i < listt.size(); i++ ) cout << listt[i] << '\n';

So it's not correct

cout << listt;

Your programm will look like:

#include <iostream>
#include <vector>

using namespace std;
int main()
{
    vector<string> listt;

    string word;
    cout << "Enter word: ";

    while (word != "Quit") {

        cin >> word;
        listt.push_back(word);
    }

    int len;
    len = listt.size();
    cout << len;

    for (int i = 0; i < len; i++) {
        cout << listt[i];
        cout << endl;

    }
    return 0;
}

Comments

0

You are probably looking for vector<string>. What you are tried to make is an array, which does not have anything like push_back().

Vector docs

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.