如何将多个用户输入添加到数组中?

发布于 2024-12-11 04:33:07 字数 2621 浏览 0 评论 0原文

如何向数组添加多个字符串或用户输入?我正在尝试创建一个通讯录,要求用户添加最多 10 个联系人。我试图将它们存储为数组或 txt 文件,然后我希望能够使用此输入。

这是我的代码。如果我想说的不清楚,运行代码会有所帮助。

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    // declare two variables;
    char name[20];
    int age;
string ans;
do {    
    // get user to input these;
    cout << "What is your name: ";
    cin >> name;
    cout << "What is your age : ";
    cin >> age;
    cout<<"continue ";cin>>ans;
  }while((ans == "y" || ans=="yes"));  
    // create output stream object for new file and call it fout
    // use this to output data to the file "test.txt"
    char filename[] = "test.txt";
    ofstream fout(filename);
    fout << name << "," << age << "\n";    // name, age to file
    fout.close();   // close file

    // output name and age : as originally entered
    cout << "\n--------------------------------------------------------"
         << "\n name and age data as entered";
    cout << "\n    Your name is: " << name;
    cout << "\n    and your age is: " << age;     

    // output name and age : as taken from file           

    // first display the header
    cout << "\n--------------------------------------------------------"
         << "\n name and age data from file"
         << "\n--------------------------------------------------------";   

      ifstream fin(filename);

       char line[50];               
    fin.getline(line, 50);      


    char fname[20];     
    int count = 0;       
    do
    {
        fname[count] = line[count];       
        count++;
    }
    while (line[count] != ',');        
    fname[count] = '\0';              


    count++;
    char fage_ch[10];    
    int fage_count = 0;   
    do
    {
        fage_ch[fage_count] = line[count];   
        fage_count++; count++;               
    }
    while (line[count] != '\0');             
    fage_ch[fage_count] = '\0';


    int fage_int = 0;        
    int total = 0;           
    char temp;               

    for (int i = 0; i < (fage_count); i++)
    {
        temp = fage_ch[i];
        total = 10*total + atoi(&temp);
    }

    fage_int = total;

    // display data
    cout << "\n\n    Your name is: " << fname;
    cout << "\n    and your age is: " << fage_int;
    cout << "\n\n--------------------------------------------------------";    

      cout <<endl;

    return EXIT_SUCCESS;
}

How can I add more than one string or user input to an array? I am trying to create a contact book that requires the user to add up to 10 contacts. I am trying to store them an an array or a txt file and then later I want to be able to use this input.

Here is my code. If what I'm trying to say is not clear, running the code will help.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    // declare two variables;
    char name[20];
    int age;
string ans;
do {    
    // get user to input these;
    cout << "What is your name: ";
    cin >> name;
    cout << "What is your age : ";
    cin >> age;
    cout<<"continue ";cin>>ans;
  }while((ans == "y" || ans=="yes"));  
    // create output stream object for new file and call it fout
    // use this to output data to the file "test.txt"
    char filename[] = "test.txt";
    ofstream fout(filename);
    fout << name << "," << age << "\n";    // name, age to file
    fout.close();   // close file

    // output name and age : as originally entered
    cout << "\n--------------------------------------------------------"
         << "\n name and age data as entered";
    cout << "\n    Your name is: " << name;
    cout << "\n    and your age is: " << age;     

    // output name and age : as taken from file           

    // first display the header
    cout << "\n--------------------------------------------------------"
         << "\n name and age data from file"
         << "\n--------------------------------------------------------";   

      ifstream fin(filename);

       char line[50];               
    fin.getline(line, 50);      


    char fname[20];     
    int count = 0;       
    do
    {
        fname[count] = line[count];       
        count++;
    }
    while (line[count] != ',');        
    fname[count] = '\0';              


    count++;
    char fage_ch[10];    
    int fage_count = 0;   
    do
    {
        fage_ch[fage_count] = line[count];   
        fage_count++; count++;               
    }
    while (line[count] != '\0');             
    fage_ch[fage_count] = '\0';


    int fage_int = 0;        
    int total = 0;           
    char temp;               

    for (int i = 0; i < (fage_count); i++)
    {
        temp = fage_ch[i];
        total = 10*total + atoi(&temp);
    }

    fage_int = total;

    // display data
    cout << "\n\n    Your name is: " << fname;
    cout << "\n    and your age is: " << fage_int;
    cout << "\n\n--------------------------------------------------------";    

      cout <<endl;

    return EXIT_SUCCESS;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2024-12-18 04:33:07

您可能最好使用结构数组而不是两个单独的数组来存储名称和名称。每个条目的年龄。然后,您可以使用 strcpy 循环将输入字符串从名称复制到结构的名称中。如果您对结构不满意,您也可以使用几个二维数组。

这看起来像是一项家庭作业,所以我不会发布代码,而是提供一个基本算法来帮助您入门(并希望简化您所拥有的内容):

#define MAX_CONTACTS 10
#define MAX_NAME_LENGTH 20

// 2D array to store up to 10 names of max 20 character length
char nameVar[MAX_CONTACTS][MAX_NAME_LENGTH]
int ageVar[MAX_CONTACTS]

do until end of user input
    read name into nameVar[index]
    read age into ageVar[index]
    index += 1
end loop

while contactCounter < index
    ouput nameVar[contactCounter]
    output age[contactCounter]
    // you could also write to file in this loop if thats what you're trying to do
    // using the fprintf function to write to an opened file
    contactCounter += 1
end loop

另外,我不确定您要做什么通过 atoi 调用,但看起来没有必要。 atoi 的工作原理是,它查看传递的第一个字符并转换所有数字,直到遇到数组中的非数字字符。因此,如果您有 char 数组 c="123h" atoi 将返回 123。如果您传递 atoi "1h2" 它将返回 1。

您还可以使用 fprintf 将 char 数组和 int 打印到文件中。

因此,如果您有 int i 和 char s[10] = "hello" 和文件流,您可以打印到流,例如:

fprintf(stream, "my text to display: %s %i", s,i)

我希望这有帮助。

You would probably be better off using an array of structs instead of two seperate arrays to store the name & age for each entry. Then you can just loop through using strcpy to copy the input string from name into your struct's name. If you aren't comfortable with structs you could also use a couple of 2 dimensional arrays.

This looks like a homework assignment so I'm not going to post code, but for a basic algorithm to get you started (and hopefully simplify what you've got):

#define MAX_CONTACTS 10
#define MAX_NAME_LENGTH 20

// 2D array to store up to 10 names of max 20 character length
char nameVar[MAX_CONTACTS][MAX_NAME_LENGTH]
int ageVar[MAX_CONTACTS]

do until end of user input
    read name into nameVar[index]
    read age into ageVar[index]
    index += 1
end loop

while contactCounter < index
    ouput nameVar[contactCounter]
    output age[contactCounter]
    // you could also write to file in this loop if thats what you're trying to do
    // using the fprintf function to write to an opened file
    contactCounter += 1
end loop

Also, I'm not sure what you're trying to do with that atoi call, but it looks like it shouldn't be necessary. How atoi works is that it looks at the first character it is passed and it converts all of the digits until it encounters a non-digit character in the array. So if you have the char array c="123h" atoi would return 123. If you pass atoi "1h2" it will return 1.

Also you can use fprintf to print out both a char array and an int to a file.

So if you've got int i and char s[10] = "hello" and file stream you could print to stream like:

fprintf(stream, "my text to display: %s %i", s,i)

I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文