• 0

[C#] I feel like an idiot.


Question

I want to drown in novocaine :cry:

I'm taking this course to learn C#, and hopefully pass the exams to get my MCPD cert'.

So my experience.. or rather lack of it, is substantial - I'm really at the basic stuff right now.

For some reason I can't for the life of me understand how to use (or manipulate) a (stored) variable.

For example; I declare a variable to hold an Integer/String (doesn't matter), call it whatever..

I want to set it by using ReadLine(), so in other words by using my keyboard.

Now, until now it's really simple to do for me, but when I use a loop to repeat the process several times - I want to be able to use everything I typed in.

This is where I fail, when I print out the variable that is used to receive my input, it just prints out the LAST word/number I put in, but I want to be able to take out everything I put in so far and then reformat it the way I want to, like add tabs between the words, for example.

I'm sorry if it sounds stupid, but I gotta start somewhere, and I'm really stuck on it.

I appreciate the help !

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

You have to append the values you want to add to the string.

string variable = "Hello";

variable = variable + " World!";

Otherwise you just keep on overwriting the values.

Link to comment
Share on other sites

  • 0

Thanks Kristian, mate.

But I don't think that it helped me in the way I wanted..

I need more booze

Ninja edit:

I appreciate the help guys, but I know about concatenation, it's not the issue here (I think).

I'd like to explain another scenario;

I have a string variable assigned to ReadLine(), in a loop that goes for.. say 3 times.

I enter 3 words.

I want to print out each word in a new line.

How do I go about doing that ? If I put += then it will just add all three words together and create one big word that I can't format.

I can obviously create 3 different string variables and do a ReadLine() to each, but that defeats the purpose of learning!

Basically it's the loop that I'm ****ed at, as it's natural for the loop to do itself over and over again (well under my conditions), it just overwrites itself, and if I do += inside a loop it will keep overwriting itself.

Edited by Inclined
Link to comment
Share on other sites

  • 0

Kristian is right, you're probably setting the variable each time, not concatenating the string.

	class Program
	{
		static void Main(string[] args)
		{
			string data = string.Empty;
			Console.WriteLine("Type some words.  When you are done type \"exit\"\n\n");
			string input = Console.ReadLine();
			while (!input.Equals("exit", StringComparison.CurrentCultureIgnoreCase))
			{
				if (data.Length > 0) data += " ";
				data += input;

				input = Console.ReadLine();
			}

			Console.WriteLine(data);
			Console.WriteLine("\n\nPress any key to continue...");
			Console.ReadKey();
		}
	}

Consider using a StringBuilder instead.

Link to comment
Share on other sites

  • 0

Thanks Antaris, that does help.

The only problem is that we didn't even learn most of what you wrote in your code

Is there a really simple way of coding it ?

Note that I'm not looking for suckers to do my "homework", I just didn't learn these things yet and I don't want to use whatever we didn't yet covered, feels like cheating.

Hints are a good way to help :)

Like I said, and I mean it, I appreciate the help, guys.

Link to comment
Share on other sites

  • 0

The way I see your problem : you want the user to enter not just one string, but a list of strings. You need to store these strings in memory to be able to use them afterwards, for instance to print them to the console.

Start by declaring, not a string, but a list of string.

List<string> listOfStrings = new List<string>();

Now get the user input :

string input = Console.ReadLine();

And loop until the user enters some terminating string, like an empty string or "exit" or whatever.

while (input.Length != 0) { // breaks when the user enters an empty string
	listOfStrings.Add(input);
	input = Console.ReadLine();
}

Each time you go through the loop, you add the new input to the list. When the loop breaks, all valuable input (all strings except the breaking string) have been added to the list.

Now you can do whatever you want with that list of strings. For instance, print each string on a line :

foreach (string line in listOfStrings) {
	Console.WriteLine(line);
}

Here's the full listing :

using System;
using System.Collections.Generic;

namespace NeowinHelp {
	class Program {
		static void Main(string[] args) {
			List<string> listOfStrings = new List<string>();
			string input = Console.ReadLine();
			while (input.Length != 0) { // breaks when the user enters an empty string
				listOfStrings.Add(input);
				input = Console.ReadLine();
			}
			foreach (string line in listOfStrings) {
				Console.WriteLine(line);
			}
		}
	}
}

Link to comment
Share on other sites

  • 0

I'd like to thank all of you who helped out :)

I managed to do it without using the stuff you mentioned though, simply because we didn't even learn about it.

But you certainly did help me out, a lot.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.