using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string userName = "";
int userAge = 0;
int currentYear = 0;
Console.Write("Please enter your name: ");
userName = Console.ReadLine();
Console.Write("Please enter your age: ");
userAge = Convert.ToInt32(Console.ReadLine());
Console.Write("Please enter the current year: ");
currentYear = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Hello World, my name is {0} and I'm {1} years old, I was born on the year {2} .", userName, userAge, currentYear - userAge);
}
}
}
It is missing the last Console.WriteLine for some reason, after I enter the currentYear, it closes the application without displaying the string to the console. Im just now starting to learn c# so also any other sources to help me learn are also welcome, thanks! (btw im using visual studio)
You need to add Console.Read or Console.ReadLine at the bottom of your code
Console.ReadLine();
Or else the console will close because the blocks of codes has already been executed.
Side Note : Possible Duplicate of this Question
Why is the console window closing immediately without displaying my output?
Related
I'm new to coding. I am trying to improve myself by making simple coding. I just encountered such error while writing a code to retrieve data from user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FuelCalc
{
internal class Program
{
static void Main(string[] args)
{
//readline
Console.WriteLine("Lütfen size hitap edebilmem için adınız ve soyadınızı giriniz...");
string namesurn= Console.ReadLine();
Console.WriteLine("Merhaba " + namesurn +" lütfen aracınızın ortalama yakıt tüketimini giriniz...");
int yakit = Console.ReadLine();
Console.ReadLine();
}
}
}
What you could try is adding a loop to repeat until the user enters a number. The int.TryParse method will return true if the string returned from Console.ReadLine() can be interpreted as an int and you will have a value for yakit in that case. Otherwise, ask the user to try again.
(I'm relying on Google Translate here leave me a comment if I did something wrong!)
static void Main(string[] args)
{
//readline
Console.WriteLine("Lütfen size hitap edebilmem için adınız ve soyadınızı giriniz...");
string namesurn = Console.ReadLine();
Console.WriteLine("Merhaba " + namesurn + " lütfen aracınızın ortalama yakıt tüketimini giriniz...");
// Loop until a valid int is received.
while(true)
{
if(int.TryParse(Console.ReadLine(), out int yakit))
{
Console.WriteLine($"{yakit} alınan");
break;
}
else
{
Console.WriteLine("Numara gerekli");
}
}
Console.ReadLine();
}
You may want to consider float or double instead of int in case the user enters a decimal point character like "40.9" in which case you could substitute:
float.TryParse(Console.ReadLine(), out float yakit)
This question already has answers here:
c# loop until Console.ReadLine = 'y' or 'n'
(2 answers)
Closed 4 years ago.
So I tryed to make a simple text game.And I wanted to do Yes/No dialong I was trying to figure this out for 2 hours, but i didn't find any solution to my problem. I'm total beginner, so I don't know much about coding.
I was trying to achive it trough "bool" bot it wasnt the right option. Obviousli.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TEXTRPG
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Greetings, stranger.");
Console.ReadKey();
Console.WriteLine("I've came here to give you a quest.");
Console.ReadKey();
Console.WriteLine("Do you accept?");
//Yes/No
Console.ReadKey();
Here is my sample
do {
Console.Write("Do you accept? [y/n] ");
response = Console.ReadKey(false).Key;
if (response != ConsoleKey.Enter)
Console.WriteLine();
} while (response != ConsoleKey.Y && response != ConsoleKey.N);
Hello im trying to learn C# step by step. I installed Visual Studio to practice but 20 mins in I cant test my basic code when executing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("My name is " + name);
}
}
}
It is as basic as this but when I execute and type a name and press enter, the cmd just closes. any help would be appreciated because i am enthusiastic to start out with C#
cmd
The Main method returns after
Console.WriteLine("My name is " + name);
And this effectively terminates the app.
You should put a
Console.Read();
to wait until the next keystroke.
Add another input to close application, so cmd wont close until you press enter again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Write("What is your name?");
string name = Console.ReadLine();
Console.WriteLine("My name is " + name);
Console.ReadLine();
}
}
}
Try Ctrl + F5
If you don't need to debug, then Ctrl-F5 is the best option
works automatically without any Console.Readline() or ReadKey()
Visual Studio will keep the console window open, until you press a key.
So I am making a super basic (I'm in my second week of learning C# so please excuse my ignorance) program that takes a string input from a user and outputs the string backwards. I have copied the book to a T in regards to a majority of it but I have noticed spelling errors in some of their code so I don't have a lot of faith in what they are showing. My compiler is giving me an error with WriteLine and ReadLine and I don't understand why as the book says it works. This is the error I am getting;
"WriteLine does not exist in the current context" same with "ReadLine"
My code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
static class funcStrings
{
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
class runProgram
{
class Program
{
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
}
}
}
Thanks for any guidance here
You just need to add Console. in front:
Console.WriteLine("Enter your name to be reversed ");
name = Console.ReadLine();
You can add such namespace:
using static System.Console;
This brings all the static members from the System.Console class into scope, so that you don't need to prefix them with Console. It is a C# 6 feature, and useful when accessing many members in a static class. See relevant documentation.
The problem is probably that it should be Console.WriteLine and Console.ReadLine.
Currently it is:
static void Main()
{
string name;
WriteLine("Enter your name to be reversed ");
name = ReadLine();
Console.WriteLine(funcStrings.ReverseString(name));
}
You need to add a Console.WriteLine("Enter your name to be reversed"):
I'm taking a programming class in c# and it is our first week and I'm trying to work ahead a little. To that end, I am trying to rework one of our class labs and am stuck with my 'GetInt' method.
Ideally, the GetInt method (line 50) takes in a string (Enter a number) and returns the number as an int so i can then use it to do some math. Currently line #24 turns the test error 'Cannot implicity convert type 'int' to 'string'.
Any help is appreciated. I am very much a newb, so please don't assume i know anything.
Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lec022_If_statement_int
{
class Program
{
//Set to public so it is visible
//void because it returns nothing
//Play is a method within the class Program I've added
public void Play()
{
DisplayStr("Lecture 2c | If Statements with ints");
DisplayReturns();
DisplayStr("Welcome to Dunut King");
DisplayReturns();
//Collect User Name
//GetString converts to lower, trims
String numDonuts = GetInt("How many donuts would you like?: ");
DisplayReturns();
//Display welcome
Console.WriteLine("You asked for " + numDonuts + " donuts.");
DisplayReturns();
DisplayReturns();
DisplayStr("Have a great Day!");
}
//MaxBox 2.0
public void DisplayStr(String StrTxt)
{ Console.Write(StrTxt); }
public void DisplayReturns()
{ Console.Write("\n\n"); }
public string GetString(String StrVar)//note - using strings here
{
Console.Write(StrVar);
return Console.ReadLine().ToLower().Trim();
}
public int GetInt(string intVar)//note - using ints here
{
Console.Write(intVar);
return int.Parse(Console.ReadLine());
}
//Initiate Program
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Play();
Console.Read();
}
}
}
Your GetInt method returns int. So you need to change from
String numDonuts = GetInt("How many donuts would you like?: ");
to
int numDonuts = GetInt("How many donuts would you like?: ");
The problem is that the method GetInt returns int.
And you're trying to assign its return value to this String variable on line 24.
String numDonuts
You cannot do this.
Change the first word on line #24 from 'String' to 'int' because I am asking for an int not a string.
Your problem is there:
String numDonuts = GetInt("How many donuts would you like?: ");
You try to set int value to string variable.
change to
var numDonuts = GetInt("How many donuts would you like?: ");
Your GetInt method returns integer type so you should use an integer type to store it, not a string.
Here is the fixed code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lec022_If_statement_int
{
class Program
{
//Set to public so it is visible
//void because it returns nothing
//Play is a method within the class Program I've added
public void Play()
{
DisplayStr("Lecture 2c | If Statements with ints");
DisplayReturns();
DisplayStr("Welcome to Dunut King");
DisplayReturns();
//Collect User Name
//GetString converts to lower, trims
int numDonuts = GetInt("How many donuts would you like?: ");
DisplayReturns();
//Display welcome
Console.WriteLine("You asked for " + numDonuts + " donuts.");
DisplayReturns();
DisplayReturns();
DisplayStr("Have a great Day!");
}
//MaxBox 2.0
public void DisplayStr(String StrTxt)
{ Console.Write(StrTxt); }
public void DisplayReturns()
{ Console.Write("\n\n"); }
public string GetString(String StrVar)//note - using strings here
{
Console.Write(StrVar);
return Console.ReadLine().ToLower().Trim();
}
public int GetInt(string intVar)//note - using ints here
{
Console.Write(intVar);
return int.Parse(Console.ReadLine());
}
//Initiate Program
static void Main(string[] args)
{
Program myProgram = new Program();
myProgram.Play();
Console.Read();
}
}
}
I've just tested the program. You enter a number and you get a response. Works nicely. (For it's simple purpose :-) )