1

This program will is giving me the error message:

use of unassigned variable yourName and use of unassigned variable yourIsp.

If I take the for loop out it will run. I am trying to ask user for name and isp at least 4 times and create email address from first and last name using string manipulation.

static void Main(string[] args)
{
    string yourName; string yourIsp;
    Console.WriteLine("Enter your full name:");
      for (int i = 0; i < 4; i++)            
    yourName = Console.ReadLine();

    Console.WriteLine("Enter your ISP:");
      for (int j = 0; j < 4; j++)
    yourIsp = Console.ReadLine();

    char[] separator = {' '};
    string[] yourWords;
    yourWords = yourName.Split(separator);
    string yourFirstName = yourWords[0];
    string yourLastName = yourWords[1];
    string yourEmailAddress = yourFirstName + yourLastName + "@" + yourIsp;
    yourEmailAddress = yourEmailAddress.ToLower();
    Console.WriteLine("Hello {0}, your email address is {1}", yourName, yourEmailAddress);
}   
puretppc
  • 3,232
  • 8
  • 38
  • 65
  • I don't understand what you think this is supposed to do. It asks for your full name, and then reads four lines of text, replacing `yourName` each time. So it will only "remember" the last line entered. Once you've entered four lines of text, it then does the same thing for ISP. – paddy Feb 03 '14 at 01:16

3 Answers3

0

You have to give yourName and yourIsp default values.

string yourName = "";
string yourIsp = "";

It doesn't know if your for loops are going to run for sure (even though you can tell that they'll each run 4 times).


Imagine if instead, your loop looked like this:

int number;

for (int i = 0; i < someList.Count(); i++)
    number = Math.Max(number, i.SomeValueInTheList);

Console.WriteLine("Largest value in list: {0}", number);

If there were no elements in the list, then the loop would never run, and number would not be initialized.


Store the values in a list, then display them one-by-one. Your overwriting values, and losing all but the last input value.

var names = new List<string>();
var isps = new List<string>();

string yourName; string yourIsp;
Console.WriteLine("Enter your full name:");
for (int i = 0; i < 4; i++)
    names.Add(Console.ReadLine());

Console.WriteLine("Enter your ISP:");
for (int j = 0; j < 4; j++)
    isps.Add(Console.ReadLine());

foreach (var sentence in names.Zip(isps, (name, isp) => string.Format("Hello {0}, your email address is {1}", name, string.Concat(name, "@", isp).ToLower())))
    Console.WriteLine(sentence);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Thanks. It did work and asked me for 4 names and isp. But for output it only gave me one email address. I mean, I input for names and isps: Jon Don, Ray Smith, Sam Dam, Rob Mob / hotmail.com, yahoo.com, gmail.com, live.com. It gave me output as Rob Mob email address is robmob@live.com. How do I get all four names to create email addresses. – user3583521 Feb 03 '14 at 01:29
  • 2
    @Ralvarez No problem. If any of the answers on StackOverflow help you, please upvote them and/or mark the most helpful as answer. It helps anyone in the future with the same problem find help quicker. – Grant Winney Feb 03 '14 at 03:09
0

Change string yourName; string yourIsp; to:

string yourName = null;
string yourIsp = null;

The problem is that the compiler cannot safely say if a value is ever assigned to the variables. Although it’s clear from the code that there will be a value assignment four times, it’s not something the compiler can say. Thus, you have to initialize the variables explicitely.

poke
  • 369,085
  • 72
  • 557
  • 602
0

The existing answers describe the error. My post addresses what I think you might have been trying to do:

for (int i = 0; i < 4; i++) {
    Console.WriteLine("Enter your full name:");
    string yourName =Console.ReadLine();

    Console.WriteLine("Enter your ISP:");
    string yourIsp = Console.ReadLine();

    char[] separator = {' '};
    string[] yourWords;
    yourWords = yourName.Split(separator);
    string yourFirstName = yourWords[0];
    string yourLastName = yourWords[1];
    string yourEmailAddress = yourFirstName + yourLastName + "@" + yourIsp;
    yourEmailAddress = yourEmailAddress.ToLower();
    Console.WriteLine("Hello {0}, your email address is {1}", yourName, yourEmailAddress);
}
paddy
  • 60,864
  • 6
  • 61
  • 103