Click here to Skip to main content
15,029,206 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project at school that I'm struggling with. We use the Microsoft Visual Studio to build Windows Forms using .NET framework. I have my form functional for calculating the numerical average and having that output to the label I've set for that.

My issue is I have formatted nested if loops that tell it if the average is 95 or higher then the restaurant is rated as "Four Star". There are 5 of these text ratings. My issue is I have no clue how to code the output and have it display in the label I've set for that purpose. I'll include a screenshot of my program.

I played around with my variable "rate" to attempt to create code that is agreeable in Microsoft Visual Studio. I hope this is all comprehensible and that you know what I'm asking. When the program is run it shows an error "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format." This error shows applying to the "rate" variable.

I honestly am brand new to C# and have realized there is a lot of differences between this and C as well as this IDE and the IDE I was using before. Thank you in advance.

***NOTE*** I am not looking for someone to do the work for me. But maybe help guide me and help me understand why it is the way it is.

What I have tried:

C#
int hours = short.Parse(txtHours.Text);
           int menu = short.Parse(txtMenu.Text);
           int friend = short.Parse(txtFriendliness.Text);
           int prices = short.Parse(txtPrices.Text);
           int clean = short.Parse(txtCleanliness.Text);
           int total = hours + menu + friend + prices + clean + clean;
           float rate = float.Parse(lblRating.Text);
           float rating = (float)total / 6;


           //Processing
           if (rating >= 95)
           {
              Console.WriteLine(rate.ToString("Four Star"));
           }else if (rating >= 85)
           {
               Console.WriteLine(rate.ToString("Fine Dining"));
           }else if (rating >= 75)
           {
               Console.WriteLine(rate.ToString("Good"));
           }else if (rating >= 65)
           {
               Console.WriteLine(rate.ToString("Okay"));
           }else
           {
               Console.WriteLine(rate.ToString("Poor"));
           }

           //Output
           lblAverage.Text = rating.ToString("n0");
           lblRating.Text = rate.ToString("a");
Posted
Updated 27-Jan-21 20:12pm
v2

1 solution

Well, the error message is clear. Looking at the documentation
Single.Parse Method (System) | Microsoft Docs[^] could help: it suggests that, at the time the
Quote:
float rate = float.Parse(lblRating.Text);
statement is executed, the lblRating.Text variable does NOT contain a valid representation of a floating point number.
   
Comments
Alan Brott 28-Jan-21 12:36pm
   
Ok broken down and after having stepped away from it I do understand what you're saying. I can't format it this way since it's alphabetical and not numerical. Any suggestions? Or anything I can do to clarify? I am still stumped on this project.

I did a similar project last semester in C and I understood that. But this transition to the way microsoft intellisense wants things laid out and the syntax of C# I'm just not understanding it yet. Thanks again.
CPallini 28-Jan-21 13:19pm
   
What exactly contains your string when the error occurs?
Alan Brott 28-Jan-21 17:23pm
   
When I attempt to run the program the error occurs in the declarations. I changed the variable to "float rate = Single.Parse(lblRating.Text);" and the same error occurs. I assume because the data I'm trying to assign to this variable is text and not a number. I hope this answers your question.
CPallini 29-Jan-21 2:00am
   
Nope, please give me an example of the text entered.
Alan Brott 29-Jan-21 12:05pm
   
The text says "Four Star" if the average is equal to or higher than 95. "Fine Dining" if its equal to or greater than 85. "Good" if its equal to or greater than 75. "Okay" if its greater than or equal to 65. "Poor" if its less than 65.
CPallini 29-Jan-21 13:31pm
   
And why are you assuming that Parse method would work on "Four Start" or "Fine Dining"?
Such strings are not valid representations of a float number. A valid representation could be, for instance, "5.34";
Alan Brott 29-Jan-21 13:35pm
   
I've assumed that because I had no where to go. I tried using the declaration "var" but I also attempted to set up the variable rate where the text will be stored before being outputted as a string declaration. But I couldn't get anything to format as I'm brand new to C#. This class it seems the professor says this is what you're doing now do it. My C class last semester there was reading and example videos from the professor. There is reading this semester but so far the readings have been learning how to use the Microsoft VS interface basically.

To answer in short I've assumed that because then intellisense didn't trigger any errors until I went to compile the program. I am absolutely clueless so I began doing whatever I could think of to attempt to get it to work correctly.
CPallini 29-Jan-21 13:49pm
   
If the user is allowed to enter textual ratings then you've to find a way to tranform such strings into numbers. You might, for instance, create a list of predefined strings and make the user choose one of them.
Alan Brott 29-Jan-21 13:52pm
   
I'm sorry for the confusion. The user will enter 5 numbers. They rate the restaurant's hours, menu, friendliness, prices, and cleanliness. The program then takes those numbers that the customer enters and gets an average. That average is then given one of the 5 ratings predefined by the program according to its numerical average.
Alan Brott 1-Feb-21 10:19am
   
I appreciate your help. I did end up figuring this out. Thank you again.
CPallini 1-Feb-21 10:22am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900