Program closing...

hello,i'm new to programming and I have the code:blocks ide with mingw compiler. It worked fine by now but I just started making a little bit more serious code with loops and so and something went wrong. My program is compiled just fine but when I type something it closes. I get to the point of typing "YES" and after that whatever i type the program closes itself.

Here's 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <string>
int main()
{
	using namespace std;
	/* Determinate Percentage taken into account for each grade
	for an english,math and science student
	*/
	const float ENGLISH_MIDTERM_PERCAENTAGE = 0.25;
	const float ENGLISH_FINALEXAM_PERCENTAGE = 0.25;
	const float ENGLISH_RESEARCH_PERCENTAGE = 0.30;
	const float ENGLISH_PRESENTATION_PERCENTAGE = 0.20;
	const float MATH_MIDTERM_PERCENTAGE = 0.50;
	const float MATH_FINALEXAM_PERCENTAGE = 0.50;
	const float SCIENCE_MIDTERM_PERCENTAGE = 0.40;
	const float SCIENCE_FINALEXAM_PERCENTAGE = 0.40;
	const float SCIENCE_RESEARCH_PERCENTAGE = 0.20;
	int midterm ;
	int finalExamGrade ;
	int research ;
	int presentation ;

	float finalNumericGrade = 0;
	char finalLetterGrade;
	char response[256];
	string moreGradesToCalculate;
	cout<<"Do you want to calculate a grade? ";
	cin>>moreGradesToCalculate;
	for(int i=0;i<moreGradesToCalculate.length();i++)
	{
	    moreGradesToCalculate[i]= toupper (moreGradesToCalculate[i]);
	}
	while (moreGradesToCalculate=="YES")
	{

	cout<<"Enter student type: English, Math or Science: "
	<<"English =1 , Math = 2, Sciece = 3"<<endl;
	cin.getline (response,256);
	if (strlen (response)==0){
	    cout<<"You must enter a student type";
	    return 1;
	}
	if ((atoi (response)<1) | (atoi (response) >3))
	{
	    cout<< response<< " - is not a valid student type";
	    return 1;
	}
	switch (atoi (response))
	{
	    //Case 1  - English Student
	    case 1:
	    cout<<"Enter the midterm grade: ";
	    cin.getline (response,256);
	    midterm= atoi(response);
	    cout<<"Enter the Final Examination grade: ";
	    cin.getline (response,256);
	    finalExamGrade = atoi (response);
	    cout<<"Enter the Research grade: ";
	    cin.getline (response,256);
	    research = atoi (response);
	    cout<<"Enter the presentation grade: ";
	    cin.getline (response,256);
	    presentation = atoi (response);


	    finalNumericGrade = midterm * ENGLISH_MIDTERM_PERCAENTAGE + finalExamGrade* ENGLISH_FINALEXAM_PERCENTAGE +
	    research* ENGLISH_RESEARCH_PERCENTAGE + presentation * ENGLISH_PRESENTATION_PERCENTAGE;
	    if (finalNumericGrade >= 93)
	    finalLetterGrade = 'A' ;
	    else
	    if ((finalNumericGrade >=85) & (finalNumericGrade < 93))
	    finalLetterGrade = 'B';
        else
        if
        (( finalNumericGrade >= 78 ) & (finalNumericGrade < 85))
        finalLetterGrade = 'C';
        else
        if (( finalNumericGrade >= 70) & ( finalNumericGrade < 78))
        finalLetterGrade = 'D';
        else
        if ( finalNumericGrade <70)
        finalLetterGrade = 'F';
        cout<<" *** ENGLISH STUDENT *** " <<endl<<endl;
        cout<<"Midterm grade is: "<<midterm<<endl;
        cout<<"Final Examination grade is : " <<finalExamGrade<<endl;
        cout<<"Research Grade is : " <<research<<endl;
        cout<<"Presentation grade is : "<<presentation<<endl;
        cout<<"Final Numeric Grade is: "<<finalNumericGrade<<endl;
        cout<<"Final Letter Grade is : "<<finalLetterGrade <<endl;
        break;
        //Case 2 is Math Student
        case 2:
        cout<<"Enter midterm grade : ";
        cin.getline (response,256);
        midterm = atoi (response);
        cout<<"Enter final examination grade : ";
        cin.getline (response,256);
        finalExamGrade = atoi (response);
        finalNumericGrade = (midterm * MATH_MIDTERM_PERCENTAGE)+( finalExamGrade * MATH_FINALEXAM_PERCENTAGE);
        if (finalNumericGrade >= 93)
	    finalLetterGrade = 'A' ;
	    else
	    if ((finalNumericGrade >=85) & (finalNumericGrade < 93))
	    finalLetterGrade = 'B';
        else
        if
        (( finalNumericGrade >= 78 ) & (finalNumericGrade < 85))
        finalLetterGrade = 'C';
        else
        if (( finalNumericGrade >= 70) & ( finalNumericGrade < 78))
        finalLetterGrade = 'D';
        else
        if ( finalNumericGrade <70)
        finalLetterGrade = 'F';
        cout<<" *** Mathematics Student *** "<<endl<<endl;
        cout<<"Midterm Grade : "<<midterm<<endl;
        cout<<"Final Examination Grade : "<<finalExamGrade<<endl;
        cout<<"Final Numeric Grade : "<<finalNumericGrade<<endl;
        cout<<"Final Letter Grade : " <<finalLetterGrade<<endl;
        break;
        //Case 3 Is Science Student
        case 3:
        cout<<"Enter Midterm Grade : ";
        cin.getline (response,256);
        midterm = atoi (response);
        cout<<"Enter Final Examination Grade : ";
        cin.getline (response,256);
        finalExamGrade = atoi (response);
        cout<<"Enter Research Grade : ";
        cin.getline (response,256);
        research = atoi(response);
        finalNumericGrade = midterm * SCIENCE_MIDTERM_PERCENTAGE + finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE
        + research* SCIENCE_RESEARCH_PERCENTAGE;
        if (finalNumericGrade >= 93)
	    finalLetterGrade = 'A' ;
	    else
	    if ((finalNumericGrade >=85) & (finalNumericGrade < 93))
	    finalLetterGrade = 'B';
        else
        if
        (( finalNumericGrade >= 78 ) & (finalNumericGrade < 85))
        finalLetterGrade = 'C';
        else
        if (( finalNumericGrade >= 70) & ( finalNumericGrade < 78))
        finalLetterGrade = 'D';
        else
        if ( finalNumericGrade <70)
        finalLetterGrade = 'F';
        cout<<" *** Science Student *** "<<endl<<endl;
        cout<<" Midterm Grade : " <<midterm<<endl;
        cout<<"Final Examamination Grade : "<<finalExamGrade<<endl;
        cout<<"Research Grade : "<<research<<endl;
        cout<<"Final Numeric Grade : "<<finalNumericGrade<<endl;
        cout<<"Final Letter Grade : "<<finalLetterGrade<<endl;
        default:
        cout <<response<<" is not a valid student type...";
        return 1;
	}
	cout<<endl<<endl<<"Do you have another grade to calculate? ";
	cin>>moreGradesToCalculate;
	for (int i = 0;i<moreGradesToCalculate.length();i++)
	{moreGradesToCalculate[i]=toupper (moreGradesToCalculate[i]);
	}
	}

	return 0;


}

I'm pretty sure that the problem lies somewhere here
1
2
3
4
5
6
7
8
9
10
while (moreGradesToCalculate=="YES")
	{

	cout<<"Enter student type: English, Math or Science: "
	<<"English =1 , Math = 2, Sciece = 3"<<endl;
	cin.getline (response,256);
	if (strlen (response)==0){
	    cout<<"You must enter a student type";
	    return 1;
	}


cus as I execute the program it just skips the first cin and it looks like the user pressed enter without typing anything. Maybe it's a compiler prblm. :(
You are mixing formatted and unformatted input.
See here http://www.cplusplus.com/forum/articles/6046/

Remember, get user input using getline(), then parse it. Don't mix cin >> x; with getline( cin, s );.

Hope this helps.
i'm a real newbie so I hope my question won't annoy you :(.
but Duoas what do u mean by formatted and unformatted?
do you mean that instead of
1
2
cin.getline (response[256])
cin>>response

i should use
getline(cin, input) ;
so my code will be something like
getline(cin, response) ;
?
Last edited on
Yes. Using cin >> response; leaves everything that doesn't go into 'response' in the input buffer, including the newline left there when the user pressed Enter.

Using getline( cin, response ); reads everything the user typed, up-to-and-including the newline (or Enter key), and stores all the characters except the newline in the string. You can then figure out what to do with the string.

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

int getint( istream& ins, int result = 0 )
  {
  string s;
  getline( ins, s );
  istringstream ss( s );
  ss >> result;
  return result;
  }

int main()
  {
  int age;
  string name;

  cout << "How old are you? " << flush;
  age = getint( cin );

  cout << "What is your name? " << flush;
  getline( cin, name );

  cout << name << " is " << age << " years old.\n";
  return 0;
  }

Hope this helps.
Duoas thanks a lot. although I don't 100% get what you meant in your code I managed to get mine working.
here's what I did. I changed the way the program handled the user input for the string "moreGradesToCalculate". From what I got getline (cin,variable); is meant to handle only strings cus I tried changing cin.getline(response,256) into getline(cin,response) and response is a char and I got an error. But again I might have made a mistake as I'm not very familiar with the syntax.
So Duoas you basically suggest me to use getline(cin,string) for strings in all cases right?

Thanks a lot for your help.
Topic archived. No new replies allowed.