Can you use cin with string

0 votes

To enter a string, I was instructed to use gets(str) rather than cin. 

In the programme below, though, I can use cin perfectly well. 

Could you please tell me if you can use cin? 

Please accept my apologies for my poor English. 

The application allows you to enter up to five names and then print them on the screen.

The code is as follows:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char **p = new char *[5];
    for (int i = 0; i < 5; i++)
    {
        *(p + i) = new char[255];
    } //make a 2 dimensional array of strings

    for (int i = 0; i < n; i++)
    {
        char n[255] = "";
        cout << "insert names: ";
        cin >> n; //how i can use cin here to insert the string to an array??
        strcpy(p[i], n);
    }

    for (int i = 0; i < n; i++)
    {
        cout << p[i] << endl; //print the names
    }
}

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
241 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
485 views
0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
536 views
0 votes
0 answers

Do you use Qt and why do you use it?

Cons and advantages? How long does it ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
226 views
0 votes
0 answers

How to use vector::push_back()` with a struct?

How do I push a struct back into a vector? struct point { int ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
904 views
0 votes
0 answers

How to initialize an std::string with a length?

How can I correctly initialise a string if its length is specified at build time? #include <string> int length = 3; string word[length]; //invalid ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
956 views
0 votes
1 answer

How do I use 'strlen()' on a C++ string

In C++, there are two standard methods for storing strings.  In this scenario, you specify an array of characters, and 0 signifies the end of the string. #include <cstring> char str[500] = "Hello"; // How ever ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
831 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,090 points
491 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,090 points
2,133 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,210 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,546 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP