Command window displaying all choices at once

closed account (oN3k92yv)
Hey, I'm a beginner and I need help with my code. Whenever I run the code, it asks me for my student ID like it's supposed to, and once I press enter all the other things that need user input show up at once. Like all the input your grades pop up in the command window.

This is how it looked like before I added the "endl;" at the end of my lines.

http://i.imgur.com/33785Zd.png

thank you.

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
  /*This program accepts a student ID, first name, last name, and student’s score for their classes
to find out their average score in the class. */


#include <iostream>

using namespace std;

int main()
{
	int id;
	char name;
	char lastName;
	double grade1, grade2, grade3;
	double average;

    cout << "Student Grading System (SGS)" << endl;
	cout << "Long Beach City College" << endl;
	cout << "==============================" << endl;

	cout << "Please enter your student id number: " << endl;
	cin >> id;
	cout << "Please enter your name: " << endl;
	cin >> name;
	cout << "Please enter your last name: " << endl;
	cin >> lastName; 
	cout << "=============================" << endl;

	cout << "Please input your CS11 grade: " << endl;
	cin >> grade1;
	cout << "Please input your CS12 grade: " << endl;
	cin >> grade2;
	cout << "Please input your CS21 grade: " << endl;
	cin >> grade3;
	cout << "The average of the three grades is: " << (grade1 + grade2 + grade3) / 300.0*100.0 << "%\n";

	
	return 0;
char name;
char holds a single character. It cannot possibly hold more than that. So if you enter more than one character for name, excess ones will be read as lastName (a single char too) and then will be passed to double grade which leads to all sort of problems.

#include <string> and use std::string instead of char variables.
char name;
char lastName;
It accept only single character.
char name[20];
char lastName[20];
if you want to enter more than one character ,you need to enter the number of character here char can accept less than 20 character.
closed account (oN3k92yv)
thank you guys, I got it.
Topic archived. No new replies allowed.