LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2005, 08:14 PM   #1
eclipse75
LQ Newbie
 
Registered: Mar 2005
Location: Western Hemisphere
Distribution: Slackware 10.1
Posts: 11

Rep: Reputation: 0
Question C++ - Wondering why my program wont loop?


When I compiled and ran the program it should at the end ask if they want to calculate another grade, but instead it automatically ends. Also I had it endl after anytime a incorrect input was recieved, yet it just ignores that and bunches the line like so:

You must select a Student Typeanon@SLACKWARE:~/cpp files/Grades$ WORK YOU FREAKING THING!
-bash: WORK: command not found
anon@SLACKWARE:~/cpp files/Grades$

//Created By Eclipse75
//Grades.cpp
#include <iostream>
#include <string>

int main()
{
using namespace std;

const float ENGLISH_MIDTERM_PERCENTAGE = .25;
const float ENGLISH_FINALEXAM_PERCENTAGE = .25;
const float ENGLISH_RESEARCH_PERCENTAGE = .30;
const float ENGLISH_PRESENTATION_PERCENTAGE = .20;
const float MATH_MIDTERM_PERCENTAGE = .5F;
const float MATH_FINALEXAM_PERCENTAGE = .50;
const float SCIENCE_MIDTERM_PERCENTAGE = .40;
const float SCIENCE_FINALEXAM_PERCENTAGE = .40;
const float SCIENCE_RESEARCH_PERCENTAGE = .20;
int midterm = 0;
int finalExamGrade = 0;
int research = 0;
int presentation = 0;
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") {

// What type of student are we calculating?
cout << "Enter student type " <<
"(1=English, 2=Math, 3=Science): ";

cin.getline(response,256);

if (strlen(response) == 0) {
cout << "You must enter a student type" << endl;
return 1;
}

if ((atoi(response) < 1) | (atoi(response) > 3)) {
cout << response <<
" - is not a valid student type" << endl;
return 1;
}

// Student type is valid, now let's calculate the grade
switch(atoi(response))
{
// Case 1 is an English Student
case 1:
cout << endl << endl << "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_PERCENTAGE) +
(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 << endl << "***ENGLISH STUDENT***"
<< endl << endl;
cout << "Midterm grade is: " <<
midterm << endl;
cout << "Final Exam 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 << endl;
break;

// Case 2 is a Math Student
case 2:
cout << "Enter the Midterm Grade: " << endl;
cin.getline(response,256);
midterm = atoi(response);
cout << "Enter the Final Examination Grade: " << endl;
cin.getline(response,256);
finalExamGrade = atoi(response);
finalNumericGrade = (MATH_MIDTERM_PERCENTAGE * midterm) +
(MATH_FINALEXAM_PERCENTAGE * finalExamGrade);
if (finalNumericGrade > 90)
finalLetterGrade = 'A';
else
if ((finalNumericGrade >= 83) &
(finalNumericGrade < 90))
finalLetterGrade = 'B';
else
if ((finalNumericGrade >= 76) &
(finalNumericGrade < 83))
finalLetterGrade = 'C';
else
if ((finalNumericGrade >= 65) &
(finalNumericGrade > 76))
finalLetterGrade = 'D';
else
if (finalNumericGrade < 65)
finalLetterGrade = 'F';

cout << endl << "***MATH STUDENT***" << endl;
cout << "Midterm grade is: " <<
midterm << endl;
cout << "Final Exam grade is: " <<
finalExamGrade << endl;
cout << "Final Numeric grade is: " <<
finalNumericGrade << endl;
cout << "Final Letter grade is: " <<
finalLetterGrade;
break;

// Case 3 is a Science Student
case 3:
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);
finalNumericGrade = (midterm * SCIENCE_MIDTERM_PERCENTAGE) +
(finalExamGrade * SCIENCE_FINALEXAM_PERCENTAGE) +
(research * SCIENCE_RESEARCH_PERCENTAGE);
if (finalNumericGrade >= 90)
finalLetterGrade = 'A';
else
if ((finalNumericGrade >=80) &
(finalNumericGrade < 90))
finalLetterGrade = 'B';
else
if ((finalNumericGrade >= 70) &
(finalNumericGrade < 80))
finalLetterGrade = 'C';
else
if ((finalNumericGrade >= 60) &
(finalNumericGrade < 70))
finalLetterGrade = 'D';
else
if (finalNumericGrade < 60)
finalLetterGrade = 'F';
cout << endl << "***SCIENCE STUDENT***" << endl;
cout << "Midterm grade is: "
<< midterm << endl;
cout << "Final Exam grade is: "
<< finalExamGrade << endl;
cout << "Research grade is: "
<< research << endl;
cout << "Final Numeric grade is: "
<< finalNumericGrade << endl;
cout << "Final Letter grade is: "
<< finalLetterGrade << endl;
break;
default:
cout << response <<
" - is not a valid student type";
return 1;
} // end of switch

cout << endl <<
"Do you have another grade to calculate? ";
cin >> moreGradesToCalculate;
for (int i = 0;
i < moreGradesToCalculate.length(); i++) {
moreGradesToCalculate[i] =
toupper (moreGradesToCalculate[i]);
} // end of for
} // end of while
cout << endl <<
"Thanks for using the Grades Calculation program!" << endl;

return 0;
} // end of main



I appreciate all those who have helped me. All others, I'm disappointed.
 
Old 03-27-2005, 08:56 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I normally don't help with homework stuff, but the problem you have is something that drove me nuts many years ago. You're dealing with a problem where some user input is left in the input buffer, and it gets mis-interpreted. Long story short, I would suggest trying to figure out a way to use cin.getline() for reading in the yes/no response from the user.

Also, in your if-statements. You're not using the right AND and OR operators. What you have in the code you posted are binary operators, not logical operators. Trust me, you want to use && and || instead. No really, trust me. What you have might work for this program, but it is a very bad habit you should avoid.

Edit:
Also, to help others help you, always, always, always use code tags around code you post.

Last edited by Dark_Helmet; 03-27-2005 at 08:59 PM.
 
Old 03-27-2005, 09:16 PM   #3
eclipse75
LQ Newbie
 
Registered: Mar 2005
Location: Western Hemisphere
Distribution: Slackware 10.1
Posts: 11

Original Poster
Rep: Reputation: 0
Smile

Okay, thanks for the reply. Tomorrow I'll work on that and see if it works. Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple program wont compile BACTRATE Linux - Software 14 07-24-2008 12:57 PM
having trouble with the do while loop in program mshinska Programming 2 10-08-2005 10:18 PM
Wondering if I should use JAVA or UNIX scripts to develop a program pedrosan Programming 4 05-22-2004 07:35 AM
Program on boot-up won't stop running in a continuous loop... CanadianSlacker Slackware 6 01-14-2004 01:06 AM
something I'm wondering about 'while' loop ... purpleburple Programming 3 03-12-2003 06:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration