Plz Help!!!!!!!!

Pages: 123
Write a program create a database for a class consisting of 10 students then prints it as a table.
Information needed are student name (array of 20 characters), academic number (long) and total assessment (float). Data should be entered by the user.
Use a function called DataIn() to enter data. Also use a function called DataOut() to print the table which should look like this:
Serial Name Academic No Total grade
1 Ali Hassan 21078767 70.9
2 Hoda Ahmed 25413625 87.5
……etc..

Can't get it right .... any one help me ..!!!!
If you cannot get it right, then show us your code that does it wrong.
this not the full code ... but i have a problem with the student name array
don't know if it is a 2 dimensional array or one dimension Array


#include<iostream>
using namespace std;

void DataIn(char Names[20],int M,long Academic[10],int N,float Grade[10],int L);
void DataOut(char Names[20],int M,long Academic[10],int N,float Grade[10],int L);
main()
{
char Names[20];//Problem
long Academic[10];
float Grade[10];
DataIn(Names,20,Academic,10,Grade,10);
DataOut(Names,20,Academic,10,Grade,10);

}
void DataIn(char Names[20],int M,long Academic[10],int N,float Grade[10],int L)
{
cout<<"Enter students names: \n";
for(int row=0;row<10;row++)//Problem
{
cin.getline(Names[row],M);
}
cout<<"Enter the Academic Numbers: \n";
for(int i=0;i<10;i++)
cin>>Academic[i];
cout<<"Enter thier Grades: \n";
for(int k=0;k<10;k++)
cin>>Grade[k];
}
void DataOut(char Names[20],int M,long Academic[10],int N,float Grade[10],int L)
{
for(int row=0;row<10;row++)
{
cout<<Names[row]<<endl;
}
for(int i=0;i<10;i++)
cout<<Academic[i]<<endl;
for(int k=0;k<10;k++)
cout<<Grade[k]<<endl;
}

I think it will be much easier if you use a structure or a class
sorry but i haven't studied them yet , the last thing i studied last week is the arrays
Mohammed Khalid wrote:
don't know if it is a 2 dimensional array or one dimension Array
char Names[20]; is a 1D array.
char Names[20][20]; would be a 2D array.
for ( int row = 0; row < 10; row ++ )

Why do you have 20 names and only get input for 10 chars?

You never really said what your problem is.

It is generally good to space out your code.
Last edited on
this is my problem ,, every thing about the Names array in the code maybe wrong because i don't know how to declare it in the first place
so just read the question and tell my how would you declare it.
Well if you really want to use arrays, you should still look up structures, classes and vectors first.
Then an array of chars is designed to hold multiple chars. A char is one letter or symbol. So an array of chars should be used for holding a word (in this case you should usually use std::string instead)
I would use an array of strings to hold multiple names:
std::string Names[20];

You don't need to write std:: if you have using namespace std; at the top of the program, but search these forums why it is a bad practice.
Last edited on
could you write it as a code please
The only thing you need to do is to replace char Names[20]; with std::string Names[20]; as far as I see. You might need to add #include <string> after #include <iostream>
ok, the for loop should stay as the same or what
Yep, it's fine. I think you should try to compile the code after updating it with suggestions, before asking!
Anyway, I would probably write
1
2
3
4
5
for (unsigned short student = 0; student < 10; student ++)
{
    std::cout << "Student " << student + 1 << ":";
    std::cin.getline( Names[student], M );
}
having this error
error: no matching function for callto'std::basic_istream<char>::getline(std::string&, int&)'|
Sorry, I can't compile code right now, otherwise I would have told you.
Apparently it doesn't like std::cin.getline( Names[student], M ); try std::cin.getline( Names[student] ); instead.

And I think, from your error, you didn't replace char Names[20] by std::string Names[20] everywhere. You have to change it in functions' parameters as well.
Last edited on
the same.
forget it now, how can i output the entered Data as a table like it say's in the question

like this
Serial Name Academic No Total grade
1 Ali Hassan 21078767 70.9
2 Hoda Ahmed 25413625 87.5
……etc..
closed account (3qX21hU5)
Here is a semi complete version demonstrating how you can use structs to make it much much more simple to complete. I didn't do the other function for you and you will need to add more functionality to the program to actually have it complete but it is a start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

using namespace std;

// This is our struct. You can think of it like a variable type
// that holds a char array, long integer, and a float.
struct Student
{
    char name[20];
    long academicNumber;
    float totalAssess;

};

// These are the function prototypes
void DataIn(Student sArray[]);
void DataOut(Student sArray[]);

// 10 Studenst this will be the size of our array
const size_t SIZE = 10;

int main()
{
    Student students[SIZE];

    DataIn(students);

    // This is where you do data out to display the info.

}

void DataIn(Student sArray[])
{
    // Gets use input to enter in all info on the every student.
    for (int i = 0; i != SIZE; ++i)
    {
        cout << "Enter the students name: ";
        cin >> sArray[i].name;

        cout << "Enter the students academic number: ";
        cin >> sArray[i].academicNumber;

        cout << "Enter the total assessment: ";
        cin >> sArray[i].totalAssess;
    }
}
Information needed are student name (array of 20 characters),
the last thing i studied last week is the arrays

sounds like you should be using arrays then, not strings or structs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;

void DataIn(char Names[][20],int M,long Academic[10],int N,float Grade[10],int L);
void DataOut(char Names[][20],int M,long Academic[10],int N,float Grade[10],int L);

int main()
{
	char Names[10][20] = {' '};
	long Academic[10];
	float Grade[10];
	DataIn(Names,20,Academic,10,Grade,10);
	DataOut(Names,20,Academic,10,Grade,10);

	cin.ignore(100, '\n');
	return 0;

}

void DataIn(char Names[][20],int M,long Academic[10],int N,float Grade[10],int L)
{
	
	cout<<"Enter students names: \n";
	for(int row=0;row<10;row++)//Problem
	{
		cin.getline(Names[row],M);
	}
	
	cout<<"Enter the Academic Numbers: \n";
	for(int i=0;i<10;i++)
	{
		cin>>Academic[i];
		cin.ignore();
	}

	cout<<"Enter thier Grades: \n";
	for(int k=0;k<10;k++)
	{
		cin>>Grade[k];
		cin.ignore();
	}
}

void DataOut(char Names[][20],int M,long Academic[10],int N,float Grade[10],int L)
{
	for(int i=0;i<10;i++)
	{
		for(int j = 0; j < 20; j++)
		{
			cout << Names[i][j];
		}
		
		cout << " " << Academic [i] << " " << Grade[i] << endl;
	}
	
}
Last edited on
sArray [] ,, ?????? what is this?
I wrote a new program, based on your code, it's a bit more complicated in some parts, but way better in my opinion. I also added unexpected input handling, for example: if the user inputs "hello" when the program asks for the grade.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <sstream>

void DataIn ( unsigned short STUDENTS, std::string Name[20], long Academic[20], float Grade[20] );
void DataOut( unsigned short STUDENTS, std::string Name[20], long Academic[20], float Grade[20] );

int main()
{
    std::string Name[20];
    long Academic[20];
    float Grade[20];
    unsigned short STUDENTS;
    std::string input = "";

    while (true)
    {
        std::cout << "How many students will you enter the data for? (MAX = 20)" << "\n";

        getline(std::cin, input);
        std::stringstream myStream(input);
        if ( myStream >> STUDENTS ) break;
        else std::cout << "Invalid number, please try again" << "\n\n";
    }

    DataIn ( STUDENTS, Name, Academic, Grade );
    DataOut( STUDENTS, Name, Academic, Grade );
}

void DataIn ( unsigned short STUDENTS, std::string Name[20], long Academic[20], float Grade[20] )
{
    std::string input = "";
    for ( unsigned short student = 0; student < STUDENTS; student ++ )
    {
        std::cout << "\n\n" << "Student " << student + 1 << "\n";

        std::cout << "\n" << "Enter the student's name:" << "\n";
        getline(std::cin, Name[student]);

        while (true)
        {
            std::cout << "\n" << "Enter the Academic Number:" << "\n";

            getline(std::cin, input);
            std::stringstream myStream(input);
            if ( myStream >> Academic[student] ) break;
            else std::cout << "Invalid number, please try again" << "\n\n";
        }

        while (true)
        {
            std::cout << "\n" << "Enter the Grade:" << "\n";

            getline(std::cin, input);
            std::stringstream myStream(input);
            if ( myStream >> Grade[student] ) break;
            else std::cout << "Invalid number, please try again" << "\n\n";
        }
    }
}

void DataOut ( unsigned short STUDENTS, std::string Name[20], long Academic[20], float Grade[20] )
{
    std::string input = "";
    for ( unsigned short student = 0; student < STUDENTS; student ++ )
    {
        std::cout << "\n" << "Student " << student + 1 << "\n\n";

        std::cout << "Name: " << Name[student] << "\n";
        std::cout << "Academic Number: " << Academic[student] << "\n";
        std::cout << "Grade: " << Grade[student] << "\n";
    }
}
Last edited on
Pages: 123