gpt4 book ai didi

c++ - 如何将多个用户输入添加到数组?

转载 作者:行者123 更新时间:2023-11-28 03:45:22 26 4
gpt4 key购买 nike

如何向数组添加多个字符串或用户输入?我正在尝试创建一个通讯录,要求用户最多添加 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;
}

最佳答案

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

这看起来像是一项家庭作业,所以我不打算发布代码,而是为了让您入门的基本算法(并希望简化您所拥有的):

#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 的工作原理是它查看传递给它的第一个字符,然后转换所有数字,直到遇到数组中的非数字字符。因此,如果您有字符数组 c="123h",atoi 将返回 123。如果您传递 atoi "1h2",它将返回 1。

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

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

fprintf(stream, "我要显示的文本: %s %i", s,i)

希望对您有所帮助。

关于c++ - 如何将多个用户输入添加到数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7855116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com