C#, basic error - c#

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"):

Related

False value from User Input

I am trying to save the value i get through Console.Read() in an Integer, but whatever i type in my keyboard, the console always gives out 13. I tried to copy an example code, which definitly has to work, but im still only getting 13 as value.
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.Read();
int Test = Console.Read();
Console.WriteLine(Test);
}
}
}
After i typed in a number, it always shows '13' in the console.
Console.Read() reads first character from input stream.
In your case, You are trying to convert second character of input stream(i.e.after first Console.Read()) which is Carriage return and its ASCII value is 13, Test variable which is of type int, storing carriage return in integer format. i.e 13
If you want to convert first character from input stream, then try below
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int Test = Console.Read(); //Or you can use Console.ReadLine() to read entire line.
Console.WriteLine(Test);//Print first character of input stream
}
}
}
Here, you have to use Console.ReadLine to read the number. As Console.ReadLine returns string, you have to convert it to Int32. The following code will fix the issue
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int Test = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Test);
}
}
}
Try changing this to:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int Test = Console.ReadLine();
Console.WriteLine(Test);
}
}
}
Since 13 is key code for enter, you are probably getting that number from trying to read the whole line.

Identifier Expected c# for unknown reason

Trying to make a simple app which will ask a few questions.
But for some reason, my AskQuestion function doesn't work.
I plan on adding an easily swap able database later which is why I'm trying to take a slightly more modular approach and as I am a beginner I am unsure what I did wrong. The only errors are in line 21 for the AskQuestion class.
Errors are:
CS1001 Identifier Expected
CS1514 { expected
CS1513 } expected
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quiz
{
class Program
{
// Question Base
class Question
{
public String question = "Empty Question";
public String correctanswer = "Empty Answer";
public String givenanswer = "Empty Answer";
public String response = "Empty Response.";
public bool cleared = false;
}
// Ask Base
class void AskQuestion(Question Q)
{
while (Q.cleared == false)
{
Console.WriteLine(Q.question);
Q.givenanswer = Console.ReadLine();
Q.givenanswer.ToLower();
if (Q.givenanswer == Q.correctanswer)
{
Console.WriteLine(Q.response);
Q.cleared = true;
}
else
{
Console.WriteLine("Wrong. Try again.");
}
}
}
// Main Function
void Main(string[] args)
{
string Name;
Console.WriteLine("Welcome challenger! You're going to have a good time.");
Console.WriteLine("Make sure you use proper grammar. Or you may be stuck for no reason.");
Console.WriteLine("What is your name challenger?");
Name = Console.ReadLine();
Console.WriteLine("Welcome {0} to the challenge. I wish you best of luck. You will need it.",Name);
Question Q1 = new Question();
Q1.question = "What is the color of the sun?";
Q1.correctanswer = "White";
Q1.response = "Correct. Despite the fact it appears Yellow on earth, if you observe the sun from space, you would see it's true color. White.";
AskQuestion(Q1);
Q1.cleared = true;
Console.WriteLine("Nice little warmup. But, lets get a bit serious.");
}
}
}
change this
class void AskQuestion(Question Q)
to
void AskQuestion(Question Q)
This should be a method. The keyword class tells the compiler you want to create a inner class inside the out class Program
Q.givenanswer.ToLower(); doesn't make Q.givenanswer lowercase - it returns a new lowercase string which you need to assign to a variable, or just `Q.givenanswer = Q.givenanswer.ToLower();

A.Speak("Hello {0}, How are you today", name);

I need help for this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Synthesis;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SpeechSynthesizer A = new SpeechSynthesizer();
A.SelectVoiceByHints(VoiceGender.Neutral);
A.Speak("Hello, my name is Ezou. What's yours?");
Console.Write(">>>>>>");
var name = Console.ReadLine();
A.Speak("Hello " + name );
A.Speak("How are you today?");
A.Speak("Hello {0}, How are you today", name);
}
}
}
The problem is here :
A.Speak("Hello {0}, How are you today", name);
There is error on the Speak part
I don't see any overload of the Speak() method which accepts two strings. So you can't pass it two strings. It looks like you want to use string.Format() to create a single string, so do exactly that:
A.Speak(string.Format("Hello {0}, How are you today", name));
As I see it, you've got two options.
As David suggested in his answer, you could use:
A.Speak(string.Format("Hello {0}, how are you today?", name));
and another thing you could do is as follows:
A.Speak($"Hello {name}, how are you today?");
Hope this helps!
Since there is no overloaded Speak() method of SpeechSynthesizer class that accepts two parameters of type string, probably you can try like this:
A.Speak(String.Format("Hello {0}, How are you today", name));
Here the String.Format will give you the formatted string as input to the Speak()

C# syntax error, nothing seems wrong

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace SchoolPasswordLockFolder
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the password: "); // the space character after the semicolon has an error
public string input = Console.ReadLine();
}
}
}
The errors:
Severity Code Description Project File Line
Error CS1513 } expected SchoolPasswordLockFolder c:\Users\CENSOREDSIJGIOFSGJIOFS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 14
(for the one after the semicolon)
and
Severity Code Description Project File Line
Error CS1022 Type or namespace definition, or end-of-file expected SchoolPasswordLockFolder c:\Users\CENSOREDIDONTWANTSTALKERS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 19
(for the last bracket)
I have not programmed in C# for a very long time as I was too busy with web development and lua...
change this:
public string input = Console.ReadLine();
to:
string input = Console.ReadLine();
local variables do not get accessibility modifiers like public.

Use method to Return value int in C#

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 :-) )

Categories

Resources