C++ 在函数中,将值放入字符数组,以及如何使用需要更新的实例更新结构
在独立函数中,如何将值放入字符数组,以及单独的独立函数如何访问结构中应该更新的实例C++ 在函数中,将值放入字符数组,以及如何使用需要更新的实例更新结构,c++,arrays,pointers,struct,C++,Arrays,Pointers,Struct,在独立函数中,如何将值放入字符数组,以及单独的独立函数如何访问结构中应该更新的实例 #include <iostream> using namespace std; struct Student{ int numRes, numNonRes; Student() { numRes = 0; numNonRes = 0; }; void
#include <iostream>
using namespace std;
struct Student{
int numRes, numNonRes;
Student()
{
numRes = 0;
numNonRes = 0;
};
void displayResCount()
{
cout << "Resident students: " <<numRes;
cout << "Non resident students: " <<numNonRes;
}
};
带注释的主要功能
int main()
{
//Declare a character pointer called resArray.
//This will be a character array
char* resArray;
int size;
cout <<"Enter the number of input values: ";
cin >> size;
Student stu;
resArray = new char[size];
//assign to each element of resArray the character
//inputted by the user.
inputArray (resArray, size);
//assign values to the menber variables
//numRes and numNonRes of stu based on the values in resArray.
countValue (resArray, size);
//called to display the number of resident and
//nonresident students. (member function of stu)
for (int x = 0;x < size;x++)
stu.displayResCount();
delete[] resArray;
return 0;
}
intmain()
{
//声明一个名为resArray的字符指针。
//这将是一个字符数组
字符*重新排列;
整数大小;
cout大小;
学生斯图;
重新排列=新字符[大小];
//将角色指定给重排列的每个元素
//由用户输入。
输入阵列(重阵列、大小);
//为menber变量赋值
//stu的numRes和numNonRes基于resArray中的值。
countValue(重新排列、大小);
//调用以显示常驻和
//非居民学生(stu成员函数)
用于(int x=0;x
用户应该在这里输入答案,并存储在char*resArray中
void inputArray(char* resArray, int size)
{
char choice;
for (int i=0; i < size; i++)
{
cout << "Enter r or n: ";
cin>> resArray[i]-> choice;
}
return;
}
void inputArray(char*resArray,int size)
{
字符选择;
对于(int i=0;iresArray[i]->选择;
}
返回;
}
这些值需要发送到struct Student以输出值
int countValue(char resArray, int num)
{
for (int i=0; i < num; i++)
{
if (resArray[i] == 'r')
Student->numRes ++;
if (resArray == 'n')
Student->numNonRes ++;
}
return;
}
int countValue(字符重排列,int num)
{
for(int i=0;inumRes++;
如果(重新排列=='n')
学生->numNonRes++;
}
返回;
}
<>代码>好的,我首先要说的是C++中,除非教授在制造你,否则不要使用结构。p>
使用类,它们会使您正在做的事情变得更简单
我会将您的学生结构重组为:
class student {
private:
int numRes;
int numNonRes;
public:
student() { numRes = 0; numNonRes = 0; }
void displayResCount() {
cout << ....;
}
int getNumRes() {return numRes;}
int getNumNonRes() {return numNonRes;}
}
class student {
private:
int numRes;
int numNonRes;
public:
student() { numRes = 0; numNonRes = 0; }
void displayResCount() {
cout << ....;
}
int getNumRes() {return numRes;}
int getNumNonRes() {return numNonRes;}
}
resArray = new char[size];
//assign to each element of resArray the character
//inputted by the user.
inputArray (resArray, size);
//assign values to the menber variables
//numRes and numNonRes of stu based on the values in resArray.
countValue (resArray, size);
//called to display the number of resident and
//nonresident students. (member function of stu)
for (int x = 0;x < size;x++)
stu.displayResCount();
delete[] resArray;
return 0;
}
int countValue(char resArray, int num)
{
for (int i=0; i < num; i++)
{
if (resArray[i] == 'r')
stu.numRes ++;
if (resArray == 'n')
stu.numNonRes ++;
}
return;
}
int countValue(char resArray, int num, Student &s)
{
for (int i=0; i < num; i++)
{
if (resArray[i] == 'r')
s.numRes ++;
if (resArray == 'n')
s.numNonRes ++;
}
return;
}
countValue (resArray, size, stu);