Click here to Skip to main content
15,885,366 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.
 
Share this answer
 
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.

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