0

Here is my code:

What I am trying to do is have the user input a bunch of strings and then the user says which string on the list you just wrote that the program will say back to you.

#include <iostream>
#include <string>

using namespace std;

int amount = 0;
int listnum;
string inpu;


void input(string in){
cout << "Enter a word" << endl;
cin >> in;
}



int main()
{
cout << "How many strings do you want to enter." << endl;
cin >> amount;

string list1[amount];
for(int x = 0; x < amount; x++){
    input(inpu);
    list1[x] = inpu;

    if(x == amount-1){
        cout << "Which word on the list do you want to use?" << endl;
        cin >> listnum;
        cout << list1[listnum] << endl;

    }
}
}

I am not sure what is happening so I really would love the help.

Thank you!

2 Answers 2

1

I do not know what problem you are having. I see a problem with this though:

void input(string in){
    cout << "Enter a word" << endl;
    cin >> in;
}

Try passing a reference to your variable instead:

void input(string &in){
cout << "Enter a word" << endl;
cin >> in;
}

Or you can pass a pointer:

void input(string *in){
cout << "Enter a word" << endl;
cin >> *in; //Now that this is a pointer, you need to add a * before the variable name when you want to access it.
}

If you pass a pointer, make sure you call your function with a pointer though:

input(&inpu);

Passing pointers is the only way to do it in C. You will probably never have to use it in C++ unless you are calling a C function.

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

Comments

0

There is no way to do what you say in C++ with plain arrays. The C++ solution for that is by using the STL library that gives you the std::vector.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

    int main() {
        vector<string> list1;//This creates a vector 
        string in = "";
        int amount = 0;
        int listnum = 0;


        cout << "How many strings do you want to enter." << endl;
        cin >> amount;

        //This for loop will append the item into the vector list
        //The for loop will control how many item will be appended to the list
        for (int i = 0; i < amount; i++) {
            cout << "Enter a word" << endl;
            cin >> in;
            //The push back function will push the string into the vecot
            list1.push_back(in);
        }

        //This will ask the user for the index position of the word
        cout << "Which index on the list do you want to use?" << endl;
        cin >> listnum;

        //This line of code will output the string that the user wants
        cout << list1[listnum] << endl;


        system("pause");
        return 0;
    }

1 Comment

The asker is probably using gcc or something, these compilers allow for string list1[amount] where amount is not a constant.

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.