-3
    string text;
    string alltext; //what do I do with this?
    do
    {
        text = Console.ReadLine();
    } while (text!="x");
    Console.WriteLine(text);
    Console.ReadKey();

What did I do wrong? Someone on Reddit said that I need another variable to hold all the texts in, but I have no idea how to do it.

lapartman
  • 45
  • 1
  • 7
  • 1
    do you know how to concatenate strings ? – Mong Zhu Oct 23 '17 at 14:31
  • Read this: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-assignment-operator – hatchet - done with SOverflow Oct 23 '17 at 14:33
  • problem is non of these stop on pressing x, they all need a line with x on its own and pressing enter – BugFinder Oct 23 '17 at 14:34
  • better question is does he know how to declare a variable..? this is not that difficult.. store all the vales into a List and when someone enters the letter To UpperCase `X` , then uses `Console,WriteLine(string.Join(" ", "Your List")` varible... – MethodMan Oct 23 '17 at 14:35

3 Answers3

2

I believe you're new to programming, so I'll try to explain what you want to do.

You want to store everything you type into a variable until you press X (or so I've understood).

The variable 'key' will store whatever you type once a cycle (can be anything) - meaning, key will always have a volatile value, because you are changing it everytime it reaches the readline. You append the variable 'key' to your other variable, 'alltext', that is where you have it's previous value + the new value 'key'. The cycle only ends when you enter 'x' and only 'x'. After that, it will print everything in 'alltext'.

There are many ways to do this, and working with an array is a better and cleaner way, however I'm just adapting what you wrote in a way that you can easily understand.

string key = "";
string alltext = "";
do
{
    key = Console.ReadLine();
    alltext += key + "\n ";
} while (key!="x");
Console.WriteLine(alltext);
Console.ReadKey();

What I'm doing with "\n" is that I'm adding a new line after that text (let the console know its a new line).

Let me know if this helps

abr
  • 2,071
  • 22
  • 38
1

Think about what you are trying to do: create a string, and keep adding lines to it, until the user hits 'x'.

Now look at what you are doing: creating a string and then creating that string again with every new line the user types.

Work out what syntax you need to append (add) to the string. Hint: look up concatenate.

mcalex
  • 6,628
  • 5
  • 50
  • 80
0
    string text="";
    string alltext=""; //what do I do with this?
    while (text!="x" || text!="X")
    {
        text = Console.ReadLine();
        alltext+=text;
    } 
    Console.WriteLine(alltext);
    Console.ReadKey();

You mean this ?

Ngine
  • 65
  • 8