Do while Loops not working - c++

I've been recycling an assignment to further practice and develop my programming skills in my class and I'm having an issue with 3 DO WHILE loops within another Do While loop.
I'm trying to deny Test scores for test1, 2 and 3 that are less than 1 and greater than 100.
I'm encountering that the loops are not processing what I am inputting for Test1/2/3. It's allowing values out of the while range to pass through. Is there anyone who can suggest or see what I might be doing wrong? Thanks ahead of time guys!
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double computeavg (int a, int b, int c);
char lettergrade (double z);
int main ()
{
double test1, test2, test3, average; //test1/2/3, test scores, Average: average of test scores
double tottest1=0, tottest2=0, tottest3=0, avg1, avg2, avg3; //tottest# - sum of test grades collected for that number test, avg# average for first second or third test
int student=0, avgvar; //average variable, Student number
char grade, ans; // Holds a letter grade, holds a response to a question
do{
student=student+1;
cout<<"Hello Student #"<<student<<endl;
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1>=1||test1<=100);
do{
cout<<"Please input test 2 Grade ";
cin>> test2;
}
while(test2>=1||test2<=100);
do{
cout<<"Please input test 3 Grade ";
cin>> test3;
}
while(test3>=1||test3<=100);
average=computeavg (test1, test2, test3);
cout<<setprecision(0)<<fixed;
cout<<"Your Average is: "<<average<<endl;
tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;
grade = lettergrade(average);
cout << "Your grade is " << grade << endl;
cout<<"Do you want to grade another student? (y/n)";
cin>>ans;
cout<<"\n";
} while(ans=='y');

I think you need to change do while as below:
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1<1||test1>100);

Your conditions are backward. Look carefully: while(test3>=1||test3<=100). All numbers satisfy at least a portion of the condition. For example, −4 is not greater than or equal to 1, but it is less than or equal to 100, and since your conditions are joined with the || operator, which is the or operator, the condition evaluates to true.
You need to change the condition. This is homework, so I won't tell you precisely what condition you should use instead.
You can use a debugger to discover this problem yourself. You can also discover it by carefully walking through your program by hand. Get a pencil and some paper, and write down the current values of your variables. Keep track of which instruction you're on, and then work your way through the program. When a variable changes, cross out its old value and write down the new one. Eventually, you'll be able to do that in your head, but while you're learning, it helps to just write it down.

Related

How to debug my C++ program that calculates CGPA?

I have been trying to write a simple code in C++ to calculate CGPA. It is a practice code. There is no error in code when I start to build. But it is not running.
I am using codeblocks. I have checked everything. but can not find any problems in it.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int g,h,i,a=0, grade[g],hour[h];
char course[10];
float z=0,sum =0, result=0, totalhour=0;
cout<<"How Many Course's do you want to calculate for CGPA: ";
cin>> a;
cout<<endl;
cout<<"Please Enter your Course name, Credit hour & Grade point of the your course"<<endl;
for(i=1;i<=a;i++)
{
cout<<i<<". no Course name: ";
cin>>course[i];
cout<<"-----";
cout<<"Credit Hour: ";
cin>>hour[h];
cout<<"-----";
cout<<"Grade point: ";
cin>>grade[g];
cout<<"\n";
}
for (i=1; i<=a;i++)
{
cout<<i<<".no Course----";
z= grade[g]*hour[h];
cout<<"Grade point X Credit Hour = "<<grade[g]<<" X "<<hour[h]<<" = "<<z<<endl;
sum = sum+z;
totalhour= totalhour+hour[h];
}
result = sum / totalhour;
cout<<"Your total Credit hour Completed is : "<<totalhour<<endl;
cout<<"----Your Total CGPA is -------- = "<<result<<endl;
getch();
return 0;
}
You have some problems in your code:
What happens if user input is larger then 10? you assume it will be smaller (you will get undefined behavior since the array size is 10, and your for loop runs until user input)
int g,h,i,a=0, grade[g],hour[h]; - what is the size of grade and hour? you can not "dynamically" give them a size. You must tell what size they are when you declare them, unless you use dynamic containers such as std::vector.

C++ Function Assignment grade calculator

my programming lecturer is teaching us how to write functions, terribly I might add, We are to make a program that calculates the grade of a students work. Here are the specs on it.
score 1 is weighted by 0.3,
score 2 is weighted by 0.5, and
score 3 is weighted by 0.2.
If the sum of the scores is greater than or equal to 85 then the Grade is an 'A'.
If the sum of the scores is greater than or equal to 75 then the Grade is a 'B'.
If the sum of the scores is greater than or equal to 65 then the Grade is a 'C'.
If the sum of the scores is greater than or equal to 50 then the Grade is a 'P'.
Otherwise the Grade is an 'F'.
So I wrote my code as follows:
#include <iostream>
using namespace std;
void calculateGrade() {
int score1, score2, score3;
int percentDec;
cin >>score1>>score2>>score3;
percentDec = (score1+score2+score3);
if (percentDec >= 85) {
cout << "The Course grade is: A";
}
else if (percentDec >= 75) {
cout << "The Course grade is: B";
}
else if (percentDec >= 65) {
cout <<"The Course grade is: C";
}
else if (percentDec >= 50) {
cout <<"The Course grade is: P";
}
else {
cout <<"The Course grade is: F";
}
} //end of calculateGrade()
int main() {
calculateGrade();
return 0;
}
Which works fine on my IDE but when I put it into the program which determines whether our answer is correct it doesn't work, that is because ordinarily we are asked only to put the stuff in main() but because it is a function and it's not in the main() it doesn't work like that. We are given this as an example and I'm about to throw something with how dumb this is. I don't know how to program it to work the way they want it.
cout << "The Course grade is: " << calculateGrade(90, 50, 99) << endl;
Cheers for any help.
This is not a forum for getting answers to your homework questions, although good job on showing what you have tried. Here are areas to look at:
1) The instructor is showing you that you can decompose code into functions. He/she wants you to wrtie a function calculateGrade that would work like this cout << "The Course grade is: " << calculateGrade(90, 50, 99) << endl;. Now every function declaration in C++ has three parts to it:
return_type functionName(param1_type param1, param2_type param2,...) {
// implementation
}
The functionName is what the function is referred to by (calculateGrade in this case), the parameters are the information you need to pass to the function for it to do its thing, and the return type is what the function will give back. In this case, your instructor is saying calculateGrade will take three integers as parameters and must return a string representing the grade of the student's scores. Thus your function should look like:
string calculateGrade(int score1, int score2, int score3) {
// ...
}
2) As the comments rightly pointed out, you aren't multiplying score1, score2, and score3 by their respective weights in the calculateGrade() method.
From your question and comments, I get the feeling your grasp of functions is not completely solid. Rather than complaining about your teacher (be it his/her fault or not), I suggest you read here about it. There are a plethora of online resources that will help you learn the basics of C++ programming.
You tutor is asking you to write a function which accept 3 parameter and return the grade.
char calculateGrade(int score1, int score2, int score3) {
char grade = 'F';
double percent = (0.3*score1 + 0.5*score2 + 0.2*score3);
if(...) {
grade = 'A/B/C/P'; // Depending upon condition, assign only value
}
else if(...) {
grade = 'A/B/C/P'; //Depending upon condition, assign only one value
}
// Add the condition in if else statements to get the actual grade.
return grade;
}
Note that the percent is of type double. You need to do all comparison in if else on double basis.

Sentinel interfering with finding minumum value in c++

I'm trying to write a program that takes in grades of an exam.(once the user is done inputting the grades he should press -1. the program should find the max and min grade and compute the average of all the grades.
My problem is that -1 is always the minumum grade computed. i tried fixing this problem by putting intmain1=999 (a very large number), but it also didnt work.
any suggestions?
thank you so much!
#include <iostream>
using namespace std;
int main(){
int grade1,average1;
int max1=0;
int min1=0;
int counter=0;
int sum=0;
cout<<"Enter grades of quiz 1. Use '-1' when there are no more grades."<<endl;
cin>>grade1;
while (grade1!=-1){
cin>>grade1;
counter++;
sum=sum+grade1;
if (grade1>max1)
max1=grade1;
else if (grade1<min1)
min1=grade1;
}
average1=sum/counter;
cout<<"The maximum grade of quiz 1 is"<<max1<<endl;
cout<<"The minumum grade of quiz 1 is"<<min1<<endl;
cout<<"THe average of quiz one is"<<average1<<endl;
}
All you need to do is move the cin>>grade1; from the first line of your while loop to the last. As your code is written, once they enter -1 it does all the logic in the loop and then finally stops, where you want it to stop before processing the -1.

For loop not letting me change input

The point of this program is for the user to enter the grade of a certain amount of students up to 50, from a range of grades A,B,C,D, or F. At the end, the program is then supposed to show how many students got each grade. Whenever I test the following code, whatever I input for the for loop repeats every time, such that if I input for it to do the grades 3 students, whatever letter I enter for student 1 will be the same grade for every student, so if one student has an A, they all will have an A. I also have to use arrays for this program because it's for college. Sorry if there's not enough information, this is my first time posting.
#include<iostream>
#include<iomanip>
#include<string>
void gradeTotals();
using namespace std;
int x,z,a=0,b=0,c=0,d=0,f=0,i=0;
char grade[50];
int main()
{
cout<<"Please enter the number of students"<<endl;
cin>>x;
for (i=0;i<x;i++)
{
int y;
y=i+1;
cout<<"Please enter a letter grade of A,B,C,D, or F for student "<<y<<endl;
cout<<"All grades must be uppercase"<<endl;
cin>>z;
grade[i]=z;
gradeTotals();
}
}
void gradeTotals()
{
if (grade[i]=='A')
{
a++;
}
else if (grade[i]=='B')
{
b++;
}
else if (grade[i]=='C')
{
c++;
}
else if (grade[i]=='D')
{
d++;
}
else if (grade[i]=='F')
{
f++;
}
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
cout<<f<<endl;
}
It looks like your if statements are not doing what you expect. For instance:
if (grade[i]='B')
{
// This code will *always* execute
}
You ought to be using the double equals == to compare a value, and a single equals = to assign a value.
(Edit after additional code change)
Inside the for-loop, you are trying to use cin to read in a single character. However, since the z is an integer, cin is looking for a valid integer, which does not happen to include 'A' or 'B', etc.
Perhaps you should try using a getline() or get().
The problem lies in having your input variable as an int, take in a char.
What happens is that when you perform cin >> z;, the character that was input by the user is recognized as an invalid input by the >> operator and therefore does not extract the character.
As such, z does not get any value, the character stays in the stream, and the >> operator continues to fail to extract the character until the loop ends.
Therefore, you can solve your problem by making your input variable a char instead.
Here's a link to help you better understand how to avoid such problems in the future.
Thank you for reading.

Declaring functions part two

Guys please help me again with my program. I changed the order of codes in it. Please check what is wrong with my codes. It runs but it doesn't perform the task it should do. It should compute the total of the grades inputted by the user and show the corresponding remarks. Unfortunately, it doesn't work :( please help me
#include<iostream>
#include<conio.h>
using namespace std;
void computePG(int& PG);
void Remark(int PG);
int x, y, z, w, p;
int prelimGrade,yourRemark,PG;
int preliminaryGrade;
int main()
{
int pGrade;
cout<<"Programmed by: Katrina G. Gozo, 1ISC";
cout<<endl<<"\nDate: Aug. 23,2013";
cout<<endl<<"\nThis program intends to compute the PG and make the necessary remarks";
cout<<"\n\nPlease enter your score on quiz 1 ";
cin>>x;
cout<<"\nPlease enter your score on quiz 2 ";
cin>>y;
cout<<"\nPlease enter your score on quiz 3 ";
cin>>z;
cout<<"\nPlease enter your score on prelims ";
cin>>p;
computePG(pGrade);
Remark(pGrade);
getch();
}
void computePG(int& PG)
{
PG = x/30 * 20 + y/50 * 20 + z/40 * 20 + w/100 * 40;
cout << "\nYour prelim grade is " << PG;
}
void Remark(int PG)
{
if (PG>=90)
cout<<"A." <<endl;
else if (PG>=80)
cout<<"B."<<endl;
else if (PG>=70)
cout<<"C."<<endl;
else if (PG>=60)
cout<<"D."<<endl;
else
cout<<"E."<<endl;
}
You're most likely running afoul of integer arithmetic. Note: when dividing an integer by another integer, you get an integer result (rounded towards zero).
So you'll want to use double as the type of PG and pGrade, and make the constants in computePG floating points numbers as well, by writing them as 30.0, 20.0, etc.
You should probably use a double for your variable "PG," that way you'll have adequate decimal accuracy.
Also, you may want to avoid global variables in the future, because I'm guessing it's how you made this error - you never assign a value to w before using it, which means it is assigned the value 0 by the compiler and may be what is screwing up your result.