 |
BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
 | This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

July 22nd, 2010, 05:21 PM
|
|
Registered User
|
|
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 4 Excercise 2
I don't get how by only using logic from Exercise 1 allows you to do this problem. I've tried if statements, but it gives me an error when I run the program. My problem is that I don't know what to put in the brackets to get it to return to the user input if the number input is greater than 10?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace summaryexample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int myInt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number:");
int myInt2 = Convert.ToInt32(Console.ReadLine());
if (myInt < 10 && myInt2 < 10)
Console.WriteLine("{0} and {1} are less than 10", myInt, myInt2);
else
{
}
Console.ReadKey();
}
}
}
|
|

July 25th, 2010, 06:39 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
It would help if you a) better explained what it is you're trying to accomplish and b) post the actual error messages you're getting.
Most of the visitors to this forum don't have this book, which makes it difficult to see what you're working on. Posting a more detailed question surely helps in getting a better answer.
Cheers,
Imar
|
|

July 27th, 2010, 07:11 PM
|
|
Registered User
|
|
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Response
I actually fixed the if statement in the code, so that is no longer a problem. The problem I am actually having is that I am uncertain as to what to put after the else statement that will return to the initial section, which asks the user for the numbers.
What the program is supposed to do is ask the user for a number, then after that number is entered it asks the user for another number. The program receives the numbers and if both numbers are less than 10, it outputs the message "number" and "number" are less than 10. That part works fine, the problem is if the numbers are more than 10. If the numbers entered are more than 10, the program is supposed to ask for two new numbers. However, I am uncertain what to put in the else statement to have the program loop to the beginning if the numbers are more than 10.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace summaryexample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int myInt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number:");
int myInt2 = Convert.ToInt32(Console.ReadLine());
if (myInt < 10 && myInt2 < 10)
Console.WriteLine("{0} and {1} are less than 10", myInt, myInt2);
else
{
<----I need to know what I should put here to get the program to return and ask for new numbers.
}
Console.ReadKey();
}
}
}
|
|

July 28th, 2010, 02:57 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Rather than using an if, I think what you're looking for is a while or a do while loop that loops as long both numbers are greater than or equal to 10.
For a simple example, check: http://www.softsteel.co.uk/tutorials...p/lesson9.html
If you want the complete solution, let me know,. Haven't posted it yet as I think figuring it out yourself is way more fun.
Cheers,
Imar
|
|

August 18th, 2010, 05:21 AM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i match the same trouble, the following is my troubleshoot: i add a goto Label:again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace summaryexample
{
class Program
{
static void Main(string[] args)
{
again:
Console.WriteLine("Enter a number:");
int myInt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number:");
int myInt2 = Convert.ToInt32(Console.ReadLine());
if (myInt < 10 && myInt2 < 10)
Console.WriteLine("{0} and {1} are less than 10", myInt, myInt2);
else
{
goto again;
}
Console.ReadKey();
}
}
}[/QUOTE]
|
|

August 18th, 2010, 07:02 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
|
|
No No No!
'goto' is a Curse!
dont use goto even if your neck is under the guillotine!
you can use this statements
Code:
static void Main(string[] args)
{
do{
Console.WriteLine("Enter a number:");
int myInt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number:");
int myInt2 = Convert.ToInt32(Console.ReadLine());
if (myInt < 10 && myInt2 < 10)
{
Console.WriteLine("{0} and {1} are less than 10", myInt, myInt2);
break;
}
}while(1);
Console.ReadKey();
}
click Thanks button if it helps
Last edited by irProject; August 18th, 2010 at 07:04 AM..
|
|
The Following User Says Thank You to irProject For This Useful Post:
|
|
|

August 18th, 2010, 09:26 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
While(1) compile generate error!!
yes, you give me alert! 3Q~~~~but do{} while(1); press F6 it generate a Error:Constant value '1' cannot be converted to a 'bool', maybe vs C# can't
agree with this syntax, so i stand on the shoulders of giants make a little modify : you can declaration a bool "type=true", do {} while(type); when you have a better idea, Pls push it to me !
Quote:
Originally Posted by irProject
'goto' is a Curse!
dont use goto even if your neck is under the guillotine!
you can use this statements
Code:
static void Main(string[] args)
{
do{
Console.WriteLine("Enter a number:");
int myInt = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number:");
int myInt2 = Convert.ToInt32(Console.ReadLine());
if (myInt < 10 && myInt2 < 10)
{
Console.WriteLine("{0} and {1} are less than 10", myInt, myInt2);
break;
}
}while(1);
Console.ReadKey();
}
click Thanks button if it helps
|
|
|

August 19th, 2010, 05:58 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
|
|
oh
oh yes.
i forgot to write 'true' instead of '1'.
does it work?
|
|

August 19th, 2010, 11:24 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes
modify "1" to "true", it can work correctly.
Quote:
Originally Posted by irProject
oh yes.
i forgot to write 'true' instead of '1'.
does it work?
|
|
|
 |