Identifier Expected
-
Hello, I am trying to create a program that reads in 3 numbers and averages them for my Programming 1 class. I am trying to use method call statements in order to do this. I got it all finished but when I built it I got three "Identifier Expected" errors on line 26, and I don't understand what that means. (I'm rather new) Here is what I've got for coding...I'm not sure if there is anything else I need to change about it other than the 3 errors, either.
using System; using System.Collections.Generic; using System.Text; namespace Program10 { class Program { static void GetNums(out double num1, out double num2, out double num3) { Console.Write("Enter first number: "); num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); num2 = double.Parse(Console.ReadLine()); Console.Write("Enter third number: "); num3 = double.Parse(Console.ReadLine()); } static void CalcAvg(out double num1, out double num2, out double num3) { double avgNum; avgNum = num1 + num2 + num3; avgNum = avgNum / 3; return (double) avgNum; } static double DspAvg(double avgNum, out num1, out num2, out num3) { Console.WriteLine("The average of {0:n}, {1:n}, and {2:n} is {3}", num1, num2, num3, avgNum); } static void Main() { double firstNum, secNum, thirdNum, avgNum; DspAvg(firstNum, secNum, thirdNum); GetNums(out firstNum, out secNum, out thirdNum); CalcAvg(avgNum); } }
Thanks in advance for your help! [Ross Murtle] [Computer Science Major - Iowa Central Community College/Iowa State University] -
Hello, I am trying to create a program that reads in 3 numbers and averages them for my Programming 1 class. I am trying to use method call statements in order to do this. I got it all finished but when I built it I got three "Identifier Expected" errors on line 26, and I don't understand what that means. (I'm rather new) Here is what I've got for coding...I'm not sure if there is anything else I need to change about it other than the 3 errors, either.
using System; using System.Collections.Generic; using System.Text; namespace Program10 { class Program { static void GetNums(out double num1, out double num2, out double num3) { Console.Write("Enter first number: "); num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); num2 = double.Parse(Console.ReadLine()); Console.Write("Enter third number: "); num3 = double.Parse(Console.ReadLine()); } static void CalcAvg(out double num1, out double num2, out double num3) { double avgNum; avgNum = num1 + num2 + num3; avgNum = avgNum / 3; return (double) avgNum; } static double DspAvg(double avgNum, out num1, out num2, out num3) { Console.WriteLine("The average of {0:n}, {1:n}, and {2:n} is {3}", num1, num2, num3, avgNum); } static void Main() { double firstNum, secNum, thirdNum, avgNum; DspAvg(firstNum, secNum, thirdNum); GetNums(out firstNum, out secNum, out thirdNum); CalcAvg(avgNum); } }
Thanks in advance for your help! [Ross Murtle] [Computer Science Major - Iowa Central Community College/Iowa State University]and which of these is line 26? don't tell us you are running Visual Studio with line numbering disabled! and please use < PRE > tags for code snippets. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
-
and which of these is line 26? don't tell us you are running Visual Studio with line numbering disabled! and please use < PRE > tags for code snippets. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
-
Hello, I am trying to create a program that reads in 3 numbers and averages them for my Programming 1 class. I am trying to use method call statements in order to do this. I got it all finished but when I built it I got three "Identifier Expected" errors on line 26, and I don't understand what that means. (I'm rather new) Here is what I've got for coding...I'm not sure if there is anything else I need to change about it other than the 3 errors, either.
using System; using System.Collections.Generic; using System.Text; namespace Program10 { class Program { static void GetNums(out double num1, out double num2, out double num3) { Console.Write("Enter first number: "); num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); num2 = double.Parse(Console.ReadLine()); Console.Write("Enter third number: "); num3 = double.Parse(Console.ReadLine()); } static void CalcAvg(out double num1, out double num2, out double num3) { double avgNum; avgNum = num1 + num2 + num3; avgNum = avgNum / 3; return (double) avgNum; } static double DspAvg(double avgNum, out num1, out num2, out num3) { Console.WriteLine("The average of {0:n}, {1:n}, and {2:n} is {3}", num1, num2, num3, avgNum); } static void Main() { double firstNum, secNum, thirdNum, avgNum; DspAvg(firstNum, secNum, thirdNum); GetNums(out firstNum, out secNum, out thirdNum); CalcAvg(avgNum); } }
Thanks in advance for your help! [Ross Murtle] [Computer Science Major - Iowa Central Community College/Iowa State University]First off, Welcome to Programming C#! It's my favorite language. There are a handfull of issues, but at least you gave it the good old college try. :) Let's get started: You were missing a close curly brace at the end, though that could have simply not made it to your clipboard. Next, (and this was the 'Identifier Expected' issue) in DspAvg you didn't define the data type for num1 num2 or num3. You must specify that they're double. After that, in DspAvg you also can't mark those parameters as 'out' because DspAvg never assigns them a value -- it just prints them on the screen. The 'out' keyword signifies that a value will be assigned to the parameter before the method returns. The same thing happened in CalcAvg. No need to mark them as 'out' since they're not being assigned in the method. In CalcAvg you explicitly cast avgNum to a double, which isn't necessary since avgNum is defined as a double at the beginning of the method. It'll still compile, but it's unecessary. Now, here it get's a little interesting. :) DspAgv is marked to return a double, but no value is returned (and one shouldn't be because you're just displaying the results). However, your CalcAvg method doesn't return a value and of course it really should. In Main it looks like you were trying to use DspAvg to show the collected values, but DspAvg requires a fourth parameter (the calculated average). You could either remove that parameter or not use DspAvg until you've done the calculation. In Main, when you call CalcAvg you'll need to store the return value. You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio). Here are the changes, hope it works for you. Now please don't expect another free homework assignment, but DO enjoy learning C#. :laugh:
using System; using System.Collections.Generic; using System.Text; namespace Program10 { class Program { static void GetNums(out double num1, out double num2, out double num3) { Console.Write("Enter first number: "); num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); num2 = double.Parse(Console.ReadLine()); Console.Write("Enter third number: "); num3 = double.Parse(Console.ReadLine()); } static double CalcAvg(double num1, double num2, double num3) { double avgNum; avgNum = num1 + n
-
First off, Welcome to Programming C#! It's my favorite language. There are a handfull of issues, but at least you gave it the good old college try. :) Let's get started: You were missing a close curly brace at the end, though that could have simply not made it to your clipboard. Next, (and this was the 'Identifier Expected' issue) in DspAvg you didn't define the data type for num1 num2 or num3. You must specify that they're double. After that, in DspAvg you also can't mark those parameters as 'out' because DspAvg never assigns them a value -- it just prints them on the screen. The 'out' keyword signifies that a value will be assigned to the parameter before the method returns. The same thing happened in CalcAvg. No need to mark them as 'out' since they're not being assigned in the method. In CalcAvg you explicitly cast avgNum to a double, which isn't necessary since avgNum is defined as a double at the beginning of the method. It'll still compile, but it's unecessary. Now, here it get's a little interesting. :) DspAgv is marked to return a double, but no value is returned (and one shouldn't be because you're just displaying the results). However, your CalcAvg method doesn't return a value and of course it really should. In Main it looks like you were trying to use DspAvg to show the collected values, but DspAvg requires a fourth parameter (the calculated average). You could either remove that parameter or not use DspAvg until you've done the calculation. In Main, when you call CalcAvg you'll need to store the return value. You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio). Here are the changes, hope it works for you. Now please don't expect another free homework assignment, but DO enjoy learning C#. :laugh:
using System; using System.Collections.Generic; using System.Text; namespace Program10 { class Program { static void GetNums(out double num1, out double num2, out double num3) { Console.Write("Enter first number: "); num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); num2 = double.Parse(Console.ReadLine()); Console.Write("Enter third number: "); num3 = double.Parse(Console.ReadLine()); } static double CalcAvg(double num1, double num2, double num3) { double avgNum; avgNum = num1 + n
-
Thank you SO MUCH for your help. I have learned from my mistakes and the fact that you took time out of your day to help a measly little college freshman like myself is greatly appreciated! Thanks again! :-D
No problem at all. The secret to becoming a successful developer is being resourceful and not giving up. Glad I could help.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team Blog -
First off, Welcome to Programming C#! It's my favorite language. There are a handfull of issues, but at least you gave it the good old college try. :) Let's get started: You were missing a close curly brace at the end, though that could have simply not made it to your clipboard. Next, (and this was the 'Identifier Expected' issue) in DspAvg you didn't define the data type for num1 num2 or num3. You must specify that they're double. After that, in DspAvg you also can't mark those parameters as 'out' because DspAvg never assigns them a value -- it just prints them on the screen. The 'out' keyword signifies that a value will be assigned to the parameter before the method returns. The same thing happened in CalcAvg. No need to mark them as 'out' since they're not being assigned in the method. In CalcAvg you explicitly cast avgNum to a double, which isn't necessary since avgNum is defined as a double at the beginning of the method. It'll still compile, but it's unecessary. Now, here it get's a little interesting. :) DspAgv is marked to return a double, but no value is returned (and one shouldn't be because you're just displaying the results). However, your CalcAvg method doesn't return a value and of course it really should. In Main it looks like you were trying to use DspAvg to show the collected values, but DspAvg requires a fourth parameter (the calculated average). You could either remove that parameter or not use DspAvg until you've done the calculation. In Main, when you call CalcAvg you'll need to store the return value. You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio). Here are the changes, hope it works for you. Now please don't expect another free homework assignment, but DO enjoy learning C#. :laugh:
using System; using System.Collections.Generic; using System.Text; namespace Program10 { class Program { static void GetNums(out double num1, out double num2, out double num3) { Console.Write("Enter first number: "); num1 = double.Parse(Console.ReadLine()); Console.Write("Enter second number: "); num2 = double.Parse(Console.ReadLine()); Console.Write("Enter third number: "); num3 = double.Parse(Console.ReadLine()); } static double CalcAvg(double num1, double num2, double num3) { double avgNum; avgNum = num1 + n
Jared Bienz [MSFT] wrote:
You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio).
I actually find that:
Console.ReadKey(true);
Is much better, as soon as you press any key (apart from a select few, NumLock, for example) it will close. The 'true' part means that it will not send the input to the buffer, so you won't actualy see the letter or number you pressed on the screen.My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -
The Undefeated
-
static double DspAvg(double avgNum, out num1, out num2, out num3)
is line 26. Sorry for the confusion. :-D
num1/2/3 are missing their types, something like int :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
-
Jared Bienz [MSFT] wrote:
You should probably have a Console.ReadLine at the end or the window will close before you see the results (if debugging in Visual Studio).
I actually find that:
Console.ReadKey(true);
Is much better, as soon as you press any key (apart from a select few, NumLock, for example) it will close. The 'true' part means that it will not send the input to the buffer, so you won't actualy see the letter or number you pressed on the screen.My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -
The Undefeated
Ah, much better. Thanks for pointing that out. I'm going to use that from now on in my SequentialWorkflow console demos!
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team Blog -
No problem at all. The secret to becoming a successful developer is being resourceful and not giving up. Glad I could help.
My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team BlogJared Bienz [MSFT] wrote:
The secret to becoming a successful developer is being resourceful and not giving up
Its not. The secret is caffeine and pizza. Plenty of it.
Deja View - the feeling that you've seen this post before.
-
Jared Bienz [MSFT] wrote:
The secret to becoming a successful developer is being resourceful and not giving up
Its not. The secret is caffeine and pizza. Plenty of it.
Deja View - the feeling that you've seen this post before.
Caffeine and Pizza inherit from Resource, don't they? ;)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
-
Caffeine and Pizza inherit from Resource, don't they? ;)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
Luc Pattyn wrote:
Caffeine and Pizza inherit from Resource, don't they?
They do. And best of all, they are Disposable - although the Garbage Collection is a bit unpredictable.;)
Deja View - the feeling that you've seen this post before.