Char数组指针(cin& cout) [英] Pointer To Char Array (cin & cout)

查看:83
本文介绍了Char数组指针(cin& cout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,..



只是为了刷新我的概念我正在研究并行阵列。一个用于存储整数数据,另一个用于char数据,即GPA。



问题编译类似魅力,但结果不正确显示学生ID正确但不是GPA那就是问题。



- 简单的cin工作正常

- 我真的不知道如何使用cin .get和cin.getline使用指针。 ??

- 请帮帮我,在输入函数中我想得到2个char长字符串+1 for null char。

- 代码如下



Hello Guys, ..

just to refresh my concept i'm working on parallel arrays. One is used to store integer data and the other one for char data i.e GPA .

The problem compiles like a charm but the result is not correct it displays the Student IDs correctly but not the GPA that's the problem.

- The simple cin works fine
- I don't really know how to use cin.get and cin.getline using pointers. ??
- Please help me in that, Here in enter function I want to get the 2 char long string +1 for null char.
- Code Below

#include <iostream>
#include <cstring>
using namespace std;

void enter(int *ar, char *arr, int size);
void exit(int *a, char *yo, int size);

int main()
{
   const int id = 5;
   const char grade = 5;
    int *student = new int[id];
    char *course = new char[grade];
    cout << "\n";

    enter(student, course, 5);
    exit(student, course, 5);

}

void enter(int *ar, char *arr, int size)
{
    for(int i = 0; i < size; i ++)
    {
        cout << "Student ID: " << i+1 << "\n";
        cin >> *(ar+i);
        cin.ignore();
        cout << "Student Grade: " << i+1 << "\n";
        cin.get(arr, 3);
    }
}

void exit(int *a, char *yo, int size)
{
    for(int i = 0; i < size; i ++)
    {
        cout << "ID And Grade Of Student #" << i+1 << ":";
            cout << *(a+i) << "\t" << *(yo+j) << endl;
    }
}





版本_2(更新)





Version_2 (updated)

#include <iostream>
#include <cstring>
using namespace std;

void get_data(int *ar, char *arr, int size);
void show_data(int *a, char *yo, int size);

int main()
{
   const int id = 5;
   const char grade = 5;
   const int arraySize = 5;
    int *student = new int[id];
    char *course = new char[grade];
    cout << "\n";

    get_data(student, course, arraySize);
    show_data(student, course, arraySize);

	return 0;

}

void get_data(int *ar, char *arr, int size)
{
    for(int i = 0; i < size; i ++)
    {
        cout << "Student ID: " << i+1 << "\n";
        cin >> *(ar+i);
        cin.ignore();
        cout << "Student Grade: " << i+1 << "\n";
        //cin.get(arr, 3);
		cin.get(*(arr+i));
    }
}

void show_data(int *a, char *b, int size)
{
    for(int i = 0; i < size; i ++)
    {
        cout << "ID And Grade Of Student #" << i+1 << ":";
            cout << *(a+i) << "\t" << *(b+i) << endl;
    }
}

推荐答案

如果学生成绩是单个字符,那么你必须更换

If the student grade is a single character, then you have to replace
Quote:

cin.get(arr,3);

cin.get(arr, 3);

with

cin.get(*(arr+i));





请注意



Please note

引用:

cout<< *(a + i)<< \t<< *(yo + j)<< endl;

cout << *(a+i) << "\t" << *(yo+j) << endl;



您应该用 i 替换 j 以便编译。


CPallini已经展示了如何修复你的程序。这里有一些关于编程风格的提示可能对你有所帮助。



良好的命名在程序中非常重要。在您的示例中,一些名称具有误导性:

CPallini has already shown how to fix your program. Here are a couple more hints about programming style that might help you along your way.

Good naming is very important in a program. In your example, some of the names are misleading:
const int id = 5;
const char grade = 5;



如何命名它们 arraySize 。而你只需要一个,因为通过设计,两个数组应该总是具有相同的长度。一旦我们定义了这个漂亮的常量,为什么不在指定数组大小的所有地方使用它:


How about naming them arraySize. And you just need one, because by design both arrays should always have the same length. And once we have defined this nice constant, why not use it all places where the array size is specified:

enter(student, course, arraySize);
exit(student, course, arraySize);



顺便说一下,退出是该功能的一个非常具有误导性的名称。你当然打算把它称为 show_data 或类似的东西。



C中最棘手的事情之一是数组和指针有某种关联,每个C教科书都会告诉你表达式*(ar + i)与ar [i]完全相同。只是,后者更直观。那么我们为什么不写:


By the way, exit is a very misleading name for that function. You certainly meant to call it show_data or something similar.

One of the tricky things in C is that arrays and pointers are somehow related and every C textbook will tell you that the expression "*(ar+i)" is exactly the same as "ar[i]". Only, the latter is a lot more intuitive. So why don't we write:

cin >> ar[i];






and

cout << a[i] << "\t" << yo[i] << endl;



还有很多事情需要改进;例如,两个函数(ar,哟......)的参数名称都很直观。



希望能帮助您改进程序。


There are still more things to improve; for example the parameter names of the two functions (ar, yo ...) are all but intuitive.

Hope that helps you improve your programs.


#include< iostream>

#include< cstring>

使用命名空间std;



void enter(int * ar,char * arr,int size);

void exit(int * a,char * yo,int size);



int main()

{

const int id = 5;

const char grade = 5;

int * student = new int [id];

char * course = new char [grade];

cout<< \ n;



输入(学生,课程,5);

退出(学生,课程,5);



}



void enter(int * ar,char * arr,int size)

{

for(int i = 0; i< size; i ++)

{

cout<< 学生证:<< i + 1<< \ n;

cin>> *(ar + i);

cin.ignore();

cout<< 学生成绩:<< i + 1<< \ n;

cin.get(arr,3);

}

}



void exit(int * a,char * yo,int size)

{

for(int i = 0; i< size; i ++)

{

cout<< ID和学生成绩#<< i + 1<< :;

cout<< *(a + i)<< \t<< *(yo + j)<< endl;

}

}
#include <iostream>
#include <cstring>
using namespace std;

void enter(int *ar, char *arr, int size);
void exit(int *a, char *yo, int size);

int main()
{
const int id = 5;
const char grade = 5;
int *student = new int[id];
char *course = new char[grade];
cout << "\n";

enter(student, course, 5);
exit(student, course, 5);

}

void enter(int *ar, char *arr, int size)
{
for(int i = 0; i < size; i ++)
{
cout << "Student ID: " << i+1 << "\n";
cin >> *(ar+i);
cin.ignore();
cout << "Student Grade: " << i+1 << "\n";
cin.get(arr, 3);
}
}

void exit(int *a, char *yo, int size)
{
for(int i = 0; i < size; i ++)
{
cout << "ID And Grade Of Student #" << i+1 << ":";
cout << *(a+i) << "\t" << *(yo+j) << endl;
}
}


这篇关于Char数组指针(cin&amp; cout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆