My output is always "1" [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to create a miles per gallon calculator with C#, but my output is always "1" no matter the input.
Is my equation not created correctly? I am supposed to base my code off of a previous assignment which looks very similar, so (if possible) try to help me find a way to keep the same relative structure to the code.
Thank you so much for any help!
Here is my code:
namespace Miles_Per_Gallon
{
class Program
{
static void Main(string[] args)
{
float Miles, Gallons, MPG;
string textline;
Console.Write("Miles Traveled :");
textline = Console.ReadLine();
Console.Write("Gallons of Gas Used :");
textline = Console.ReadLine();
Miles = float.Parse(textline);
Gallons = float.Parse(textline);
MPG = Miles / Gallons;
Console.Write("Miles Per Gallon : ");
Console.WriteLine(MPG.ToString());
Console.ReadLine();

Here is the fix one, try this and compare with your old code, you should figure out the problem.
float Miles, Gallons, MPG;
string textline;
Console.Write("Miles Traveled :");
textline = Console.ReadLine();
Miles = float.Parse(textline); //Store to variable
Console.Write("Gallons of Gas Used :");
textline = Console.ReadLine();
Gallons = float.Parse(textline); //Store to variable
MPG = Miles / Gallons;
Console.Write("Miles Per Gallon : ");
Console.WriteLine(MPG.ToString());
Console.ReadLine();

You are using the same variable textline to store both miles and gallons input. And when you divide them together you'll get 1.
Use different variables to get input of both miles and gallons

Related

Asking for a help. How do I fix this? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 22 days ago.
Improve this question
`static void Main(string[] args)
{
double celsius, Fahrenheit;
Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Fahrenheit temperature : ");
celsius = (Fahrenheit - 32) * 5 / 9;
Console.WriteLine();
Console.WriteLine("\nThe converted Celsius temperature is : " + celsius);
Console.ReadLine();
}
}
}`
I tried to different methods but it's seems like it doesn't work. I expect that you could help me and this program will work! thanks
You're printing the prompt to enter the temperature after reading the input for it with ReadLine.
You never assign the read value to anything.
Try this:
Console.Write("Enter Fahrenheit temperature : ");
var fahrenheit = Convert.ToDouble(Console.ReadLine());
var celsius = (fahrenheit - 32) * 5 / 9;

Clear console buffer between Console.ReadLine and Console.Read [duplicate]

This question already has an answer here:
Clear Console Buffer
(1 answer)
Closed 1 year ago.
I'm a beginner in C# so please be patient with me. :) I'm practicing to write a simple C# code to understand how this language works. I have tried to read characters, integers and strings from console but between different tries/methods the buffer never empties. I would like to clear the buffer of console between each methods, for example:
using System;
namespace ElsoKonzolAppom
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Write something!");
string text1 = Console.ReadLine();
Console.WriteLine("Your text:" + text1);
//here, I would like to clear the buffer
int number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your first number:" + number1);
//here, I would like to clear the buffer
int number2 = Console.Read();
Console.WriteLine("Your second number:" + number2);
}
}
}
Probably this is something very trivial that I haven’t mastered yet.
As far i understood you want to clear input buffer? There is no method in console class for it. Try this:
while(Console.KeyAvailable)
Console.ReadKey(false); // skips previous inputs
Console.ReadKey(); // reads a new char
Use Console.ReadKey(true) if you don't want to print skipped chars.
If you want to clear the screen use:
Console.Clear()
Regards
you can use Console.ReadKey(false); // true = hide input

Is there a function that swaps two variables in a string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So we have to have code that swaps the integers in a two-digit number, such as "43" being "34". The user inputs a random two digit number and that number must be swapped.
I am not sure how to separate or mess with the two digit number that the user inputs into the console, so I have not had much luck in doing this.
static void Main(string[] args)
{
Console.WriteLine("Please enter a two-digit integer");
string input = Console.ReadLine();
int number = Convert.ToInt32(input);
Console.ReadKey();
}
You can also just reverse the string before you parse it:
string input = string.Concat(Console.ReadLine().Reverse());
// If the user entered "34", 'input' will equal "43"
You can try modulo arithmetics:
number = number % 10 * 10 + number / 10;
you could do:
Console.WriteLine("Enter a No. to reverse");
int Number = int.Parse(Console.ReadLine());
int Reverse = 0;
while(Number>0)
{
int remainder = Number % 10;
Reverse = (Reverse * 10) + remainder;
Number = Number / 10;
}
Console.WriteLine("Reverse No. is {0}",Reverse);
Console.ReadLine();
this will give you 34 if you entered 43.
You can checkout this https://www.c-sharpcorner.com/blogs/reverse-a-number-and-string-in-c-sharp1 for more info.

C# Write a program that computes the average of five exam scores [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am doing my homework. English is not my first language, so I am confused.
This is the question: Write a program that computes the average of five exam scores. Declare and perform a compile-time initialization with the five exam values. Declare integer memory locations for the exam values. Use an integer constant to define the number of scores. Print all scores. The average value should be formatted with two digits to the right of the decimal. Rerun the application with different values. Be sure to desk check your results.
I am not sure what is "a compile-time initialization" mean? What is "Declare integer memory locations for the exam values." want me to do? What is "desk check" mean?
Here is my c# code:
using System;
using static System.Console;
namespace Chap2_1
{
class Chap2_1
{
static void Main()
{
int score1;
int score2;
int score3;
int score4;
int score5;
double average;
Console.Write("Please enter the 1st score: ");
score1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 2nd score: ");
score2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 3rd score: ");
score3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 4th score: ");
score4 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 5th score: ");
score5 = Convert.ToInt32(Console.ReadLine());
average = (score1+score2+score3+score4+score5) /5;
Console.Write("Average score is " + "{0:N2}", average);
Console.ReadKey();
}
}
}
I am not sure what is "a compile-time initialization" mean?
It means that your scores should have a value set in code from the start (hard coded), rather than values set by user input or "figured out" by the program after it's already running.
In other words, replace this:
Console.Write("Please enter the 1st score: ");
score1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 2nd score: ");
score2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 3rd score: ");
score3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 4th score: ");
score4 = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the 5th score: ");
score5 = Convert.ToInt32(Console.ReadLine());
With something like this:
//Replace the pointed numbers with whatever the scores should be.
// ||
// vv
score1 = 11;
score2 = 22;
score3 = 33;
score4 = 44;
score5 = 55;
What is "Declare integer memory locations for the exam values."
It means declaring the variables responsible for holding the scores so that you can average them. In other words, this part:
int score1;
int score2;
int score3;
int score4;
int score5;
What is "desk check" mean?
It means that you should average the scores with pen & paper, and make sure the result your program outputs is correct.
PS: I don't want to be rude, but this community is made for questions about code. If you don't understand the question, or english in general, you should ask your teacher.
We are here to help you with programming... not with translation or interpretation.
Shouldn't these be questions for your teacher? Your teacher will know your struggles and be able to help you far better than any of us simply because of his/her role in your studies.
That said, a compile-time initialization is something like:
int[] scores = new int[] { 100,80,90,64,72 };
or:
int score1 = 100;
int score2 = 80;
int score3 = 90;
int score4 = 64;
int score5 = 72;
As far as the memory locations go, I'd recommend reading Microsoft's C# programming guide here about it:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/unsafe-code-pointers/how-to-obtain-the-address-of-a-variable
Oh, and "desk check" means to do the same calculations manually with pen and paper to validate that you get the same results as your code.

Storing integers from Console to an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im writing a program that asks the console for x numbers from the console. If they pick the number 4, then 4 different numbers should be stored. Then the program must store all these inputed numbers in an array, and then add the numbers together and print it out in the console.
So, i tried to do:
Console.WriteLine("Write out a number: ");
int[] x = int[].Parse(Console.ReadLine());
and apparently you cant read in array elements from the console on that way, so do I need to store them inside an variabel and then add them to an new array?
Console.Writeline("Enter the number of numbers to add: ");
//Yes, I know you should actually do validation here
var numOfNumbersToAdd = int.Parse(Console.Readline());
int value;
int[] arrayValues = new int[numOfNumbersToAdd];
for(int i = 0; i < numOfNumbersToAdd; i++)
{
Console.Writeline("Please enter a value: ");
//Primed read
var isValidValue = int.TryParse(Console.Readline(), out value);
while(!isValidValue) //Loop until you get a value
{
Console.WriteLine("Invalid value, please try again: "); //Tell the user why they are entering a value again...
isValidValue = int.TryParse(Console.Readline(), out value);
}
arrayValues[i] = value; //store the value in the array
}
//I would much rather do this with LINQ and Lists, but the question asked for arrays.
var sum = 0;
foreach(var v in arrayValues)
{
sum += v;
}
Console.Writeline("Sum {0}", sum);

Categories