Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Problem - how can I covert the int into string?

string

  • Please log in to reply
3 replies to this topic

#1 jclarke

jclarke

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 100 posts

Posted 25 March 2012 - 11:30 PM

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 BookClubApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {






           double pointScore;
           string str_score;


[COLOR=#ff0000]           pointScore = int.Parse(textBox1.Text);[/COLOR]
[COLOR=#ff0000]           [/COLOR]


            //Determine the points for books


            if (pointScore = [COLOR=#ff0000]"0"[/COLOR])
            {
                label3.Text = "1";
            }
            else if (pointScore = "1")
            {
                label3.Text = "5";
            }
            else if (pointScore = "2")
            {
                label3.Text = "15";
            }
            else if (pointScore = "3")
            {
                label3.Text = "30";


            }
            else
            {
                label3.Text = "60";
            }
        }
    }
}

Above code, in red - when I try and type up the 'int.parse()' it should convert or something to ensure the program reads the user's input to determine the points given from the input given. Am I missing something? Help?
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 26 March 2012 - 05:33 AM

double pointScore;
pointScore = int.Parse(textBox1.Text);


First of all, you're storing an int into a double, so the double precision is wasted here. Don't know if that was what you were trying to do, if you need to do some double precision math on this later. But that's a separate point...


            if (pointScore = "0")
            {
                label3.Text = "1";
            }
            else if (pointScore = "1")
            {
                label3.Text = "5";
            }
            else if (pointScore = "2")
            {
                label3.Text = "15";
            }
            else if (pointScore = "3")
            {
                label3.Text = "30";


            }
            else
            {
                label3.Text = "60";
            }


There are several things wrong here. First, in each 'if' statement, you're using the assignment operator '=' instead of the equivalence operator '=='.

Secondly, you're trying to compare doubles to strings. 'pointScore' is of type 'double', and "0" is a literal string.

Lastly, comparing doubles using the equivalence operator is generally a bad idea. This can be solved by switching 'pointScore' to type 'int', but if you must keep the double precision, the best way to compare is to subtract the value from the comparison value, and check to see whether the resulting difference is between a very small range (margin of error), such as 0.00001.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 cdg10620

cdg10620

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 344 posts
  • Programming Language:C#, JavaScript, PL/SQL, Transact-SQL, VBScript, Others

Posted 26 March 2012 - 05:30 PM

Along with what Greg has already stated, I would personally use a switch statement there to check those values.

switch (pointScore)
{
    case 0:
        label3.Text = "1";
        break;
    case 1:
        label3.Text = "5";
        break;
    case 2:
        label3.Text = "15";
        break;
    case 3:
        label3.Text = "30";
        break;
    default:
        label3.Text = "60";
        break;
}

  • 0
-CDG10620
Software Developer

#4 jclarke

jclarke

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 100 posts

Posted 27 March 2012 - 10:00 PM

Appericate the help guys but i fixed it all.
its all good. ;)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download