Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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
 
Old July 22nd, 2010, 08:05 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default TRY IT OUT Manipulating Variables with Mathematical Operators

Hi,

I've just typed in the following piece of code from chapter 3 page 47 of Beginning Visual C# 2010. From the very first line "static void Main(string[] args)", I get the following errors:
void = expected class, delegate, enum, interface or struct
on the [] after string, I get "Identifier Expected"
arg = expected class, delegate, enum, interface or struct
Those are the same errors I get if I try to compile it with CSC. I can't see any other errors in the code. Here's a copy of my code. I would really appreciate if anyone can offer me any assistance or show me where I'm going wrong. Thanks very much!!!

static void Main(string[] args)
{
double firstNumber, secondNumber;
string userName;
Console.WriteLine("Enter Your Name:");
userName = Console.ReadLine();
Console.WriteLine("Welcome {0}!", userName);
Console.WriteLine("Now Give Me A Number");
firstNumber = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Now Give Me Another Number:");
secondNumber = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The Sum of {0} and {1} is {2}.", firstNumber,
secondNumber, firstNumber + secondNumber);
Console.WriteLine("The Product of {0} and {1} is {2}", firstNumber,
secondNumber, firstNumber * secondNumber);
Console.WriteLine("The Result of Dividing {0} by {1} is {2}.",
firstNumber, secondNumber, firstNumber / secondNumber);
Console.WriteLine("The Remainder after Dividing {0} by {1} is {2}.",
firstNumber, secondNumber, firstNumber % secondNumber);]
Console.Readkey();

}
 
Old July 22nd, 2010, 10:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sounds like you're missing brace somewhere. Try creating a new class and pasting the example over the skeleton provided.
__________________
Joe
http://joe.fawcett.name/
 
Old July 22nd, 2010, 11:20 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by joefawcett View Post
Sounds like you're missing brace somewhere. Try creating a new class and pasting the example over the skeleton provided.
Hi Joe,

Please excuse my ignorance but I'm a beginner. I'm not quite sure if I've followed your advice correctly but here's what I've done and it's really gone off on one now:)

Thanks very much for your help:)

static Main(string[] args)
{

{
public class prog1() ; (with and without the parentesis and the semi colon)
double firstNumber, secondNumber;
string userName;
Console.WriteLine("Enter Your Name:");
userName = Console.ReadLine();
Console.WriteLine("Welcome {0}!", userName);
Console.WriteLine("Now Give Me A Number");
firstNumber = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Now Give Me Another Number:");
secondNumber = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The Sum of {0} and {1} is {2}.", firstNumber,
secondNumber, firstNumber + secondNumber);
Console.WriteLine("The Product of {0} and {1} is {2}", firstNumber,
secondNumber, firstNumber * secondNumber);
Console.WriteLine("The Result of Dividing {0} by {1} is {2}.",
firstNumber, secondNumber, firstNumber / secondNumber);
Console.WriteLine("The Remainder after Dividing {0} by {1} is {2}.",
firstNumber, secondNumber, firstNumber % secondNumber);]
Console.Readkey();

}

}
 
Old July 23rd, 2010, 07:30 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default C# - TRY IT OUT Manipulating Variables with Mathematical Operators

Surely someone must have an answer for this. It's only a basic program from chapter 3 in a beginner's C# book.
 
Old July 23rd, 2010, 07:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You need to add your code to a class, like this:

Code:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
 class Program
 {
  static void Main(string[] args)
  {
    double firstNumber, secondNumber;
    string userName;
    Console.WriteLine("Enter Your Name:");
    // Rest of your code here
  }
 }
}
Hope this helps,

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!
The Following User Says Thank You to Imar For This Useful Post:
bargeanimal (July 23rd, 2010)
 
Old July 23rd, 2010, 08:44 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Firstly, we don't have the book you are referring to.

Secondly, you have posted to a C# 1.0 forum, when the book you are referring to is a C# 4.0 topic. Also each book should have its own forum where hopefully the author, or at least people who have access to the book are watching.

Thirdly, have you tried downloading to books source code. Maybe this contains the example you are trying to type in and you can spot the differences.

Finally, as Imar says your Main method should be inside a class, and that probably inside a namespace. Usually there are those using statements at the top.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 23rd, 2010, 11:37 AM
Registered User
 
Join Date: Jul 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks a million Imar. That did the trick! Many many thanks!!!
 
Old September 8th, 2010, 05:29 PM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default firstNumber % secondNumber

Quote:
Originally Posted by bargeanimal View Post
Surely someone must have an answer for this. It's only a basic program from chapter 3 in a beginner's C# book.
Learning C# for 1st time and using and on pg 48 of 1037 and Ch03Ex02 from the book and downloaded code the division operator is not working as expected for me...

Welcome... give#1(5)
give#2(5)

each math answer works except the last one:
Code:
onsole.WriteLine("The result of dividing {0} by {1} is {2}.",
                firstNumber, secondNumber, firstNumber % secondNumber);
It returns as
Quote:
The result of dividing 5 by 5 is 0.

Code:
#region Using D's

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#endregion

namespace Ch03Ex02
{
    class Program
    {
        static void Main(string[] args)
        {
            double firstNumber, secondNumber;
            string userName;
            Console.WriteLine("Enter your name:");
            userName = Console.ReadLine();
            Console.WriteLine("Welcome {0}!", userName);
            Console.WriteLine("Now give me a number:");
            firstNumber = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Now give me another number:");
            secondNumber = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("The sum of {0} and {1} is {2}.", firstNumber,
                secondNumber, firstNumber + secondNumber);
            Console.WriteLine("The result of subtracting {0} from {1} is {2}.",
                secondNumber, firstNumber, firstNumber - secondNumber);
            Console.WriteLine("The product of {0} and {1} is {2}.", firstNumber, secondNumber, firstNumber * secondNumber);
            Console.WriteLine("The result of dividing {0} by {1} is {2}.",
                firstNumber, secondNumber, firstNumber % secondNumber);
            Console.ReadKey();

        }
    }
}
I don't want to think of how much work it takes to make a book like this... it's crazy and I'm thankful for it... just learning something for the first time and getting not just right code or not explicit instructions are frustrating; I want sample code to simply work :( Why is it returning the "0" and not 1?
 
Old September 8th, 2010, 06:28 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummm...either the book is wrong or you miscopied.

The division operator is *NOT* %

That is the *MODULO* operator. And 5 % 5 is indeed zero.

Try using / instead of %

(FWIW: "modulo" means "the remainder left over when doing an integer division". If you hark back to your elementary school days when you first learned division, you probably had problems where the teacher would ask you "What is the remainder when you divide 17 by 5?" The answer is of course 2. That is: 17/5 is 3 remainder 2. In virtually all computer languages, a "modulo" operator is available to do that for you. Indeed 17 % 5 ==>> 2.)
The Following User Says Thank You to Old Pedant For This Useful Post:
oldSqlNewToC (September 9th, 2010)
 
Old September 9th, 2010, 09:23 AM
Registered User
 
Join Date: Sep 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Thanks

LOL

The obvious worked ' / ' and does provide the division I was thinking of...

Thanks for explaining the ' % ' but after looking at wiki I still don't get its use but the wording in the book had my simple mind thinking it was talking about ÷

Thank you :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Mathematical puzzles Will BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 13 May 30th, 2010 08:38 AM
plsql mathematical function RatanKumar SQL Language 1 May 29th, 2008 09:04 AM
How to do Mathematical expressions (seriously) Apocolypse2005 Beginning VB 6 5 December 15th, 2006 03:35 PM
Math operators nguyendh BOOK: Ivor Horton's Beginning Visual C++ 2005 1 June 25th, 2006 01:35 AM
Matrix operators Ravel99 Pro VB 6 0 April 28th, 2006 03:45 AM





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