Plz Help!!!!!!!!

Pages: 123
closed account (3qX21hU5)
It is just a parameter name you can have it whatever you want. sArray can mean studentArray.

If you don't know about parameters I would highly suggest you do a total reread on everything functions, arrays, and variables. You need to know these things not just for your assignment but for everything you do in programming

@Yanson Please don't just hand over complete programs! It doesn't help the orginal poster if he just copies and pastes a complete program and calls it done on his assignment. If you want to show him how things work in a program use comments and explain what things are doing and leave stuff out for him to complete.

EDIT: @Vidimas the same goes for you please don't hand over complete programs!
Last edited on
Zereo wrote:
Vidimas
lol, ok whatever...

Sorry for writing the whole program, I missed out the fact that it was an assignment.
I hope the OP can learn from the code I wrote...

Anyway the OP said he needs the info to be output as a table, so he has some adjusting to do in the program I wrote.
Last edited on
thank u all :), specially @Yanson
@Zereo i know that he shouldn't write the whole program ,but believe me, i used all the comments to write my own program and the only thing i was need is how to make it as a table , believe me.

Again thank u all... :)
Last edited on
@zero I only slightly edited his original program most of the work was done by the op. I only gave him a complete program becouse you and vidminas were confusing him/her by talking about stuff that was over his/her head
i have finished,but there is a little problem . it's obvious that i used an array of 20 characters(elements),,,,,,,
if i entered a name consist of 19 char it's ok and the program works correctly
but if i entered the full 20 char it simply crashes don't no why
so any help here
closed account (3qX21hU5)
Could you show the code where you put your input into your array? Remember that a array that looks like this myArray[20] has the range 0 - 19. It starts at 0 and ends at 19. It sounds like you are trying to write past the range of your array, but need to see your code to confirm that.
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>
#include<iomanip>
using namespace std;

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

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');
	cin.get();

}

void DataIn(char Names[][20],int N,long Academic[10],int A,float Grade[10],int G)
{

	cout<<"Enter students names: \n";
	for(int row=0;row<10;row++)
	{
		cin.getline(Names[row],N);
	}

	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 N,long Academic[10],int A,float Grade[10],int G)
{
        cout << left;
        cout <<"\n"<<setw(10) << "Serial" << setw(25) << "Name" << setw(15) << "Academic No." << setw(10)
             << "Grade" << endl << endl;
	for(int i=0;i<10;i++)
	{
		cout << setw(10) << (i + 1) << setw(25) << Names[i] << setw(15) << Academic[i] << setw(10) << Grade[i] << endl;
	}

}
and if i entered 20 char the output is like this
enter students names:
abcdefghijklmnopqrst //the name i entered
enter thier academib number:
enter thier Grades:

Serial 		Names		Academic No		Grade
1	  abcdefghijklmnopqrst			    random values
2							.
3							.
4							.
5
6
7
8
9
10
Last edited on
You write 20 characters and hit enter. Getline takes 19 and adds null. Next getline takes the one remaining char and adds null. Two names have been entered.

At what point does it crash?
after entering a name with 20 char and hit enter to enter the next name it's just give me the output i posted , and do not let me enter the remaining informations like academic and grades.
Last edited on
closed account (3qX21hU5)
Remember c strings are null terminated. Meaning when you enter a word that is 19 characters long and press enter you are really entering a 20 character word because of the null char it adds to the end.

This is why std::string's are much better to work with then a char array or c string
as i told you before i haven't studied strings yet,,, but if you want write the same code using the strings for me may be i can understand it.
As Zereo pointed out, we shouldn't write full code for you, if this is homework.
You can look back to the program I wrote before. It uses strings.
closed account (3qX21hU5)
Lol so you can copy and paste mine for your assignment? No thanks ;p

Here I'll demostrate strings for you. They really are very very simple.

1) At the tops makes sure you #include <string>

2) To declare a string variable just do this. string myString; there now you have a string that can hold anything that a char array can hold.

3) To help explain this look at this example.

Instead of doing this

1
2
char name[20];
cin >> name;


you can do this

1
2
string name;
cin >> name;


They might look the same, but they are much different. For one the char array can only hold up to 20 characters, where as the string can hold as many characters as you want.

Other benifits are that std::string has way more functionality then a char array. Like if you want to find out how many characters are in a string you can do this.

1
2
3
4
string name;
cin >> name;

cout << "There are " << name.length() << " characters in the string!";
hhhhhhh :)
ok how can i pass the string to a function....
closed account (3qX21hU5)
Yup that would look like this

1
2
3
4
void printMyString(string word)
{
    cout << word << endl;
}


This is obviously a trivial useless function but it demonstrates the point. Whatever string you pass to the parameter word will be printed.
std::string is a type. int is a type. Both can be passed to a function the same way.

Multi-word names have to be read with getline. >> with the default skipws won't do.
ok it work.
but i want to enter 10 names using for loop,so if i print them they shoud be printed with the same order ! but here the output is random sympols.
another thing in the code of the char array when i was press enter with out entering any thing it takes it as a blank name i want that with the strings.

this is the code after using strings
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
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

void DataIn(string Names,long Academic[10],int A,float Grade[10],int G);
void DataOut(string Names,long Academic[10],int A,float Grade[10],int G);

main()
{
	string Names;
	long Academic[10];
	float Grade[10];
	DataIn(Names,Academic,10,Grade,10);
	DataOut(Names,Academic,10,Grade,10);

	cin.ignore(100, '\n');
	cin.get();

}

void DataIn(string Names,long Academic[10],int A,float Grade[10],int G)
{

	cout<<"Enter students names: \n";
	for(int row=0;row<10;row++)
	{
		cin>>Names;
	}

	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(string Names,long Academic[10],int A,float Grade[10],int G)
{
        cout << left;
        cout <<"\n"<<setw(10) << "Serial" << setw(25) << "Name" << setw(15) << "Academic No." << setw(10)
             << "Grade" << endl << endl;
	for(int i=0;i<10;i++)
	{
		cout << setw(10) << (i + 1) << setw(25) << Names[i] << setw(15) << Academic[i] << setw(10) << Grade[i] << endl;
	}

}
closed account (3qX21hU5)
string Names;

You are only storing 1 person's name. Remember a string is only 1 variable. If you want to hold a collection of names like your char names[][] variable you will need to make a array of string which looks like this.

string names[10];

That will be array that can hold 10 strings or in your case 10 names.

In your dataIn function change the

cin >> names;

to cin >> names[i]

And you should be good to go.


P.S

Just wanted to explain what this loop was doing when you called Names[i] with your current string Names;, this won't happen after you make the changes above.

1
2
3
4
for(int i=0;i<10;i++)
	{
		cout << setw(10) << (i + 1) << setw(25) << Names[i] << setw(15) << Academic[i] << setw(10) << Grade[i] << endl;
	}


What happens in this loop when it gets to the point where it prints Names[i], is it will print the ith character in the string each time through the loop.

For example lets say we entered "Jake" for the name. Here is what it would print every time through the loop.

1st Time through: "J"
2nd Time through: "A"
3rd Time through: "K"
4th Time through: "E"

6 - 10th time through: It will print a random char or symbol or crash because you are trying to access a place in the string that is not there (The word only has 4 characters and a null terminator).
Last edited on
with this i pass void myname(string Names,10)
Pages: 123