3 Replies - 7166 Views - Last Post: 18 September 2008 - 08:43 PM Rate Topic: -----

#1 ExplicitViper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 08-September 08

adding decimal to two places...

Post icon  Posted 18 September 2008 - 06:39 PM

i am trying to write a program that averages 5 test grades to two decimal places.
//Program Name: 	Test Interacter
//Created By: 	Casey Marcrum
//Date: 		19 September 2008
//Purpose: 		Accepts test scores from the user.

using System;

public class Tests
{
	public static void Main()
	{
			//Variable List
			
			decimal decFirstScore;
			string strFirstScore;
			decimal decSecondScore;
			string strSecondScore;
			decimal decThirdScore;
			string strThirdScore;
			decimal decFourthScore;
			string strFourthScore;
			decimal decFifthScore;
			string strFifthScore;
			decimal decFinalScore;
			decimal decTests = 5;
			
			//User Types Test Scores
			
			Console.WriteLine("Enter your First Test score: ");
			strFirstScore = decFirstScore.ToString("D");
			Console.ReadLine();
			
			Console.WriteLine("Enter your Second Test score: ");
			strSecondScore = decSecondScore.ToString("D");
			Console.ReadLine();
			
			Console.WriteLine("Enter your Third Test score: ");
			strThirdScore = decThirdScore.ToString("D");
			Console.ReadLine();
			
			Console.WriteLine("Enter your Fourth Test score: ");
			strFourthScore = decFourthScore.ToString("D");
			Console.ReadLine();
			
			Console.WriteLine("Enter your Fifth Test score: ");
			strFifthScore = decFifthScore.ToString("D");
			Console.ReadLine();
			
			
			
			//Calculation of Scores
			
			decFinalScore = ((decFirstScore + decSecondScore + decThirdScore + decFourthScore + decFifthScore)/decTests);
			
			//Output On Screen
			
			Console.WriteLine("Your average of the five tests is: {0}", decFinalScore);
	}
}


this is my code. when i try to compile it. it says that i didnt assign "Decimal decFirstscore-FifthScore a value" i am aware of this and i know how to assign it a value. the thing is i need the user to put in their test scores, and not me assigning it a test score. any suggestions?

oh and some of the stuff you guys post, are way to advanced for the stuff we have done in class. so try to keep it as "beginner" as you can. thank you :D

Is This A Good Question/Topic? 0
  • +

Replies To: adding decimal to two places...

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: adding decimal to two places...

Posted 18 September 2008 - 06:52 PM

You need to assign them an initial value, then when the user enters their values the initial value is over written with the new value. It's standard in a strongly typed language that you be required to supply an initial value to a variable.

Also, from someone who has taught college level programming courses, most instructors have no problem with students who take the initiative to learn stuff that isn't being taught in class. This is the case because college is only going to teach you the basics of what you need to know in order to program, the rest if up to you :)

//Program Name:     Test Interacter
//Created By:     Casey Marcrum
//Date:         19 September 2008
//Purpose:         Accepts test scores from the user.

using System;

public class Tests
{
    public static void Main()
    {
            //Variable List
            
            decimal decFirstScore = 0;
            string strFirstScore;
            decimal decSecondScore= 0;
            string strSecondScore;
            decimal decThirdScore= 0;
            string strThirdScore;
            decimal decFourthScore= 0;
            string strFourthScore;
            decimal decFifthScore= 0;
            string strFifthScore;
            decimal decFinalScore= 0;
            decimal decTests = 5;
            
            //User Types Test Scores
            
            Console.WriteLine("Enter your First Test score: ");
            strFirstScore = decFirstScore.ToString("D");
            Console.ReadLine();
            
            Console.WriteLine("Enter your Second Test score: ");
            strSecondScore = decSecondScore.ToString("D");
            Console.ReadLine();
            
            Console.WriteLine("Enter your Third Test score: ");
            strThirdScore = decThirdScore.ToString("D");
            Console.ReadLine();
            
            Console.WriteLine("Enter your Fourth Test score: ");
            strFourthScore = decFourthScore.ToString("D");
            Console.ReadLine();
            
            Console.WriteLine("Enter your Fifth Test score: ");
            strFifthScore = decFifthScore.ToString("D");
            Console.ReadLine();
            
            
            
            //Calculation of Scores
            
            decFinalScore = ((decFirstScore + decSecondScore + decThirdScore + decFourthScore + decFifthScore)/decTests);
            
            //Output On Screen
            
            Console.WriteLine("Your average of the five tests is: {0}", decFinalScore);
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 ExplicitViper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 08-September 08

Re: adding decimal to two places...

Posted 18 September 2008 - 07:55 PM

ok i compiled it, and it compiled with no problem. when i went to run the program i get this error. " Unhandled Exception: System.FormatException: Format specifier was invalid. at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfoinfo) at System.Decimal.ToString(String format) at Tests.Main()
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: adding decimal to two places...

Posted 18 September 2008 - 08:43 PM

There are some other issues in your code that need to be resolved first. Console.ReadLine returns the value entered by the user, therefore you need to store that value in something.

Now the following changes will provide you with the logic you need to run your application. Be aware that it would be prudent to wrap the string to double conversions in a Try/Catch to handle any input by the user that is not numeric. Otherwise the application will crash.

Here is one way you can accomplish what you want. From this example you should have no trouble fixing the rest of the code.
			Console.WriteLine("Enter your First Test score: ");
			strFirstScore = Console.ReadLine();
			decFirstScore= Convert.ToDouble(strFirstScore);
			
			Console.WriteLine("Enter your Second Test score: ");
			strSecondScore = Console.ReadLine();
			decSecondScore = Convert.ToDouble(strSecondScore);


You can also get the output to 2 decimal places by changing the following code.
Console.WriteLine("Your average of the five tests is: {0:D2}", decFinalScore);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1