Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old July 22nd, 2010, 05:21 PM
Registered User
 
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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();
}
}
}
  #2 (permalink)  
Old July 25th, 2010, 06:39 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old July 27th, 2010, 07:11 PM
Registered User
 
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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();
}
}
}
  #4 (permalink)  
Old July 28th, 2010, 02:57 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old August 18th, 2010, 05:21 AM
Registered User
 
Join Date: Aug 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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]
  #6 (permalink)  
Old August 18th, 2010, 07:02 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default 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:
  #7 (permalink)  
Old August 18th, 2010, 09:26 PM
Registered User
 
Join Date: Aug 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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 View Post
'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
  #8 (permalink)  
Old August 19th, 2010, 05:58 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default oh

oh yes.
i forgot to write 'true' instead of '1'.
does it work?
  #9 (permalink)  
Old August 19th, 2010, 11:24 PM
Registered User
 
Join Date: Aug 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default yes

modify "1" to "true", it can work correctly.

Quote:
Originally Posted by irProject View Post
oh yes.
i forgot to write 'true' instead of '1'.
does it work?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with excercise in page 43 (Ch. 2) Luachan BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 8 January 25th, 2010 03:02 PM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM
Excercise answers for book GrantWillie C# 0 October 22nd, 2003 05:07 AM
C#,chapter 8 excercise akbard BOOK: Beginning ASP.NET 1.0 2 October 7th, 2003 03:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.