Need help with simple code for school

Hi all,

I've been having trouble making this code work exactly as I want it too for school. The code should display:

Input the first grade.
97
Input the second grade.
98.3
Input the third grade.
95
Please input the student name:
John Smith
Please input the student email:
john.smith@mail.com

Student name: John Smith
Student email: john.smith@mail.com
The average of the three grades is 96.7667

-The code is as follows below:

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	//These lines contain the variables to sum up the grades and contain the grade values.
	double average;
	double grade1, grade2, grade3;
	string name;
	string email;
	
	//User inputs the three test scores
	cout << "Input the first grade\n";
	cin >> grade1;
	
	cout << "Input the second grade\n";
	cin >> grade2;
	
	cout << "Input the second grade\n";
	cin >> grade3;

	cout << "Please Input the student name: \n";
	getline(cin, name);
	
	cout << "Please input the student email: \n";
	getline(cin, email);
	
	//This sums the three grades and averages them out.
	average = (grade1 + grade2 + grade3)/3;
	
	//The end result is displayed to the user.
	cout << "Student name: " << name << endl;
	cout << "Student email address: " << email << endl;
	cout << "The average of the three grades is " << average << "." << endl;
	return 0;
}


however, it displays incorrectly like this:

Input the first grade.
97
Input the second grade.
98.3
Input the third grade.
95
Please input the student name:
Please input the student email:
Student name:
Student email: john.smith@mail.com
The average of the three grades is 96.7667

-The code makes it skip the name part. I've also used "cin.ignore()" after the name input but it just makes the name move to the email line. I've tried using "cin >> name"but it doesn't display the last name correctly if there is a space. I've been working on this for over 3 hours thinking it's a typing error on my part but I think there might be conflicting parts in my code. I'm just starting on my 3rd week of school so any help is appreciated.
Last edited on
Mixing cin and getline will always cause problems. I suck at streams co cannot give you an explanation without looking it up. Just use getline for all the inputs and you should be OK.
cin >> std::flush before a getline should do the same thing.
IIRC, cin does not clear the buffer, that is why the name was offset when you use ignore.

Sorry for the horrible explanation.
Umm... not quite.

std::flush is only applicable to std::ostream and derivatives, and flushes the output buffer, making sure that everything is printed to the output medium before your program proceeds.

Still, the idea is correct. Using the stream operator will read your number, but leave the newline that you presumably terminated the number with in the buffer, as well as any other characters you may have typed that it couldn't include in the number. What you can do is toss them away with std::cin.ignore, like so:
1
2
3
4
5
6
7
8
9
10
#include <limits>

// ...

std::cin >> grade3;

// ignore everything up to and including the first newline
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

std::getline(std::cin, email); // works as intended 
I'd do that the following way
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

#include<iostream>
#include<string>
using namespace std;

string read(string a){
string b, c;
cin>>b>>c;
string fi = b + " " + c;
return fi;
}

int main(){
float g1, g2, g3;
cout<<"Input first grade\n";
cin>>g1;
cout<<"Input second grade\n";
cin>>g2;
cout<<"Input third grade\n";
cin>>g3;
float avg = (g1+g2+g3)/3;
cout<<"Input email\n";
string email;

if (cin>>email){
cout<<"Input name\n";
string name;
cout<<"Name: "<<read(name)<<"\n";
cout<<"Email: "<<email<<"\n";
cout<<"Grade average: "<<avg<<"\n";

}

}

If there's anything in the code you don't understand, just ask.
Thanks everyone for the replies! This helps a lot! Still really new at C++ so the only thing I don't understand is what the lines "string read(string a)" through "string fi;" do. I'll definitely learn more about "if" statements and see how I can use them in my assignments.

Also I figured out how to make it not skip the "name" and "email" line by putting "cin.ignore()" before the "getline" inputs. Thanks to admkrk for the tip about "getlines" cause I was using them at the end of the code.

Also thanks jgg2002 for showing me an alternative way to do it!

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	//These lines contain the variables to sum up the grades and contain the grade values.
	double average;
	double grade1, grade2, grade3;
	string name;
	string email;
	
	//User inputs the three test scores
	cout << "Please input first grade: " << endl;
	cin >> grade1;
	
	cout << "Please input second grade: " << endl;
	cin >> grade2;
	
	cout << "Please input third grade: " << endl;
	cin >> grade3;
	
	cin.ignore(); //The change that stops the line skipping.
	
	cout << "Please input student name: " << endl;
	getline(cin, name);
	
	cout << "Please input student email: " << endl;
	getline(cin, email);

	//This sums the three grades and averages them out.
	average = (grade1 + grade2 + grade3)/3;
	
	//The end result is displayed to the user.
	cout << "\n";
	cout << "Student name: " << name << endl;
	cout << "Student email address: " << email << endl;
	cout << "The average of the three grades is " << average << endl;
	return 0;
	
}
@TwilightSpectre Thanks for clearing that up. I really should be able to remember how streams work better.
@jgg2002

In your read function, string a is unused. If you pass a std::string or any other class type to a function, it should be by reference.

I prefer the OP's code as it is at the moment, it's easier to understand. Although a bigger program would make more use of functions.

Avoid using float, unless there is a library that requires it, or you have billions of them. The precision of float is easily exceeded, and it's not any quicker for the compiler to retrieve them - at least on a 64 bit system which seems to be the norm these days. So stick to double

I would also avoid over abbreviating your variable names - good variable and function names aid in understanding and readability. While I am at it, you could do with some white space around your operators: running everything together also decreases readability.

Sorry for being so critical, hopefully it's all for the better :+)
I know all about the variable names. But, I didn't really need to name them grade1, grade2, grade3... g1 works just as well. Also I'm quite used to writing small variables to avoid mistakes, such as in the following example.

 
table[position[check]].push_back(input);

which requires more typing
@jgg2002

I know all about the variable names. But, I didn't really need to name them grade1, grade2, grade3... g1 works just as well.


I still disagree. The trouble with short variable names everywhere, is that the code becomes cryptic. Plenty of times before, people have looked at their own code from a few months prior say, and haven't understood it.

Also, you may not be the only one looking at the code: In industry you would probably get a talking to, if you wrote code like that at work. And this is one of the reasons why Coding Style Guides exist: they try to sort out problems that come from bad style, by promoting good style to start with.

IMO, the perception that one needs to abbreviate everything becomes a disease. Maybe it started with C programming, wasn't helped at all with Hungarian Notation, then was sorted out more and more with larger projects. People realised that they had to have better style to avoid problems. With C, it might not have been such a problem, one is supposed to have have short functions everywhere (but this doesn't always happen). C++ is supposed to have short functions too, but there is often hell to pay if member variables, functions and objects have short meaningless names.

which requires more typing


I wouldn't worry about abbreviating to avoid errors, the compiler will find most of the errors hopefully. And I think one is less likely to mess up with a meaningful name.

Good variable and function names aid understanding and can help reduce problems. If one does it well, the code should read like a story. Here is a somewhat comical example of what I mean:

http://www.cplusplus.com/forum/lounge/176684/#msg872881


I am not saying short variable names are all bad, here is the guideline written by Bjarne Stroustrup and Herb Sutter, Section E7 is the one you want:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.html


There is heaps of really good stuff in that document, it's worthwhile reading the whole thing.

IMO, an exception to short names is when the documentation (from wiki say) has mathematical equations. It's sensible to stick with the conventions in the documentation. I would comment a link to it, and comment pasted individual equations as well. Most people who know any math would understand y = r * sin(theta); But some might argue for :

double YOrdinate = Radius * sin(ThetaAsRadian);

These could be named after what they actually are:

double RoofHeight = RoofSlopeDistance * sin(RoofAngleAsRadian);

This is much better than having ra, a classic mistake is to call sin(ra) when ra is in degrees, not radians.

Sorry if all this comes across as a lecture, but it could be quite useful to lots of other people too.

Regards :+)
I agree with what you have said. In codes that I know I'll need to see again I usually use understandable names. But I'm a little used to short and faster to type variable names because I'm a competitive programmer, and in competitions it is essential I save as much time as possible when typing my code.
@Kurayami20XX

When I wrote that code I thought you knew about functions, but it seems you don't. Once you learn it's pretty simple to understand.

The code you didn't understand, just declares a function that when called, reads two strings and then returns a new string which equals to string1+" "+string2.

 
string3 = string1+" "+string2

since in C++ you can add strings, so that you create a new string with those combined, string 3 would be equal to the string1 combined with a space between combined with string2.
Topic archived. No new replies allowed.