I NEED HELP ASAP!!!

ok guys, im trying to code a grade calculator for my programming class and my if/else if block is screwing me over because it equates y/n to elicit the same response.

every time i run the program and, say i put n to answer the if else statement, it will tell the program that both conditions were satisfied and idk it's screwing me over

Here's my code





#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//declare all grade variables
int homework1,homework2,homework3,homework4,homework5,homework6,homework7,recitation1,recitation2,recitation3,recitation4,recitation5,recitation6,midterm1,midterm2,finalexam;

cout << "Welcome to the Grade Calculator!" << endl;

cout << "Can I have your homework grades in order from homework's 1-7?" << endl;
cin >> homework1 >> homework2 >> homework3 >> homework4 >> homework5 >> homework6 >> homework7;

cout << "Can I have your recitation grades in order from recitation's 1-7?" << endl;
cin >> recitation1 >> recitation2 >> recitation3 >> recitation4 >> recitation5 >> recitation6;

cout << "Can I have your test grades for miderm 1 and midterm 2 in that order?" << endl;
cin >> midterm1 >> midterm2;

cout << "Can I have the grade of your final exam?" << endl;
cin >> finalexam;


//grades posted as a list for visual aid
cout << "HOMEWORK GRADES -->\n" << "Homework 1:" << homework1 << endl;
cout << "Homework 2:" << homework2 << endl;
cout << "Homework 3:" << homework3 << endl;
cout << "Homework 4:" << homework4 << endl;
cout << "Homework 5:" << homework5 << endl;
cout << "Homework 6:" << homework6 << endl;
cout << "Homework 7:" << homework7 << endl;

cout << "\nRECITATION GRADES -->\n" << "Recitation 1:" << recitation1 << endl;
cout << "Recitation 2:" << recitation2 << endl;
cout << "Recitation 3:" << recitation3 << endl;
cout << "Recitation 4:" << recitation4 << endl;
cout << "Recitation 5:" << recitation5 << endl;
cout << "Recitation 6:" << recitation6 << endl;

cout << "\nTEST GRADES -->\n" << "Midterm 1:" << midterm1 << endl;
cout << "Midterm 2:" << midterm2 << endl;
cout << "Final:" << finalexam << endl;


//declaring possible answers to extracred question
bool answ1;
int extracred;
char Y,y,N,n;

//I will declare your average variables
int homework_avg,test_avg,course_avg;

cout << "\nIs there any extra credit to add in? (Y or N) --> ";
cin >> answ1;
if (answ1 == Y || answ1 == y)
{
cout << "How many extra credit points have you earned?" << endl;
cin >> extracred;
//I will initialize averages with extracred
int homework_avg = (((homework1 + homework2 + homework3 + homework4 + homework5 + homework6 + homework6 + homework7 + recitation1 + recitation2 + recitation3 + recitation4 + reci$
}
else if (answ1 == N || answ1 == n)
{
//I will initialize averages without extracred
int homework_avg = (((homework1 + homework2 + homework3 + homework4 + homework5 + homework6 + homework6 + homework7 + recitation1 + recitation2 + recitation3 + recitation4 + reci$
}
else
{
cout << " Invalid option, exiting program";
}

//declare test avg and course avg
int test_avg = (((midterm1 * .175) + (midterm2 * .175) + (finalexam * .25) + (homework_avg * .40)) / 60);
int course_avg = ((midterm1 * .175) + (midterm2 * .175) + (finalexam * .25) + (homework_avg * .40));

//I will now tell you your averages
cout << "Homework Average:" << homework_avg << "%" << endl;
cout << "Test Average:" << test_avg << "%" << endl;
cout << "Course Average" << course_avg << "%" << endl;


//course grade if-else-if block
cout << "Course Grade:";
if (course_avg >= 92)
cout << "A";

else if (course_avg >= 90 && course_avg <= 92)
cout << "A-";

else if (course_avg >= 88 && course_avg < 90)
cout << "B+";

else if (course_avg >= 82 && course_avg < 88)
cout << "B";

else if (course_avg >= 80 && course_avg < 82)
cout << "B-";

else if (course_avg >= 78 && course_avg < 80)
cout << "C+";

else if (course_avg >= 72 && course_avg < 78)
cout << "C";

else if (course_avg >= 69 && course_avg < 72)
cout << "C-";

else if (course_avg >= 62 && course_avg < 69)
cout << "D";

else if (course_avg >= 60 && course_avg < 62)
cout << "D-";


else
cout << "F";





return 0;
}







Last edited on
1
2
3
4
5
6
7
8
9
10
	//declaring possible answers to extracred question
bool answ1;
	int extracred;
	char Y, y, N, n;

	//I will declare your average variables
	int homework_avg, test_avg, course_avg;

	cout << "\nIs there any extra credit to add in? (Y or N) --> ";
	cin >> answ1;


I will focus on what you are asking. The one thing that caught my attention is that your variable answ1 should be a char data type and not a bool.
why? i thought boolean variables stored true or false values. and i also thought that you needed that for if/else if blocks.

my main issue is with the if/else if block that asks for extra credit.

By the way, when i switch answ1 to char type it reads any response to "Is there any extra credit to add in? (Y or N) ==>" as an invalid option and it subsequently exits the program.

Oh and another thing are you allowed to put input and variable initialization within the if/else if block because I've never seen other people do this even though I don't see why you wouldn't be able to?
Last edited on
if (answ1 == Y || answ1 == y)

I guess you rather mean if (answ1 == 'Y' || answ1 == 'y')
Yeah, that worked. Thank you. Another thing, do you know how i can have an integer expression such as

int test_avg = (((midterm1 * .175) + (midterm2 * .175) + (finalexam * .25) + (homework_avg * .40)) / 60);

display the output as a double. Having already tried fixed << showpoint << setprecision(2)
nvm, got it
Topic archived. No new replies allowed.