Reputation: 247
I am making a random maths console-project that asks the user addition, subtraction, multiplication, division, power and square-root questions!
I am trying to make the code more complex by telling the user the score they got the last time they ran the test and advise which difficulty to choose.
However, I am getting an error "Specified cast is not valid" when I run the code.
Here is part of my code where the error is occurring:
[Serializable]
public class ToFile
{
public int TotalScore { get; private set; }
public int NumberOfQuestions { get; }
public UserDifficulty UserDifficulty { get; }
public ToFile(int numberOfQuestions, UserDifficulty userDifficulty)
{
NumberOfQuestions = numberOfQuestions;
UserDifficulty = userDifficulty;
}
public static void Serialize()
{
var (userDifficulty, numberOfQuestions) = UserInputs();
ToFile obj = new ToFile(numberOfQuestions, userDifficulty);
_ = obj.NumberOfQuestions;
_ = obj.UserDifficulty;
_ = obj.TotalScore;
Stream stream = new FileStream("Example.txt", FileMode.Create, FileAccess.Write);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Close();
}
public static void Deserialize()
{
Stream stream = new FileStream("Example.txt", FileMode.Open, FileAccess.Read);
BinaryFormatter formatter = new BinaryFormatter();
ToFile objnew = (ToFile)formatter.Deserialize(stream);
stream.Close();
Console.WriteLine($"Last time you did the test on {objnew.UserDifficulty} level and got {objnew.TotalScore}/{objnew.NumberOfQuestions}");
double decimalScore = (double)objnew.TotalScore / (double)objnew.NumberOfQuestions;
if (objnew.UserDifficulty == UserDifficulty.Easy)
{
if (decimalScore <= 0.7)
{
Console.WriteLine($"You should stay on Easy difficulty");
}
else
{
Console.WriteLine($"Easy difficulty seems to easy for you! You should go up to Normal difficulty");
}
}
else if (objnew.UserDifficulty == UserDifficulty.Normal)
{
if (decimalScore <= 0.3)
{
Console.WriteLine($"Normal difficulty seems to be to hard for you:( You should go down to Easy difficulty");
}
else if ((decimalScore > 0.3) && (decimalScore <= 0.7))
{
Console.WriteLine($"You should stay on Normal difficulty");
}
else
{
Console.WriteLine($"Normal difficulty seems to easy for you! You should go up to Hard difficulty");
}
}
else if (objnew.UserDifficulty == UserDifficulty.Hard)
{
if (decimalScore <= 0.3)
{
Console.WriteLine($"Hard difficulty seems to hard for you:( You should go down to Normal difficulty");
}
else if ((decimalScore > 0.3) && (decimalScore <= 0.8))
{
Console.WriteLine($"You should stay on Hard difficulty");
}
else
{
Console.WriteLine($"You are a maths Genius! Sadly this is the hardest level");
}
}
Console.ReadKey();
Console.Write(Environment.NewLine);
}
}
public static void Main(string[] args)
{
ToFile.Deserialize();
ToFile.Serialize();
}
Here is my full code: https://github.com/CrazyDanyal1414/mathstester/blob/master/Program.cs
Can anyone help me?
Upvotes: 1
Views: 986
Reputation: 5213
I am getting an error "Specified cast is not valid" when I run the code.
I tried your code from github
and it worked for me. I suspect that your file Examples.txt
with last user choice contains invalid data therefore you get an error when you try to deserialize data contained in it. Try to delete the file and then create it again. I think that it will solve the problem.
My code at the moment runs my
UserInputs
function twice. Could you help me fix this?
The problem is that you call it twice:
Main
before calling method ToFile.Serialize
;ToFile.Serialize
.To fix the problem you should rewrite you code in Main
:
ToFile.Deserialize();
var (userDifficulty, numberOfQuestions) = UserInputs();
OperationQuestionScore score = RunTest(numberOfQuestions, userDifficulty);
// 1. You should serialize data after running a test, because property
// ToFile.TotalScore can be filled only after test.
// 2. Now we pass into method Serialize three parameters: numberOfQuestions,
// score.TotalScore and userDifficulty.
ToFile.Serialize(numberOfQuestions, score.TotalScore, userDifficulty);
And make next changes to the ToFile.Serialize
method:
public static void Serialize(int numberOfQuestions, int totalScore, UserDifficulty userDifficulty)
{
// Now you can delete this line, because user input comes from method parameters.
// var (userDifficulty, numberOfQuestions) = UserInputs();
// And then serialize values: numberOfQuestions, totalScore, userDifficulty.
...
}
Upvotes: 1