Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 7th, 2005, 3:53 PM   #1
Haz
Programmer
 
Haz's Avatar
 
Join Date: Feb 2005
Location: England
Posts: 37
Rep Power: 0 Haz is on a distinguished road
Send a message via ICQ to Haz Send a message via AIM to Haz Send a message via MSN to Haz Send a message via Yahoo to Haz
Assignment of numbers to variables without asking

When I finally was able to compile my program, and ran it, when I tried to enter some numbers to variables, they already seemed to be assigned to them, with out any of the numbers found in the code. E.g: When I executed the speed, distance, time calculator and executed the speed case; it automatically did 13/10 = 1.3, without me assigning ANY variables at all. What is wrong? What should I do?

Thanks,

Haz
Haz is offline   Reply With Quote
Old May 7th, 2005, 4:06 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Post some code.
uman is offline   Reply With Quote
Old May 7th, 2005, 4:27 PM   #3
Haz
Programmer
 
Haz's Avatar
 
Join Date: Feb 2005
Location: England
Posts: 37
Rep Power: 0 Haz is on a distinguished road
Send a message via ICQ to Haz Send a message via AIM to Haz Send a message via MSN to Haz Send a message via Yahoo to Haz
This is just an extract of it:

using System;

class Avi {
	public static void Main () {
		char choice;
		char std;
		double speed;
		double distance;
		double time;
		int alt;
		double tempx;
		double x, y, z, a, b, c, d, e, f, g;
		int ac1, ac2;
		double vis;
		double dt, dg, dtg;
		
		for(;;) {
			do {
				
				Console.WriteLine("Choose one from the list:\n");
				Console.WriteLine("1.  Speed; Distance; Time");
				Console.WriteLine("2.  ISA Deviation");
				Console.WriteLine("3.  Time to Impact");
				Console.WriteLine("4.  Track Error; Closing Angle\n");
				Console.Write("Choose one (press Q to quit): ");
				do {
					choice = (char) Console.Read();
				} while(choice == '\n' | choice == '\r');
			} while( choice < '1' | choice > '4' & choice != 'q' );
			
			if(choice == 'q') break;
			
			Console.WriteLine("\n");
			
			switch(choice) {
				case '1':
					do {
						Console.WriteLine("Here you can calculate the Speed, Distance or Time\n");
						Console.WriteLine("Choose which one you want to calculate:");
						Console.WriteLine("1.  Speed");
						Console.WriteLine("2.  Distance");
						Console.WriteLine("3.  Time\n");
						Console.Write("Choose one: ");
						do {
							std = (char) Console.Read();
						} while(std == '\n' | std == '\r');
					} while( std <'1'| std > '3');
					
					Console.WriteLine("\n");
					
					
					switch(std) {
						case '1':
							Console.Write("Distance: ");
							distance = (double) Console.Read();
							Console.Write("Time: ");
							time = (double) Console.Read();
							
							speed = distance / time;
							
							Console.WriteLine("Your Distance was: " + distance);
							Console.WriteLine("Your Time was: " + time);
							
							Console.WriteLine("Thus your Speed is " + speed);
							
							Console.WriteLine("NOTE: Make sure the units are compatible with each other");
							break;
							
						case '2':
							Console.Write("Speed: ");
							speed = (double) Console.Read();
							Console.WriteLine("Time: ");
							time = (double) Console.Read();
							
							distance = speed * time;
							
							Console.WriteLine("Your Speed was: " + speed);
							Console.WriteLine("Your Time was: " + time);
							
							Console.WriteLine("Thus your Distance is: " + distance);
							break;
							
						case '3':
							Console.Write("Speed: ");
							speed = (double) Console.Read();
							Console.WriteLine("Distance: ");
							distance = (double) Console.Read();
							
							time = distance / speed;
							
							Console.WriteLine("Your Speed was: " + speed);
							Console.WriteLine("Your Distance was: " + distance);
							
							Console.WriteLine("Thus your Time is: " + time);
							break;
					}
			        break;
Haz is offline   Reply With Quote
Old May 7th, 2005, 8:27 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
When you declare a variable and assign it some space, it doesn't clear out or zero whatever's in that space - it just leaves it there. Therefore, I strongly advise you to allocate variables upon declaration.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 8th, 2005, 11:55 AM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Ooble, I knew that was the case in C/C++ but I had no idea C# didn't default to 0 on numeric variables like the other .Net languages.
Dameon is offline   Reply With Quote
Old May 8th, 2005, 12:49 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Well, VB's always set variables to 0 or Empty upon declaration. I don't know about the others.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 8th, 2005, 1:46 PM   #7
Haz
Programmer
 
Haz's Avatar
 
Join Date: Feb 2005
Location: England
Posts: 37
Rep Power: 0 Haz is on a distinguished road
Send a message via ICQ to Haz Send a message via AIM to Haz Send a message via MSN to Haz Send a message via Yahoo to Haz
Sorry but I'm REALLY confused at what I'm suppose to do here .
Haz is offline   Reply With Quote
Old May 8th, 2005, 3:37 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Simple:
char blah = '\0';
int foo = 0;
double me = 0;
string hello = "";
Basically, allocate things to the variables before you use them.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 8th, 2005, 5:11 PM   #9
Haz
Programmer
 
Haz's Avatar
 
Join Date: Feb 2005
Location: England
Posts: 37
Rep Power: 0 Haz is on a distinguished road
Send a message via ICQ to Haz Send a message via AIM to Haz Send a message via MSN to Haz Send a message via Yahoo to Haz
Ahhhh I see now thanks
Haz is offline   Reply With Quote
Old May 8th, 2005, 5:19 PM   #10
Haz
Programmer
 
Haz's Avatar
 
Join Date: Feb 2005
Location: England
Posts: 37
Rep Power: 0 Haz is on a distinguished road
Send a message via ICQ to Haz Send a message via AIM to Haz Send a message via MSN to Haz Send a message via Yahoo to Haz
Sorry, doesn't work, this is the code for the variable allocation:

char choice = '\0';
		char std = '\0';
		double speed = 0;
		double distance = 0;
		double time = 0;
		int alt = 0;
		double tempx = 0;
		double x = 0, y = 0, z = 0, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0;
		int ac1 = 0, ac2 = 0;
		double vis = 0;
		double dt = 0, dg = 0, dtg = 0;
Haz is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC