-3
//Hold the Roses' cost
decimal numberofRoses;//user's desired number of roses
decimal totalCostofRoses; //the total amount of the roses' price
const decimal COST_PER_ROSE = 10;//each rose cost 10 dollars

//Get the user's input
numberofRoses = decimal.Parse(rosesTextBox.Text);

if ()//Require a condition where the user has to type in a whole number
{

}
else
{
    MessageBox.Show("Please input a whole number for the number of roses");
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • use a numericupdown set to 0 decimals, or just see of the text `.Contains()` the local decimal character – Ňɏssa Pøngjǣrdenlarp Feb 01 '15 at 23:20
  • 1
    Welcome to StackOverflow. Can you try google next time ? and [read this](http://mattgemmell.com/what-have-you-tried/) please. – aloisdg Feb 01 '15 at 23:26
  • yeah sorry about this I tried google I couldn't find the answer I was looking for. every program has different ways to set the condition. thanks for these comments though, sorry if I made a group of pro programmers angry lol... – Bleu Ray Feb 01 '15 at 23:36
  • 2
    @BlueRay I don't think you made anyone angry, it's just that the best part of programming, at least to me, is the excitement of solving a puzzle. When you ask such a very basic question, that appears to be part of school work, without doing much research (it's apparent, because there are a ton of resources on the subject), it makes one question if you're heading down the right path, if you catch my drift. – B.K. Feb 02 '15 at 00:04
  • @B.K. I love your comment. As programmer, we are just riddle player and we love it. – aloisdg Feb 02 '15 at 01:12
  • In the future, googling **how to check if a string is an integer c#**, which is exactly what you want to do, should get you [to this question and answer](http://stackoverflow.com/questions/1752499/c-sharp-testing-to-see-if-a-string-is-an-integer) – djv Feb 02 '15 at 04:40

2 Answers2

4

You can use TryParse() :

int num; // use a int not a decimal. You dont want half of a rose.
if (Int32.TryParse(rosesTextBox.Text, out num)
{
    // do something with num. it is an int.
}
else
{
    MessageBox.Show("Please input a whole number for the number of roses");
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • I have not yet learn what Int32 does and what's its purpose. However this condition works. I'm going to use the less advance method to determine if the user input has to be a whole number. Great answer though thanks. – Bleu Ray Feb 01 '15 at 23:33
0

Divide it by 1 and if the remainder is 0, it is a whole number:

if ((numberofRoses % 1) == 0) 
{

}

You can also parse the number as an int using Int32.TryParse(), which would be more effective:

int roses = 0;
if (int.TryParse(numberofRoses, out roses)
{
    //"roses" is now an int (Whole number)
}
Cyral
  • 13,999
  • 6
  • 50
  • 90