String vector with user input [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 6 years ago.
Improve this question
Stuck for hours on this task. I really appreciate help, it is not shown in the literature how this is to be done. I can create the vector but after that I'm totally blocked.
Create a string vector with five elements. The user will then enter 5 names via a for loop. The program then writes these names via another for loop.
Please type out the code so I can understand. Without solving this first task I can't do the other ones.
Here's my futile attempt so far
int [] namn = new int [5];
for (int i = 0; i < 5; i++);
{
Console.Write("Ange fem namn");
string str = Console.ReadLine();
int names = Convert.ToInt32(str);
}

It seems that the main (and the most difficult error to find) is ; after for loop
for (int i = 0; i < 5; i++);
this loop does nothing five times.
//DONE: "string[]" - Create a STRING vector with five elements...
string [] namn = new string [5];
//DONE: namn.Length - no magic numbers (5)
for (int i = 0; i < namn.Length; i++) // !!! no ";" !!!
{
Console.Write("Ange fem namn");
//DONE: put into array, not to a local variable
namn[i] = Console.ReadLine();
}
// Print out the names
foreach(var item in namn)
Console.WriteLine(item);

Related

Master mind in console [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to make some sort of mastermind game in the console where the computer generates a 3 digit code and you have to guess it. The computers also tells you which digit you get right after you take a guess. How would i best do this? I don't know a way to "chop up" input so that i can make the computer check it against the digit it has.
example:
computer generates random code: 123
you guess: 321
computer : -*- (the star indicates which digit you got right)
I have an idea about how i'd indicate what digits are right or not, but i don't know how to separate the input into parts
Convert the computer generated random number into a string. The user input is given as string anyway.
The string type has an indexer allowing you to access single characters
string s = "abc";
char a = s[0];
char b = s[1];
char c = s[2];
for (int i = 0; i < s.Length; i++) {
Console.WriteLine($"s[{i}] = '{s[i]}'");
}
You write char literals as
char ch = 'x';
using single quotes.
Example:
string userInput = Console.ReadLine();
int code = SomehowGenerateRandomCode();
string codeString = code.ToString();
// Let's assume that both numbers have 3 digits.
for (int i = 0; i < 3; i++) {
if(userInput[i] == codeString[i]) {
// digits are equal
} else {
// digits are different
}
}
To split an int to its digits you can convert to string an split like:
int i = 123;
string myInt = i.ToString();
List<int> digits =new List<int>();
foreach (char c in myInt)
{
digits.Add(int.Parse(c.ToString());
}

Check if 2 strings contain same letters at same index [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 6 years ago.
Improve this question
string text1 = "abcde";
string text2 = "fgchi";
I want to check if the 2 strings have the same characters at the same index and if they do then let the place where they are the same be printed.
for (int i = 0; i < text1.Length; i++)
if (text1[i] == text2[i])
Console.WriteLine("Character {0} at index {1}", text1[i], i);
Considering your strings have the same length.
Edit: if I should not give answers to trivial tasks such as this and instead encourage the user to find it by himself, then please point out that to me. I'm new around. [I suppose it's obvious, so i'm just not gonna do it, and adjust]
Maybe something like the following code helps. And it shouldn´t be important how long each string is with that. Maybe the string.Format isn´t needed.
private string charMatch(string str_a, string str_b)
{
int char_a = str_a.Count();
int char_b = str_b.Count();
int runs = 0;
StringBuilder sb = new StringBuilder();
if (char_a <= char_b) { runs = char_a; }
else { runs = char_b; }
for (int i = 0; i < runs; i++)
{
if (str_a[i] == str_b[i])
{
sb.Append(string.Format("Match found at {0} \n", i));
}
}
return sb.ToString();
}

Creating a string using StringBuilder in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am in the process of converting a VB application. I am trying to create a string using StringBuilder as I cannot change a string using the Mid function. I have a loop although it only adds the first character to the string the rest is just white space. How can i add all the data to the string?
The code snippet is below; thanks for your help
int table = 0;
string tableData = Strings.StrDup(253, " ");
int i = 0;
string listData = null;
int pointer =0;
StringBuilder sb = new StringBuilder(tableData);
sb.Insert(0, Strings.StrDup(253, Strings.Chr(32)));
sb.Insert(0, typeOfTable + Strings.Chr(0));
pointer = 2;
for (i = 0; i <= lstTABLE.Items.Count -1; i++)
{
listData = lstTABLE.Items[i].ToString();
table = Convert.ToInt32(-(Conversion.Val(listData) * 10));
sb.Insert(pointer, Functions.FNCodeTwoChar(table));
pointer +=2;
}
sb.Insert (202,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblStartTable.Text))));
sb.Insert(205,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblEndTable.Text))));
sb.Insert(208, Strings.Space(36));
sb.Insert(244, " 0");
sb.Insert(248, " 0");
The answer to the question of "I have a loop although it only adds the first character to the string the rest is just white space. How can i add all the data to the string?" is:
The second character you add to the string is Strings.Chr(0) which is the string terminator character. When C# (or VB.Net for that matter) hit this character it stops reading the string.

For loop working in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am writing a program that finds the number of 1's in an array.. but the "scanning" inside the "for" loop takes place only once.
for (i = 0; i < 11; i++ )
{
k = Convert.ToInt32(Console.Read());
if (k==1)
{
ans++;
}
// Console.WriteLine("i == {0}", i);
}
Is this normal in C# or am I doing something wrong? I tried to search for this problem but cannot find any answers!
Do
Console.ReadLine()
instead of
Console.Read()
When you reach the first Console.Read() the inputstream is saved but only the first character is returned by the Read method. Subsequent calls to the Read method retrieve your input one character at a time from the inputstream.
Ex:
First iteration:
k = Console.Read(); //you input "abc1", k = a
Second iteration:
k = Console.Read(); // k = b
and so on.
When the last character in the inputstream is returned, the next call to Console.Read() will display the console again so you can input a new string and hit Enter.
Console.Read() docu
There's no array in your code, actually. Personally I'd rewrite this to:
string input = Console.ReadLine();
for (i = 0; i < input.Length; i++ )
{
int k = Convert.ToInt32(input.Substring(i, 1));
if (k==1)
{
ans++;
}
}
Which lets the user enter a string first and then does all the parsing. Of course, the user may enter more or less than exactly 11 characters in this code, so if you really need 11 numbers, I'd add extra messages and checks.
Also this code (as your original code) fail miserably if the user enters something non-numeric, so exception handling would be on the task list as well.
Try to use this code, I think it will answer your problem
string[] sample = { "1", "1", "3" };
var a = (from w in sample
where w == "1"
select w).ToList();
Console.Write(a.Count);

How do you loop Readline statements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a homework assignment and I am stuck on a certain part of the question.
How do I loop numbers regarding the readline statement.
In other words, suppose I have two numbers that I want to input. Instead of typing out the readline statement twice, how do I get a loop that would allow me to type the readline statement once?
have two numbers that I want to input. Instead of typing out the readline statement twice, how do I get a loop that would allow me to type the readline statement once
Well, if you want two numbers, then you're going to need two variables (or an array), so I assume you're trying to change this:
int firstNumber;
int secondNumber;
firstNumber = int.Parse(Console.ReadLine());
secondNumber = int.Parse(Console.ReadLine());
into something like this:
int[] myNumbers = new int[2];
for(int i = 0; i < 2; i++)
{
myNumbers[i] = int.Parse(Console.ReadLine());
}
but think about this:
Will the prompt be the same for both numbers? Or will the prompt change between inputs
Is it easier to assess the variables independently versus a part of the array/list?
Will having the numbers in a structure benefit other parts of my app (looping to get a sum, etc.)
I think a loop as fine so long as you don't end up with logic inside the lop that changes depending on which number you're using:
int[] myNumbers = new int[2];
for(int i = 0; i < 2; i++)
{
if i == 0 // bad
string prompt = "Enter the first number";
else
string prompt = "Enter the second number";
Console.WriteLine(prompt);
myNumbers[i] = int.Parse(Console.ReadLine());
}
int[] myInputs = new int[2];
for(int i=0; i < 2; ++i)
{
myInputs[i] = Int32.Parse(Console.ReadLine());
}
After this, myInputs[0] is the first value, and myInputs[1] is the second value.

Categories