Simple c++ program (new to c++ need help with cin/cout multiple variables in DOS program

Will show current code at bottom of thread, first of all huge thanks to anyone who helps out and even for reading thread. What happens when I run this is it lets me fill out first variable, but it runs all of the quizes without allowing the user to input a value and im really new to c++ and im sure im over looking something but I cant seem to find where im wrong. Point of program is to b simple DOS window to persons first and last name and the average of 5 quizzes a midterm and a final exam. Thanks again




#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
short quiz1, quiz2, quiz3, quiz4, quiz5, quiztotal, midterm, final, avg;
int studentname;

//get student name
cout << "What is the students First and Last name? \n";
cin >> studentname;

//get quiz1
cout << "What is the first quiz grade? \n";
cin >> quiz1;

//get quiz2
cout << "What is the second quiz grade? \n";
cin >> quiz2;

//get quiz3
cout << "What is the third quiz grade? \n";
cin >> quiz3;

//get quiz4
cout << "What is the fourth quiz grade? \n";
cin >> quiz4;

//get quiz5
cout << "What is the fifth quiz grade? \n";
cin >> quiz5;

//get midterm
cout << "What is the midterm grade? \n";
cin >> midterm;

//get final
cout << "What is the final exam grade? \n";
cin >> final;

//get quiz total
quiztotal = quiz1 + quiz2 + quiz3 + quiz4 + quiz5;

//get overall average
avg = midterm + final + quiztotal / 7;

system("PAUSE");
return EXIT_SUCCESS;
}
1
2
3
4
5
int studentname;

//get student name
cout << "What is the students First and Last name? \n";
cin >> studentname;


You can't stuff a string into a number.
Sounds stupid but can you or someone explain that a bit more in depth.. What should I use instead of int then? string?
Yes. If you're dealing with a string, you should... use a string.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    short quiz1, quiz2, quiz3, quiz4, quiz5, quiztotal, midterm, final, avg;
    string studentname ;

    //get student name
    cout << "What is the students First and Last name? \n";
    getline(cin, studentname) ;
    // ... 


Because you're asking for both the first and last name, you need to use getline. Using cin >> studentname would result in the extraction stopping at the first whitespace (and therefore only extracting the first name from the input stream.)
Last edited on
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 <cstdlib>
#include <iostream>
#include <string>  // to use string and getline.

using namespace std;

int main()
{
	// changed to float unless you really want to divide integers... ;)
	float quiz1, quiz2, quiz3, quiz4, quiz5, quiztotal, midterm, final, avg;
	string studentname;

	//get student name
	cout << "What is the students First and Last name? \n";
	getline(cin, studentname);

	//get quiz1
	cout << "What is the first quiz grade? \n";
	cin >> quiz1;

	//get quiz2
	cout << "What is the second quiz grade? \n";
	cin >> quiz2;

	//get quiz3
	cout << "What is the third quiz grade? \n";
	cin >> quiz3;

	//get quiz4
	cout << "What is the fourth quiz grade? \n";
	cin >> quiz4;

	//get quiz5
	cout << "What is the fifth quiz grade? \n";
	cin >> quiz5;

	//get midterm
	cout << "What is the midterm grade? \n";
	cin >> midterm;

	//get final
	cout << "What is the final exam grade? \n";
	cin >> final;

	//get quiz total
	quiztotal = quiz1 + quiz2 + quiz3 + quiz4 + quiz5;

	//get overall average
	avg = midterm + final + quiztotal / 7;
	cout << "\n" << studentname << "'s average is: " << avg << endl;

	// Dont use system(exit) if you can help it, bad habit.
	cout << "\npress Enter to exit" << endl;
	cin.ignore(99, '\n');
	return 0;
}


Did a few changes for you, read notes on source.

Also if you did want to convert string to int or int to string. use stringstream in sstream header. With that you can load the int into the stream and spit it back out into a string. However, wont work if you are trying to put letters in an int.
Topic archived. No new replies allowed.