Reputation:
So, I am a new student in Software and I'm doing my assignment, this may seem like a simple question but it is wrecking my head.
The assignment is for the user to input 6 school subject and the grade they got for each, then to calculate the grades into points and display. I feel that I have the skeleton of the code done, however when I run the program no matter what input I put in the answer always seems to be invalid.
Thanks for your help, Joe
Here is the code I am using, its being used in Visual Studio.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace caoAssignment
{
class Program
{
static void Main(string[] args)
{
// Delcaring variables //
string subIn, sub1, sub2, sub3, sub4, sub5, sub6;
string gradeIn, grade1, grade2, grade3, grade4, grade5, grade6;
int pointsIn, points1, points2, points3, points4, points5, points6;
int x;
string doAgain = "";
// Getting the user to input necessary information. 2 peices needed.
do
{
x = 1;
while (x <= 6)
{
Console.WriteLine("Please enter a subject ");
subIn = Console.ReadLine();
Console.WriteLine("Please enter the grade you got in that subject (H1 - O8) ");
gradeIn = Console.ReadLine();
// Processing the grades in points //
if (gradeIn == "H1")
pointsIn = 100;
if (gradeIn == "H2")
pointsIn = 88;
if (gradeIn == "H3")
pointsIn = 77;
if (gradeIn == "H4")
pointsIn = 66;
if (gradeIn == "H5")
pointsIn = 56;
if (gradeIn == "H6")
pointsIn = 46;
if (gradeIn == "H7")
pointsIn = 37;
if (gradeIn == "H8")
pointsIn = 0;
if (gradeIn == "O1")
pointsIn = 56;
if (gradeIn == "O2")
pointsIn = 46;
if (gradeIn == "O3")
pointsIn = 37;
if (gradeIn == "O4")
pointsIn = 28;
if (gradeIn == "O5")
pointsIn = 20;
if (gradeIn == "O6")
pointsIn = 12;
if (gradeIn == "O7")
pointsIn = 0;
if (gradeIn == "O8")
pointsIn = 0;
else
Console.WriteLine("Input is wrong, please redo. ");
// Assigning each grade,subject and points depending on what X is //
if (x == 1)
{
subIn = sub1;
gradeIn = grade1;
pointsIn = points1;
}
if (x == 2)
{
subIn = sub2;
gradeIn = grade2;
pointsIn = points2;
}
if (x == 3)
{
subIn = sub3;
gradeIn = grade3;
pointsIn = points3;
}
if (x == 4)
{
subIn = sub4;
gradeIn = grade4;
pointsIn = points4;
}
if (x == 5)
{
subIn = sub5;
gradeIn = grade5;
pointsIn = points5;
}
if (x == 6)
{
subIn = sub6;
gradeIn = grade6;
pointsIn = points6;
}
x++;
} // End of the while loop
Console.WriteLine("Do again? (Y/N) ");
doAgain = Console.ReadLine();
} // End of the do loop
while (doAgain == "Y");
// Command to keep the program open for the user //
Console.ReadKey();
}
}
}
Upvotes: 0
Views: 174
Reputation: 29801
Take a look at this part of the code:
if (gradeIn == "O7")
pointsIn = 0;
if (gradeIn == "O8")
pointsIn = 0;
else
Console.WriteLine("Input is wrong, please redo. ");
What happens if grade is not "08"? The else
statement is only applied to the last if
statement. Also a good rule of thumb is to always add curly braces after if
, else
, etc, to indicate the scope and avoid mistakes.
If you chain the if
statements with else
, you are telling the program that the next if
is only going to apply if the previous was not applied.
if (gradeIn == "O7")
{
pointsIn = 0;
}
else if (gradeIn == "O8")
{
pointsIn = 0;
}
else
{
Console.WriteLine("Input is wrong, please redo. ");
}
When comparing strings using the ==
operator, the comparison is case sensitive. To make it case insensitive, use the overloaded string.Equals
method:
if(string.Equals(gradeIn, "O7", StringComparison.OrdinalIgnoreCase))
Upvotes: 1
Reputation: 26
Firstly, project should not compile, as code is trying to use unassigned variables. Sub1
//Assigning each grade,subject and points depending on what X is
if (x == 1)
{
subIn = sub1;
gradeIn = grade1;
pointsIn = points1;
}
Secondly
subIn = sub2;
This should be the other way. The first value is the variable you want to assign second value.
Upvotes: 0