simple cin and cout

Hi,

I'm trying to print name: and Grade: on the same line. how can I do that?


1
2
3
4
5
6
7
8
9
10
11
  string studentName;
	int studentGrade = 0;
	Student Student_1(&studentName, &studentGrade);

	for (int i = 0; i < *numOfStudents; i++){
		cout << "Name " << ": ";
		cin >> studentName;
		//cout << string(100, '\n');
		cout << "Grade " << ":";
		cin >> studentGrade;
	}
It is not possible - the console decides how to handle user input. You would have to hook directly into the console to override its behavior, but there are am multitude of consoles/shells and not all allow this.
Thanks for the quick reply!
project requirements says get student Name and grades from the user and store them into array of object by calling the class member function.

here's my main ccp so far.
am I on the right track?
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
#include <iostream>
#include <string>

#include "Student.h"
using namespace std;

const int CAPACITY = 1000;

int main(){
	int *numOfStudents;
	numOfStudents = new int[CAPACITY];//points to an unknown number of students.Allocates memory of 
	//1000 array of type int and store the base address of array in numOfStudent.

	cout << " Please enter the number of students " << ":";
	cin >> *numOfStudents;
	cout << "Please enter student data" << ":" <<endl;

	//Student numOfStudents[CAPACITY];

	string studentName;
	int studentGrade = 0;
	Student Student_1(&studentName, &studentGrade);

	for (int i = 0; i < *numOfStudents; i++){
		cout << "Name " << ": ";
		cin >> studentName;
		//cout << string(100, '\n');
		cout << "Grade " << ":";
		cin >> studentGrade;
	}
	cout << "Class Average " << ":";


	for (int i = 0; i < *numOfStudents; i++){
		Student_1.avgGrade();
		cout << "Average Grade: " << Student_1.getGrade() << endl;
	}


	for (int i = 0; i < *numOfStudents; i++){
		Student_1.gradeDistribution();
		cout << "Grade Distribution: " << Student_1.getGrade() << endl;
	}


	system("pause");
	return 0;
}
Well, I'd have to say no, because your requirements use the phrase "class member function" and your code has no classes. Check out the tutorials on this site if you think you missed a lecture:
http://www.cplusplus.com/doc/tutorial/classes/
well I did,

here is my student.cpp and h file

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
#include "Student.h"

Student::Student(){
	newGrade;
}

Student::Student(string* studentName, int* studentGrade){
	newName = *studentName;
	newGrade = *studentGrade;
}

Student::~Student(){

}

int* Student::avgGrade(){
	//newGrade
	return 0;
}

void Student::gradeDistribution(){

}

string Student::getName(){
	return newName;
}

int Student::getGrade(){
	return newGrade;
}



#include <iostream>
#include <string>

using namespace std;

#ifndef STUDENT_H
#define STUDENT_H

class Student{
public:
	//Default constructor.
	Student();

	//Overload constructor.
	Student(string*, int*);

	//Destructor
	~Student();

	int* avgGrade();

	void gradeDistribution();

	//Accessor function.
	string getName();
	int getGrade();

	

private:
	//Member variables.
	string newName;
	int newGrade;

};
#endif 

Oh, ok. I just quickly glanced at your code and didn't see any classes and assumed you hadn't made one, but you certainly did. Does it give you the correct output?
It kind of works. I'm just trying to get the first part to work.

1
2
3
4
5
6
7
8
9
10
11
string studentName;
	int studentGrade = 0;
	Student Student_1(&studentName, &studentGrade);

	for (int i = 0; i < *numOfStudents; i++){
		cout << "Name " << ": ";
		cin >> studentName;
		//cout << string(100, '\n');
		cout << "Grade " << ":";
		cin >> studentGrade;
	}


As I mentioned earlier, Name: and Grade should be printed next to each other.
is there a different way to get student Name and grades from the user and store them into array of object by calling the class member function?

As I already said, it isn't possible to have "Grade: " appear on the same line after the user types their name. 99% of consoles do not send input to your program until the user presses enter, and the user pressing enter goes to the next line. Remember, the user needs to be able to backspace.
Topic archived. No new replies allowed.