![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
Post some code.
|
|
|
|
|
|
#3 |
|
Programmer
|
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; |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Well, VB's always set variables to 0 or Empty upon declaration. I don't know about the others.
|
|
|
|
|
|
#7 |
|
Programmer
|
Sorry but I'm REALLY confused at what I'm suppose to do here
. |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Simple:
char blah = '\0'; int foo = 0; double me = 0; string hello = ""; |
|
|
|
|
|
#9 |
|
Programmer
|
Ahhhh I see now thanks
![]() |
|
|
|
|
|
#10 |
|
Programmer
|
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; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|