I can't get the it to output correctly! Please help

When it compiles I am able to enter the name but not the GPA or Major. It skips over them so that the user can't enter data. And then it displays the correct name but the memory address for the GPA and Major.

Here is my code:
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
#include<iostream>
#include<iomanip>
#include<string>
#include<istream>

using namespace std;

const int size = 30;

//Global Structure
struct Student
{
	char Name[30];
	float GPA;
	int Major;
};

// Function Prototype
Student studentData(Student&);


int main()
{
	Student s1;		//s1 is a Student structure
	Student s2;		//s2 is a Student structure

	s2 = studentData(s1);
	

	cout << "Student Name: " << s1.Name << endl;
	cout << "Student GPA: " << s1.GPA << endl;
	cout << "Student Major: " << s1.Major << endl;

	system("Pause");
	return 0;
}

Student studentData(Student& s1)
{
	cout << "Please enter the students name: ";
	cin.getline(s1.Name, 2);
	cin.ignore();
	cout << "Please enter the students GPA: ";
	cin >> s1.GPA;
	cout << "Please enter the students Major: ";
	cin >> s1.Major;
	
	return s1;
}
Last edited on
@lyla

Try using a larger number here.
cin.getline(s1.Name, 2);
You allocated 30 spaces in Name, but only asking for two in the input.
Increase it to the 30.
cin.getline(s1.Name, 30);
closed account (DETpfSEw)
in line No. 41 you use 2 which allow you to get input of only 1 character while second bit is for "\0".

cin.getline(s1.Name, 2);

Try a larger number 15,20 instead of 2. so that your line No. 44 become able to work.

cin.getline(s1.Name, 15);

Enjoy...
Last edited on
i changed it and now it only asks the user to enter the student name and allows me to enter a name but it doesn't go any further.
closed account (3qX21hU5)
Could you post what you changed please?
Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
Student studentData(Student& s1)
{
	cout << "Please enter the students name: ";
	cin.getline(s1.Name, 30);
	cin.ignore();
	cout << "Please enter the students GPA: ";
	cin >> s1.GPA;
	cout << "Please enter the students Major: ";
	cin >> s1.Major;
	

	return s1;
}
Last edited on
Ok I took the cin.ignore out and it runs through them and allows me to input the info and outputs it correctly with exception of the major which give me the memory address. Here is the full code:

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
#include<iostream>
#include<iomanip>
#include<string>
#include<istream>

using namespace std;

const int size = 30;

//Global Structure
struct Student
{
	char Name[30];
	float GPA;
	int Major;
};

// Function Prototype
Student studentData(Student&);


int main()
{
	Student s1;		//s1 is a Student structure
	Student s2;		//s2 is a Student structure

	s2 = studentData(s1);
	

	cout << "Student Name: " << s1.Name << endl;
	cout << "Student GPA: " << s1.GPA << endl;
	cout << "Student Major: " << s1.Major << endl;

	system("Pause");
	return 0;
}

Student studentData(Student& s1)
{
	cout << "Please enter the students name: ";
	cin.getline(s1.Name, 30);
	cout << "Please enter the students GPA: ";
	cin >> s1.GPA;
	cout << "Please enter the students Major: ";
	cin >> s1.Major;
	

	return s1;
}
closed account (3qX21hU5)
Here is your problem int Major;. This should be a string or a char array if your assignment cant use strings. When the user sees "Enter your major" he/she automatically thinks you mean something like "Computer Science", "Math" ect. If you want them to use a number for their major give them some choices like

1 - Math

2 - Science

3 - Plumbing

Or something like that.

Also noticed you make a call to your function s2 = studentData(s1); but you never use s2. This might be something you plan to do later but I thought I might need to explain. If you only wanted to return your function results to s1 you can call it this way studentData(s1);. Also I would recommend changing the &s1 in your function parameters to something else so it doesn't cause confusion with the other s1.
Last edited on
Unfortunately, she has given us the instructions with it in it. I have completed the program but now I have more issues and I am about to throw my computer against the wall. I've been working on this for 3 days now and I know it's not that complicated. Can I submit my code w the instruction under this same thread or do i have to start a new one?
Here is my final code and the instructions. I am not sure if I covered everything correctly. The instructions aren't clear as to what should fully display.
Instructions:
This program will include error trapping with try and catch.

Put a throw in each function which gets user input and throw a string "Bad Major" if a Major of 0 is entered. The input functions are specified in 2, 4 and 7 below.

Create a global structure as follows:

1
2
3
4
5
6
struct Student 
{ 
       char Name[30]; 
       float GPA; 
       int Major; 
};


1. In main create 2 instances of that structure. Call them S1 and S2.

2. Create and call a function named StudentData:
S2 = StudentData( S1 ); //this is the call to the function
The function receives as a parameter a reference to the structure (prototyping will handle this) and will return a reference to the structure. Use couts and cins for getting data from the user. For testing purposes, change the data in S1 so that the GPA is 3.5 and the Major is 2. Since you are to use cins for getting data from the user, you are the user and just enter these values. After the call to the function both S1 and S2 will contain the same data.

3. In main print the data stored in the structures S1 and S2 using cout.

4. Call a function named ChangeData with a pointer to S2 as the argument:
ChangeData( &S2 ); //this is the call to the function
Change the data in S2 so that the GPA is 3.0 and the Major is 1. (Using these values for testing…)

5. Back in main print the data stored in the structure S2 using cout.

6. Now create an array of 2 structures in main. Call the array Students.

7. Create a function, GetStudents, which will receive the array and an int representing the number of elements(2). In the function, loop through the data and get all three fields from the user using cin, cin.getline and cout statements. Organize like this:
1
2
3
4
5
6
7
8
9
10
 for (...........) 
                  { 
                         cout prompt to user 
                         cin.getline for name 
                         cout prompt to user 
                         cin for GPA
                         cout promp to user
                         cin for Major 
                          cin.ignore(1); 
                      }


The problem is that a cin for a numeric value will leave the ENTER key in the keyboard buffer and that is OK with cin and other numbers but not with strings, thus we must remove it on our own. cin.ignore should handle this for us.

8. Call the function GetStudents from main.

9. Create a function, PrintStudents, which will receive the same arguments as GetStudents. It will print out the array of students on 2 lines, 1 line per student.

My Code:
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
#include<iomanip>
#include<string>
#include<istream>

using namespace std;

const int size = 30;

//Global Structure
struct Student
{
	char Name[30];
	float GPA;
	int Major;
};

// Function Prototype
Student studentData(Student&);
Student changeData(Student&);
Student getStudent(Student array[], int size);
Student printStudent(Student getStudent[], int size);

int main()
{
	Student s1;		//s1 is a Student structure
	Student s2;		//s2 is a Student structure

	s2 = studentData(s1);
	s2 = changeData(s2);

	Student getStudent[2], size;

	Student printStudent();

	cout << "Student 1 Name: " << s1.Name << endl;
	cout << "Student 1 GPA: " << s1.GPA << endl;
	cout << "Student 1 Major: " << s1.Major << endl;
	cout << "_______________________________" << endl;
	cout << "Student 2 Name: " << s2.Name << endl;
	cout << "Student 2 GPA: " << s2.GPA << endl;
	cout << "Student 2 Major: " << s2.Major << endl;

	system("Pause");
	return 0;
}

Student studentData(Student& s1)
{
	cout << "Please enter the students name: ";
	cin.getline(s1.Name, 30);
	cout << "Please enter the students GPA: ";
	cin >> s1.GPA;
	cout << "Please enter the students Major: ";
	cin >> s1.Major;
		try
		{
			s1.Major == 0;
			cout << "Bad Major";
		}
		catch (string exceptionString)
		{
			cout << exceptionString;
		}
	return s1;
}

Student changeData(Student& s2)
{
	s2.GPA = 3;
	s2.Major = 1;
		try
		{
			s2.Major == 0;
			cout << "Bad Major";
		}
		catch (string exceptionString)
		{
			cout << exceptionString;
		}
	return s2;
}

Student getStudent(Student getStudent[], int size)
{
	for (int index = 0; index > size; index ++)
	{
		cout << "Please enter the students name: ";
		cin.getline(getStudent[index].Name, 30);
		cout << "Please enter the students GPA: ";
		cin >> getStudent[index].GPA;
		cout << "Please enter the students Major: ";
		cin >> getStudent[index].Major;
			try
			{
			getStudent[index].Major == 0;
			cout << "Bad Major";
			}
			catch (string exceptionString)
			{
				cout << exceptionString;
			}
	
	return getStudent[index];
	}
}

Student printStudent(Student getStudent[], int size)
{
	for (int index = 0; index > size; index ++)
	{
		cout << "Name: " << getStudent[index].Name << "GPA: " << getStudent[index].GPA << "Major: " << getStudent[index].Major << endl;
	}
}
closed account (3qX21hU5)
Ok I think your going to fast through the steps. Trying going back to Step 3. Which what you have here is correct.

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
#include<iostream>
#include<iomanip>
#include<string>
#include<istream>

using namespace std;


//Global Structure
struct Student
{
	char Name[30];
	float GPA;
	int Major;
};

// Function Prototype
Student studentData(Student&);

int main()
{
	Student s1;		//s1 is a Student structure
	Student s2;		//s2 is a Student structure

	s2 = studentData(s1);


	cout << "Student 1 Name: " << s1.Name << endl;
	cout << "Student 1 GPA: " << s1.GPA << endl;
	cout << "Student 1 Major: " << s1.Major << endl;
	cout << "_______________________________" << endl;
	cout << "Student 2 Name: " << s2.Name << endl;
	cout << "Student 2 GPA: " << s2.GPA << endl;
	cout << "Student 2 Major: " << s2.Major << endl;

	return 0;
}

Student studentData(Student& s1)
{
	cout << "Please enter the students name: ";
	cin.getline(s1.Name, 30);
	cout << "Please enter the students GPA: ";
	cin >> s1.GPA;
	cout << "Please enter the students Major: ";
	cin >> s1.Major;
		try
		{
			s1.Major == 0;
			cout << "Bad Major";
		}
		catch (string exceptionString)
		{
			cout << exceptionString;
		}
	return s1;


}


Now for number 4 I see a few things but mostly look good. Ill comment on some things in the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Student changeData(Student& s2) // Get in the habbit of naming your your parameters 
{                               // Something different then a varible you already have its confusing
	s2.GPA = 3;
	s2.Major = 1;
		try                     // Dont really need this since you are changing it not the user.
		{                       // But you can keep it in.
			s2.Major == 0;
			cout << "Bad Major";
		}
		catch (string exceptionString)
		{
			cout << exceptionString;
		}
	return s2;
}


Now for number 5. This is where it started going wrong. They want you to print the data you changed again. So put your call to changeData() below your cout statements you already have. And remmber when you want to call a function like changeData() you can call it like this changeData(s2); which will do the same thing as s2 = changeData(s2);. Let me explain this a little more. When you use a reference (the &) in a function parameter like changeData(Students &s2) you are changing whatever variable you pass into the function, which in this case is s2. Like this example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int myfunction(int &n);

int main()
{
    int mynumber = 0; // mynumber is 0 right now
    cout << mynumber << endl;   // Shows that mynumber is 0
    
    myfunction(mynumber);       // This is my function call. So whatever int variable I put between
                                // the (..) will change to 5 because I used the &.
    cout << mynumber << endl;
}

int myfunction(int &n)
{
    n = 5;
    return n;
}


Then after your changeData() call print the data in s2 again in main.

Now for number 6. Student students[2]; that will be your array of 2 student structs. Make sure to name it Students like they said to not getStudents.

Number 7. Your function looks good except for the use of getStudent[] for a parameter (Remmber dont use names that you have already used for a variable or anything else if possible. Your function call can look something like this.

1
2
3
    Student students[2];    // A array that holds 2 student structs

    getStudent(students, 2); // Note you can use a literal int as a parameter. 


Hope this helps and makes sense I'm not the best at explaining things, and wasn't really sure where you were stuck at. But you seem capable of doing the rest without to much problems. Just keep working at it and if you have any questions ask and we can try and help with it.
Last edited on
Topic archived. No new replies allowed.