Vector, how to use pointers

Asked 1 years ago, Updated 1 years ago, 36 views

I want to print as below, but it doesn't work.
Thank you for your cooperation.

Example Output 1:

Please select two types of numbers from 1 to 10
ingress) 12
The two phrases do not match
The first phrase is "Yoshio Kojima"

Example Output 2:
Please select two types of numbers from 1 to 10
Input) 49
You found a pair!
The two phrases are "Mr. and Mrs. Tanaka"

// Code from here

#include<iostream>
# include <vector>
using namespace std;

int main()
{
    vector<string>phrases;
    phrases.push_back("Ashikaga Yoshimi");
    phrases.push_back("Yoshio Kojima");
    phrases.push_back("dog god";
    Phrases.push_back("Mr. and Mrs. Tanaka");
    phrases.push_back("rural");

    vector<string*>randomPhrases;
    randomPhrases.push_back(&phrases.at(1));
    randomPhrases.push_back(&phrases.at(4));
    randomPhrases.push_back(&phrases.at(0));
    randomPhrases.push_back(&phrases.at(3));
    randomPhrases.push_back(&phrases.at(4));
    randomPhrases.push_back(&phrases.at(2));
    randomPhrases.push_back(&phrases.at(1));
    randomPhrases.push_back(&phrases.at(2));
    randomPhrases.push_back(&phrases.at(3));
    randomPhrases.push_back(&phrases.at(0));


    // Ask user to select two Phrases
    cout<<"Please select two types of numbers from 1 to 10"<<endl;
    intx;
    inty;
    cin>>x;
    cin>>y;


    // Determine if the two selected phases match (i.e.have the same memory address)


    if(&phrases.at(x)==&phrases.at(y))

    {
        cout<<"You found a pair!" <<endl;
        cout<<"The two phrases are"<<phrases.at(x)<<endl;
    }
    else
    {
        cout<<"The two phrases do not match"<<endl;
        cout<<"The first phrase is "<<phrases.at(x)<<endl;


    }





    return 0;
}

c++

2022-09-30 20:21

1 Answers

There are four things I noticed.

If you change the original code as much as possible,

#include<iostream>
# include <vector>
#include<string>//1.include required
using namespace std;

int main()
{
    vector<string>phrases;
    phrases.push_back("Ashikaga Yoshimi");
    phrases.push_back("Yoshio Kojima");
    phrases.push_back("dog god";
    Phrases.push_back("Mr. and Mrs. Tanaka");
    phrases.push_back("rural");

    vector<string*>randomPhrases;
    randomPhrases.push_back(&phrases.at(1));
    randomPhrases.push_back(&phrases.at(4));
    randomPhrases.push_back(&phrases.at(0));
    randomPhrases.push_back(&phrases.at(3));
    randomPhrases.push_back(&phrases.at(4));
    randomPhrases.push_back(&phrases.at(2));
    randomPhrases.push_back(&phrases.at(1));
    randomPhrases.push_back(&phrases.at(2));
    randomPhrases.push_back(&phrases.at(3));
    randomPhrases.push_back(&phrases.at(0));


    // Ask user to select two Phrases
    cout<<"Please select two types of numbers from 1 to 10"<<endl;
    intx;
    inty;
    cin>>x;
    cin>>y;
    --x;--y;//2.index translation


    // Determine if the two selected phases match (i.e.have the same memory address)


    if (*randomPhrases.at(x)==*randomPhrases.at(y)) // 3. and 4.

    {
        cout<<"You found a pair!" <<endl;
        cout<<"The two phrases are"<<*randomPhrases.at(x)<<endl;//3. and 4.
    }
    else
    {
        cout<<"The two phrases do not match"<<endl;
        cout<<"The first phrase is "<<*randomPhrases.at(x)<<endl;//3. and 4.


    }





    return 0;
}

For now, this will work.


2022-09-30 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.