I am a new C# Programmer

E

Eric Anderson

Hi Guys,

I had posted awhile back that I am a new C# programmer and I have just completed my first program. It's pretty simple, but I'm having a slight problem with it. The program that I made rearranges the order of a persons name after they input it into the console. But for some reason the order of rearrangement that I set does not work the way that I want it to work. If you guys can solve my problem and tell me what I did wrong or did not do, it will be greatly appreciated. Here is my code:

using System;

namespace Name_Rearrange

{

/// <summary>

/// Name Rearranger

/// Program rearranges a persons name

/// Eric Anderson, 11.29.05

/// </summary>

class rearrange

{

static void Main(string[] args)

{

string firstname;

string middlename;

string lastname;

Console.WriteLine("Welcome to the Name Rearranger");

// Add 1 line of space here

Console.WriteLine();



Console.Write("What is your first name? ");

firstname = Console.ReadLine();



Console.Write("What is your middle name? ");

middlename = Console.ReadLine();



Console.Write("What is your last name? ");

lastname = Console.ReadLine();



// Add 1 line of space here

Console.WriteLine();

// Show rearranged name here

Console.WriteLine("{0}, {1}, {2}",firstname,middlename,lastname);

Console.WriteLine("{2}, {0}, {1}",lastname,firstname,middlename);

Console.WriteLine("{2}, {1}, {0}", lastname, middlename, firstname);


// Add 1 line of space here

Console.WriteLine();

// Ask to press enter to end

Console.WriteLine("Thank you for playing");

Console.Write("Please press the enter key to exit!");

Console.ReadLine();

}// End Main

}// End Class

}// End Namespace
 
T

Tom Dacon

Eric, you need to tell us what you want it to do.

Tom Dacon
Dacon Software Consulting

Hi Guys,

I had posted awhile back that I am a new C# programmer and I have just
completed my first program. It's pretty simple, but I'm having a slight
problem with it. The program that I made rearranges the order of a persons
name after they input it into the console. But for some reason the order of
rearrangement that I set does not work the way that I want it to work. If
you guys can solve my problem and tell me what I did wrong or did not do, it
will be greatly appreciated. Here is my code:

using System;

namespace Name_Rearrange

{

/// <summary>

/// Name Rearranger

/// Program rearranges a persons name

/// Eric Anderson, 11.29.05

/// </summary>

class rearrange

{

static void Main(string[] args)

{

string firstname;

string middlename;

string lastname;

Console.WriteLine("Welcome to the Name Rearranger");

// Add 1 line of space here

Console.WriteLine();



Console.Write("What is your first name? ");

firstname = Console.ReadLine();



Console.Write("What is your middle name? ");

middlename = Console.ReadLine();



Console.Write("What is your last name? ");

lastname = Console.ReadLine();



// Add 1 line of space here

Console.WriteLine();

// Show rearranged name here

Console.WriteLine("{0}, {1},
{2}",firstname,middlename,lastname);

Console.WriteLine("{2}, {0},
{1}",lastname,firstname,middlename);

Console.WriteLine("{2}, {1}, {0}", lastname, middlename,
firstname);


// Add 1 line of space here

Console.WriteLine();

// Ask to press enter to end

Console.WriteLine("Thank you for playing");

Console.Write("Please press the enter key to exit!");

Console.ReadLine();

}// End Main

}// End Class

}// End Namespace
 
S

SP

Hi Guys,

I had posted awhile back that I am a new C# programmer and I have just completed my first program. It's pretty simple, but I'm having a slight problem with it. The program that I made rearranges the order of a persons name after they input it into the console. But for some reason the order of rearrangement that I set does not work the way that I want it to work. If you guys can solve my problem and tell me what I did wrong or did not do, it will be greatly appreciated. Here is my code:

Console.WriteLine("{0}, {1}, {2}",firstname,middlename,lastname);

Console.WriteLine("{2}, {0}, {1}",lastname,firstname,middlename);

Console.WriteLine("{2}, {1}, {0}", lastname, middlename, firstname);


You should be changing only the order of the {0}, {1}, {2} and leave firstname,middlename,lastname the same.

SP
 
B

Brian Pelton

I think you might want this:

// Show rearranged name here
Console.WriteLine("{0}, {1}, {2}", firstname, middlename, lastname);
Console.WriteLine("{0}, {1}, {2}", lastname, firstname, middlename);
Console.WriteLine("{0}, {1}, {2}", lastname, middlename, firstname);


The {0} refers to the first argument. So when it prints, it will
replace {0} with the value of the first argument.

So, if you have Console.WriteLine("{1}", a, b); then the value of b
will be printed, and a will be ignored!

Another way to do your rearranging would be:

Console.WriteLine("{0}, {1}, {2}", firstname, middlename, lastname);
Console.WriteLine("{2}, {0}, {1}", firstname, middlename, lastname);
Console.WriteLine("{2}, {1}, {0}", firstname, middlename, lastname);

That is the same as the first method.
 
C

chris martin

Console.WriteLine("{0}, {1},
{2}",firstname,middlename,lastname);

Console.WriteLine("{2}, {0},
{1}",lastname,firstname,middlename);

Console.WriteLine("{2}, {1}, {0}", lastname, middlename,
firstname);

The numbers in the brackets tells the compiler which argument in WriteLine
should be placed in that position. So, you should not change the ordering
of {0}, {1}, {2}. Just change the ordering of your arguments like this.

Console.WriteLine("{0}, {1}, {2}", firstname, middlename, lastname);

Console.WriteLine("{0}, {1}, {2}", lastname, firstname, middlename);

Console.WriteLine("{0}, {1}, {2}", lastname, middlename, firstname)
 
E

Eric Anderson

Thanks you guys!

--
Eric Anderson
Hi Guys,

I had posted awhile back that I am a new C# programmer and I have just completed my first program. It's pretty simple, but I'm having a slight problem with it. The program that I made rearranges the order of a persons name after they input it into the console. But for some reason the order of rearrangement that I set does not work the way that I want it to work. If you guys can solve my problem and tell me what I did wrong or did not do, it will be greatly appreciated. Here is my code:

using System;

namespace Name_Rearrange

{

/// <summary>

/// Name Rearranger

/// Program rearranges a persons name

/// Eric Anderson, 11.29.05

/// </summary>

class rearrange

{

static void Main(string[] args)

{

string firstname;

string middlename;

string lastname;

Console.WriteLine("Welcome to the Name Rearranger");

// Add 1 line of space here

Console.WriteLine();



Console.Write("What is your first name? ");

firstname = Console.ReadLine();



Console.Write("What is your middle name? ");

middlename = Console.ReadLine();



Console.Write("What is your last name? ");

lastname = Console.ReadLine();



// Add 1 line of space here

Console.WriteLine();

// Show rearranged name here

Console.WriteLine("{0}, {1}, {2}",firstname,middlename,lastname);

Console.WriteLine("{2}, {0}, {1}",lastname,firstname,middlename);

Console.WriteLine("{2}, {1}, {0}", lastname, middlename, firstname);


// Add 1 line of space here

Console.WriteLine();

// Ask to press enter to end

Console.WriteLine("Thank you for playing");

Console.Write("Please press the enter key to exit!");

Console.ReadLine();

}// End Main

}// End Class

}// End Namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top