Click here to Skip to main content
15,888,157 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
#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;
    }
}


Version_2 (updated)

C++
#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;
    }
}
Posted
Updated 29-Oct-13 22:00pm
v3
Comments
Aescleal 30-Oct-13 4:33am    
Why are you using pointers, dynamic memory allocation and raw character arrays when std::vector and std::string would make your life a lot easier? You're writing too much low level code for the scale of the problem.

If the student grade is a single character, then you have to replace
Quote:
cin.get(arr, 3);
with
C++
cin.get(*(arr+i));


Please note
Quote:
cout << *(a+i) << "\t" << *(yo+j) << endl;

you should replace j with i in order to compile.
 
Share this answer
 
Comments
Usman Hunjra 30-Oct-13 3:59am    
yes sir, it works like a charm ..
but i actually want to enter more then one characters..
please help me in that ..
PLEASE ...
CPallini 30-Oct-13 4:53am    
Why dont you use std::string, then? It works like a charm with std::cin... :-)
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:
C++
const int id = 5;
const char grade = 5;

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:
C++
enter(student, course, arraySize);
exit(student, course, arraySize);

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:
C++
cin >> ar[i];

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

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.
 
Share this answer
 
v3
Comments
Usman Hunjra 30-Oct-13 4:02am    
yes i know it .. but i want to do it in pointers ...
please guide me how can i get more characters. Also by the use of cin.get() NOT getline(). ?!
because getline gets the whole line of input. . .
nv3 30-Oct-13 4:13am    
I'd start by using an array of std::string for the course names, instead of an array of char.

cin >> courseName[i];

will work nicely. Or if you want to do it with pointer instead of array indices:

std::string* pCourseName = &courseNames[0];
for ( .... )
{
...
cin >> *pCourseName++;
...
}
Usman Hunjra 30-Oct-13 4:20am    
yes that's nice but what if i use pointer to char and use cin.get()
actually this concept is stuck in my mind. I want to clarify it first..
nv3 30-Oct-13 4:39am    
That's of course possible, but unnecessarily burdensome. You would declare the course name array as two-dimensional array:

char courseNames[maxNameLen][maxNumCourses];

and read each course name by

cin.get (&courseNames[0][i], maxNameLen);

CPallini 30-Oct-13 4:52am    
5.
#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;
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900