CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2013
    Location
    Chattanooga TN
    Posts
    2

    Have Mercy 1st Post Visual C#

    I moderate a few forums..so I know how aggravating noobs are.

    Anyway..
    Trying to form a basic Calorie Counter App.. (Visual Studio 2010)

    My issue is that it appears my methods are not being called properly, I simply need to accept user input, and call the method (a simple method that multiplies the user input * a variable) and then display that answer out into a label. For some reason all I am getting in the output label is the exact input the user enters.

    Code:
    Code:
    namespace Calorie_Calculator
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            //Methods:
            private double FatCalories(double Fat)
            {
                return Fat * 9.0;
            }
    
            private double CarbCalories(double Carb)
            {
                return Carb * 4.0;
            }
    
    
            private void fatCalloriesButton_Click(object sender, EventArgs e)
            {
                //Variables for Fat Carbs & Calories
                double Fat, Calories;
    
                //Get Number of FAT Grams
                if (double.TryParse(fatTextBox.Text, out Fat))
                {
                    //Calculate Calories from Fat:
                    Calories = FatCalories(Fat);
    
                    //Display Fat Calories:
                    outputLabel.Text = Fat.ToString("n1");
                }
                else
                {
                    //Error Message:
                    MessageBox.Show("Enter a valid Number.");
                }
            }
    
            private void carbCallorieButton_Click(object sender, EventArgs e)
            {
                //Variables:
                double Carb, Calories;
    
                if (double.TryParse(carbTextBox.Text, out Carb))
                {
                    Calories = CarbCalories(Carb);
    
                    outputLabel.Text = Carb.ToString("n1");
                }
                else
                {
                    MessageBox.Show("Enter a valid Number.");
                }
    
                
            }
    
            private void exitButton_Click(object sender, EventArgs e)
            {
                this.Close();

    Any thoughts as to why the heck this isnt working? Again.. just a noob here.
    Last edited by Arjay; July 12th, 2013 at 02:30 PM.

  2. #2
    Join Date
    Jul 2013
    Location
    Chattanooga TN
    Posts
    2

    Re: Have Mercy 1st Post Visual C#

    Perhaps I need to elaborate a bit more..rather than being so vague.

    The app simply needs to use the methods to calculate the number of callories from fat grams as well as from carb grams.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Have Mercy 1st Post Visual C#

    This is the visual c++ forum, not the c# one! Please re-post to the c-sharp programming forum. Also, when posting code please use code tags. Go Advanced, select code and click '#'.

    [moderator - could you move please?]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Have Mercy 1st Post Visual C#

    Quote Originally Posted by JeepNDave View Post
    Any thoughts as to why the heck this isnt working? Again.. just a noob here.
    What isn't working? No values show up? The values are incorrect? What?

  5. #5
    Join Date
    Sep 2006
    Posts
    31

    Re: Have Mercy 1st Post Visual C#

    Quote Originally Posted by JeepNDave View Post
    I moderate a few forums..so I know how aggravating noobs are.

    Anyway..
    Trying to form a basic Calorie Counter App.. (Visual Studio 2010)

    My issue is that it appears my methods are not being called properly, I simply need to accept user input, and call the method (a simple method that multiplies the user input * a variable) and then display that answer out into a label. For some reason all I am getting in the output label is the exact input the user enters.
    You are passing the user input to the output label:
    Code:
    outputLabel.Text = Fat.ToString("n1");
    You need to pass the calories value to the output lable:
    Code:
    outputLabel.Text = Calories.ToString("n1");
    Kristof

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured