Making the user input this

Feb 24, 2011 at 9:52pm
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
#include <iostream>

using namespace std;

struct Student{
	char Firstname[20];
	char Lastname[20];
	int grades[4];
	double average; 
	int i, k, n;
	void calgradeaverage(void);
};

void Student::calgradeaverage(void)
{
	average = 0;
	short unsigned int index;
	for(index=0; index<4; index++){
		average+=grades[index];
	}
	average /= 4;
	cout<<average<<endl;
}



int main(void)
{
	struct Student me ={"keith","lin",10,20,30,40};

	
}


well as you see i am hard coding the name and test scores. How to make the user input it? I'm not just asking letting the user input the name and stuff i want the user to be able to input the numbers of objects too.

e.g
how many object you need?
user input: 4
what's your first name?
user input: bla
what's your last name?
user input: bla
what's your first grades score?
user input: bla
what's your second grades score?
user input: bla
what's your third grades score?
user input:bla
what's your fourth grades score?
user input:bla

times four. (it means the asking ur first name last name and test scores times four since the i.e user input for the number of object is 4)

Last edited on Feb 24, 2011 at 9:53pm
Feb 24, 2011 at 10:07pm
Create an array of student type and loop though it asking user for input, also you dont need struct keyword in declaration part in the main.
Feb 24, 2011 at 10:17pm
I thought of that, but go back to my main problem
let's say i am hardcoding only 1 object how to cout cin the name and scores?

Feb 24, 2011 at 10:40pm
I guess we could break this down to getting char and int arrays from the user.

For your students' names you could use this:
http://cplusplus.com/reference/iostream/istream/getline/

If you are allowed to use std::string, do it. I will make things much simpler:
http://cplusplus.com/reference/string/string/

Then, you'll have to use this version of getline instead of the one metioned above:
http://cplusplus.com/reference/string/getline/

As for the grades, it can be done using a for loop.

If your problem is that you don't remember how to access data members of a struct,
this could be of help -> http://cplusplus.com/doc/tutorial/structures/
Feb 25, 2011 at 12:22am
yea but those are simple cin cout i knew it, but how to score it to a class/struct's object?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Student{
	char Firstname[20];
	char Lastname[20];
	int grades[4];
	double average; 
	int i, k, n;
	void calgradeaverage(void);
};

void Student::calgradeaverage(void)
{
	average = 0;
	short unsigned int index;
	for(index=0; index<4; index++){
		average+=grades[index];
	}
	average /= 4;
	cout<<average<<endl;
}

the above is a requirement.
Last edited on Feb 25, 2011 at 12:23am
Feb 25, 2011 at 1:59pm
There is an example in the last link I gave you.
Topic archived. No new replies allowed.