尝试通过方法C++实现字符串数组

尝试通过方法C++实现字符串数组,c++,arrays,string,C++,Arrays,String,我试图从用户那里读取姓名和年龄,直到用户输入停止。然后打印所有这些值。请帮帮我,我只是C语言的初学者++ // Pass.cpp // Reading names and ages from user and outputting them #include <iostream> #include <iomanip> #include <cstring> using std::cout; using std::cin; using std::endl; u

我试图从用户那里读取姓名和年龄,直到用户输入停止。然后打印所有这些值。请帮帮我,我只是C语言的初学者++

// Pass.cpp
// Reading names and ages from user and outputting them

#include <iostream>
#include <iomanip>
#include <cstring>

using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::strcmp;

char** larger(char** arr);
int* larger(int* arr);
void read_data(char*** names, int** ages);
void print_data(char*** names, int** ages);

int main()
{
    char** names = new char*[5];
    char*** p_names = &names;
    int* ages = new int[5];
    int** p_ages = &ages;
    read_data(p_names,p_ages);
    print_data(p_names,p_ages);
}

void read_data(char*** names, int** ages)
{
    const char* sent = "stop";
    const int MAX = 15;
    int count = 0;
    char UI[MAX];

    cout << "Enter names and ages."
        << endl << "Maximum length of name is " << MAX
        << endl << "When stop enter \"" << sent << "\".";

    while (true)
    {
        cout << endl << "Name: ";
        cin.getline(UI,MAX,'\n');

        if (!strcmp(UI, sent))
            break;
        if (count + 1 > sizeof (&ages) / sizeof (&ages[0]))
        {
            *names = larger(*names);
            *ages = larger(*ages);
        }

        *names[count] = UI;

        cout << endl << "Age: ";
        cin >> *ages[count++];
    }
}

void print_data(char*** names, int** ages)
{
    for (int i = 0; i < sizeof(*ages) / sizeof(*ages[0]);i++)
    {
        cout << endl << setw(10) << "Name: " << *names[i]
            << setw(10) << "Age: " << *ages[i];
    }
}

char** larger(char** names)
{
    const int size = sizeof(names) / sizeof(*names);
    char** new_arr = new char*[2*size];
    for (int i = 0; i < size; i++)
        new_arr[i] = names[i];
    return new_arr;
}

int* larger(int* ages)
{
    const int size = sizeof(ages) / sizeof(*ages);
    int* new_arr = new int[2 * size];
    for (int i = 0; i < size; i++)
        new_arr[i] = ages[i];
    return new_arr;
}
试试这个:

#include <iostream>
#include <iomanip>
#include <vector>
#include <sstream>

using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::strcmp;

void read_data(std::vector<std::string> &names, std::vector<int> &ages);
void print_data(std::vector<std::string> &names, std::vector<int> &ages);

int main()
{
    std::vector<std::string> names;
    std::vector<int> ages;

    read_data(names, ages);
    print_data(names, ages);
}

void read_data(std::vector<std::string> &names, std::vector<int> &ages)
{
    const char* sent = "stop";

    cout << "Enter names and ages."
        << endl << "When stop enter \"" << sent << "\".";

    while (true)
    {
        std::string input;
        cout << endl << "Name: ";
        std::getline(cin, input);

        if (!strcmp(input.c_str(), sent))
            break;
        names.push_back(input);

        cout << endl << "Age: ";
        std::string age;
        std::getline(cin, age);

        ages.push_back(atoi(age.c_str()));
    }
}

void print_data(std::vector<std::string> &names, std::vector<int> &ages)
{
    for (int i = 0; i < names.capacity() ; i++)
    {
        cout << endl << setw(10) << "Name: " << names.at(i)
            << setw(10) << "Age: " << ages.at(i);
    }
}

我看到的一个问题是if语句:

if (count + 1 > sizeof (&ages) / sizeof (&ages[0]))
&ages是一个int**(指针)的地址,所以它的大小通常是8,因为这是指针类型的大小。函数不知道数组的大小,只有在同一范围内声明ages时,sizeof才会返回正确答案

sizeof(&ages) / sizeof(&ages[0])

将始终返回1

你真的把事情复杂化了

鉴于最初的问题:

编写一个程序,读取一个数,一个整数和一个小于 键盘输入15个字符。设计程序,使数据 在一个函数中完成,在另一个函数中输出。将数据存储在 主要功能。当为输入零时,程序应结束 号码。想一想你将如何在两个用户之间传递数据 功能

这个问题需要您考虑将参数传递给函数。一个简单的解决办法是:

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

// Pass in a char array and an integer reference.
// These values will be modified in the function
void read_data(char name[], int& age)
{
    cout << endl << "Age: ";
    cin >> age;
    cin.ignore();
    cout << endl << "Name: ";
    cin.getline(name, 16);
}

// Pass a const array and an int value
// These values will not be modified
void print_data(char const *name, int age)
{
    cout << endl << setw(10) << "Name: " << name
            << setw(10) << "Age: " << age;
}
int main()
{
    char name[16];
    int age;

    cout << "Enter names and ages."
        << endl << "Enter 0 age to quit.";

    do {
            read_data(name, age);
        print_data(name, age);
    } while (0 != age)
}
编辑:根据用户3290289的评论进行修改

EDIT2:在数组中存储数据

// Simplify by storing data in a struct (so we don't have to manage 2 arrays)
struct Person {
    char name[16];
    int age;
};

// Returns how many People were input
int read_data(Person*& arr)
{
    int block = 10; // How many persons to allocate at a time
    arr = NULL;
    int arr_size = 0;
    int index = 0;
    while (true) {
        if (index == arr_size) {
            arr_size += block;
            arr = (Person *)realloc(arr, arr_size * sizeof(Person)); // Reallocation
            // Should check for error here!
        }
        cout << endl << "Age: ";
        cin >> arr[index].age;
        cin.ignore();
        if (0 == arr[index].age) {
            return index;
        }
        cout << endl << "Name: ";
        cin.getline(arr[index++].name, 16);
    }
}

void print_data(Person *arr, int count)
{
    for (int i = 0; i < count; i++) {
        cout << endl << setw(10) << "Name: " << arr[i].name
            << setw(10) << "Age: " << arr[i].age;
    }
}

int main()
{
    Person *arr;

    int count = read_data(arr);
    print_data(arr, count);
    free(arr);  // Free the memory
}

我认为解决这个问题的一个自然方法是:

创建一个std::map实例。这里std::map将根据年龄对元素进行排序。这里我的假设是,在将数据存储到容器中之后,您希望了解特定的学生年龄/最小/最大,以及各种各样的数据操作。一般来说,仅仅存储和打印数据没有多大意义

创建一个std::pair,并将用户的两个输入分别放入std::pair的第一个和第二个成员中。现在您可以将这个std::pair实例值插入到上面的std::map对象中

打印时,现在可以以std::pair的形式获取std::map的每个元素,然后可以分别显示pairfirst和secondpart


编辑您的帖子并添加代码。很可能您想使用std::string&for@user3290289“How?”重现您的问题。祝贺您的三星级编程。一个即时的改进是使用修改参数的引用,然后是std::string和std::vector。哦,您的意思是要存储所有的姓名/年龄。所以我的更新不是你想要的…如果不使用STL向量,你可以创建一个简单的链表。或者使用数组,但您必须准备重新调整数组的大小,因为您事先不知道将输入多少个名称。@user3290289好的,再次修改以将数据存储在数组中。感谢您的辛勤工作:,效果很好。稍后我将了解realloc和struct是如何工作的。我想我把事情弄得过于复杂了:我一直在用java编写代码,我知道这真的是一项非常简单的任务,所以我尝试在较低的级别上完成:DD