5 Replies - 5036 Views - Last Post: 27 November 2011 - 03:42 PM Rate Topic: ****- 1 Votes

#1 ChuckStar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-September 11

Stupid 0 before my dollar sign, how do I get rid of it?

Posted 27 November 2011 - 03:20 PM

What in the world did I do wrong? I can't figure out why this stupid zero keeps showing up before the dollar sign in the totalSalesLabel. Any input would be awesome! Thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test2
{
    struct Drinks
    {
        public string drinkName;
        public string drinkCost;
        public string drinkInventory;
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[,] drinksChoice = new string[,]  {{"Cola", "1.00", "20"},{"Root Beer", "1.00", "20"},
                                           {"Lemon Lime", "1.00", "20"},{"Grape Soda", "1.50", "20"},
                                           {"Cream Soda", "1.50", "20"}};
        int totalSold = 0;
        int totalSales = 0;
        
        int colaSold = 20;
        int rootBeerSold = 20;
        int lemonLimeSold = 20;
        int grapeSodaSold = 20;
        int creamSodaSold = 20;
        int total = 0;

        Drinks option = new Drinks();

        private void colaPictureBox_Click_1(object sender, EventArgs e)
        {
            colaSold--;
            option.drinkName = drinksChoice[0, 0];
            option.drinkCost = drinksChoice[0, 1];
            option.drinkInventory = drinksChoice[0, 2];
            colaRemainingLabel.Text = colaSold.ToString();
            total++;
            totalSales = total;
            totalSalesLabel.Text = totalSold.ToString() + totalSales.ToString("c");
            if (colaSold < 1)
            {
                MessageBox.Show("Sorry, Cola is Sold Out");
            }
        }

        private void exitButton_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rootBeer_Click(object sender, EventArgs e)
        {  
            rootBeerSold--;
            option.drinkName = drinksChoice[0, 0];
            option.drinkCost = drinksChoice[0, 1];
            option.drinkInventory = drinksChoice[0, 2];
            rootBeerLabel.Text = rootBeerSold.ToString();
            total++;
            totalSales = total;
            totalSalesLabel.Text = totalSold.ToString() + totalSales.ToString("c");
            if (rootBeerSold < 1)
            {
                MessageBox.Show("Sorry, Root Beer is Sold Out");
            }
        }

        private void lemonLime_Click_1(object sender, EventArgs e)
        {
            lemonLimeSold--;
            option.drinkName = drinksChoice[0, 0];
            option.drinkCost = drinksChoice[0, 1];
            option.drinkInventory = drinksChoice[0, 2];
            lemonLimeRemainingLabel.Text = lemonLimeSold.ToString();
            total++;
            totalSales = total;
            totalSalesLabel.Text = totalSold.ToString() + totalSales.ToString("c");
            if (lemonLimeSold < 1)
            {
                MessageBox.Show("Sorry, Lemon Lime is Sold Out");
            }
        }

        private void grapeSoda_Click(object sender, EventArgs e)
        {
            grapeSodaSold--;
            option.drinkName = drinksChoice[0, 0];
            option.drinkCost = drinksChoice[0, 1];
            option.drinkInventory = drinksChoice[0, 2];
            grapeRemainingLabel.Text = grapeSodaSold.ToString();
            total++;
            totalSales = total;
            totalSalesLabel.Text = totalSold.ToString() + totalSales.ToString("c");
            if (grapeSodaSold < 1)
            {
                MessageBox.Show("Sorry, Grape is Sold Out");
            }
        }

        private void creamSoda_Click(object sender, EventArgs e)
        {
            creamSodaSold--;
            option.drinkName = drinksChoice[0, 0];
            option.drinkCost = drinksChoice[0, 1];
            option.drinkInventory = drinksChoice[0, 2];
            creamSodaRemainingLabel.Text = creamSodaSold.ToString();
            total++;
            totalSales = total;
            totalSalesLabel.Text = totalSold.ToString() + totalSales.ToString("c");
            if (creamSodaSold < 1)
            {
                MessageBox.Show("Sorry, Cream Soda is Sold Out");
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Stupid 0 before my dollar sign, how do I get rid of it?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Stupid 0 before my dollar sign, how do I get rid of it?

Posted 27 November 2011 - 03:30 PM

Reminder to all: This is a homework question so completed source code is not allowed.

Chuck: YOu dropped in 125 lines of code with no reference to which output you are talking about. HOw about not making us scrub through it all. Which line are you talking about?

Sadly schools don't seem to be teaching students how to debug their own work.

See FAQ #5 for debugging tutorials


FAQ (Frequently Asked Questions - Updated Nov 2011

Spoiler

Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Stupid 0 before my dollar sign, how do I get rid of it?

Posted 27 November 2011 - 03:30 PM

Because you do a "totalSold.ToString()" prior to totalSales.ToString("c") where you set the label. You have never used this variable totalSold (so it is zero) and you make it a string so you are appending it on the front of the totalSales.

Make it this line...

totalSalesLabel.Text = totalSales.ToString("c");



That should fix you up. :)
Was This Post Helpful? 2
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Stupid 0 before my dollar sign, how do I get rid of it?

Posted 27 November 2011 - 03:31 PM

Hi,

Could it be something to do with this line (line 70):

totalSalesLabel.Text = totalSold.ToString() + totalSales.ToString("c");


What's the value of totalSold there?
Was This Post Helpful? 4
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Stupid 0 before my dollar sign, how do I get rid of it?

Posted 27 November 2011 - 03:34 PM

Just a review of your code:

It has a horrible amount of duplicate code. Every button click is identical to each other except for the product. This really should be a case of the soda type and cost being sent as parameters to single method that does everything you duplicated several times.

And your problem is a good example of *WHY*

You have this line in every button click handler:

totalSalesLabel.Text = totalSold.ToString() + totalSales.ToString("c");


So in order to fix your problem, you have to fix it 'n' times: Once for every handler that you put this line in.
Was This Post Helpful? 2
  • +
  • -

#6 ChuckStar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-September 11

Re: Stupid 0 before my dollar sign, how do I get rid of it?

Posted 27 November 2011 - 03:42 PM

Wow! Lots of helpful info, especially the FAQ's. I really appreciate the input and the help. I'm doing this online so it's hard to get any help.
Thanks for the tips, I appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1