Input Problem

When someone goes to put in their name I get an error, for example if they just put in "John" and then hit enter everything acts fine, but if they put a first and last name for example "John Doe" the outputs gets really nasty.

What do I need to change in 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
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;
class student
{
private :
	int sno;
	char name[20];
	int g1,g2,g3,g4;
	int absen;
public :
	void Getdata()
	{
		cout << "Enter Student ID:  "; cin >> sno;
		cout << "Enter Student NAME:  "; cin >> name;
		cout << "Enter Student TEST SCORES (3):  "; cin >> g1 >> g2 >> g3;
		cout << "Enter # of Absences:  "; cin >> absen;
	}
	void Putdata()
	{
		cout  << "Student ID:  " << sno << endl;
		cout << "Student NAME:  " << name << endl;
		cout << "# of Absences:  " << absen << endl;
		if (absen == 0) {
  g4 = 2;
} else {
  g4 = 0;
}

cout << "Grade:  " << (g1+g2+g3 + g4)/3 << endl;
		if ((((g1+g2+g3 + g4)/3) >= 73))
		cout << "Grade:  " << (g1+g2+g3)/3 << endl;
		if ((((g1+g2+g3)/3) >= 73))
		{cout << "Successful" << endl;}
		else
		{cout << "Unsuccessful" << endl;}
	};
};


int main()
{
	student s;
	s.Getdata();
	s.Putdata();
	getch();
	return 0;
}
It might be because you are using cin which I believe only reads up until a space. So I would change that by using getline.
getline(cin,Variable name);

I think this is the right reference
http://www.cplusplus.com/reference/string/getline/

You should be good if you change that. Let me know.
I changed it to this but it wouldnt compile
1
2
		cout << "Enter Student Name: ";
  getline (cin,name[20]);


it says
error C3861: 'getline': identifier not found
Sorry,
Try this. Let me know how it works.
cout << "Enter Student NAME: "; cin.getline(name,'\n');
Wait don't do that ...sorryy
You may be having an issue because variable name is a char type. Try using string instead (make sure to include #include <string>). Also use a getline statement to grab the first and last name because as jlillie said cin only reads until a whitespace.
Sorry, about that dude, I had a minor stroke or something. Try this I changed a few things but I put comments.
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 <string>  //String
#include <stdio.h>
#include <conio.h>

using namespace std;
class student
{
private :
	int sno;
	string name;  //String
	int g1,g2,g3,g4;
	int absen;
public :
	void Getdata()
	{
		cout << "Enter Student ID:  " << endl; 
		cin >> sno;
		cout << "Enter Student NAME:  " << endl;
		cin.get(); // Gets the space so it doesn't skip the next line 
		getline(cin, name); // gets the entire line
		cout << "Enter Student TEST SCORES (3):  " << endl;
		cin >> g1 >> g2 >> g3;
		cout << "Enter # of Absences:  " << endl;
		cin >> absen;
	}
	void Putdata()
	{
		cout  << "Student ID:  " << sno << endl;
		cout << "Student NAME:  " << name << endl;
		cout << "# of Absences:  " << absen << endl;
		if (absen == 0) {
  g4 = 2;
} else {
  g4 = 0;
}

cout << "Grade:  " << (g1+g2+g3 + g4)/3 << endl;
		if ((((g1+g2+g3 + g4)/3) >= 73))
		cout << "Grade:  " << (g1+g2+g3)/3 << endl;
		if ((((g1+g2+g3)/3) >= 73))
		{cout << "Successful" << endl;}
		else
		{cout << "Unsuccessful" << endl;}
	};
};


int main()
{
	student s;
	s.Getdata();
	s.Putdata();
	getch();
	return 0;
}
I got your guys suggestions to eventually compile but soon as it comes to entering in the Students name it just completely skips over it and doesn't allow me to enter anything.
Look at the code I posted above it should work cin leaves a extra space or something when it is done so you use cin.get() before. It is in the code above I posted.
Oh wow I entered that comment and didn't even see your post. Thanks Will try it in a sec.
It worked! Thanks alot! I am cramming hard for this C++ Exam tomorrow and I need a few more things. Could you help me out more? If so, do you have a better way I can contact you, do you have access to mIRC?
I don't know what mIRC is. I have an exam tomorrow myself and it is 11:03 here so I need to sleep soon. I'm not promising anything but I'm curious what else do you have left?
well for that same program I am working on this topic, I need to make it so it only lets people enter test scores from 1-100.
Last edited on
Use an if statement.

Like
1
2
3
4
if(x < 1 || x > 100)
{
   then whatever you want to have happen
}
yeah but i have the person entering 3 test scores on the same line idk how i would write it, ive been looking on google for something similar but i cant find anything >_>
I'm not sure. Sorry dude. Good luck.
Topic archived. No new replies allowed.