Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to figure out how to do a do while loop for my project in my c# class. We have not covered any of the loops yet, but he asked us to try. I have no idea what I am doing wrong. I am wanting it to ask the user if they would like to continue if they answer no then the program should terminate.

C#
string stateAbbrev;

            Console.WriteLine("Enter the state abbreviation. ");
            Console.WriteLine("Its full name will" + " be displayed");
            Console.WriteLine();
            stateAbbrev = Console.ReadLine();
            stateAbbrev = stateAbbrev.ToUpper();    //Converts the characters to uppercase

            switch (stateAbbrev)
            {

                case "AL": Console.WriteLine("Alabama");
                    break;

                case "AK": Console.WriteLine("Alaska");
                    break;

                case "AZ": Console.WriteLine("Arizona");
                    break;

                case "AR": Console.WriteLine("Arkansas");
                    break;

                case "CA": Console.WriteLine("California");
                    break;

                case "CO": Console.WriteLine("Colorado");
                    break;

                case "CT": Console.WriteLine("Connecticut");
                    break;

                case "DE": Console.WriteLine("Delaware");
                    break;

                case "FL": Console.WriteLine("Florida");
                    break;

                case "GA": Console.WriteLine("Georgia");
                    break;

                case "HI": Console.WriteLine("Hawaii");
                    break;

                case "ID": Console.WriteLine("Idaho");
                    break;

                case "IL": Console.WriteLine("Illinois");
                    break;

                case "IN": Console.WriteLine("Indiana");
                    break;

                case "IA": Console.WriteLine("Iowa");
                    break;

                case "KS": Console.WriteLine("Kansas");
                    break;

                case "KY": Console.WriteLine("Kentucky");
                    break;

                case "LA": Console.WriteLine("Louisiana");
                    break;

                case "ME": Console.WriteLine("Maine");
                    break;

                case "MD": Console.WriteLine("Maryland");
                    break;

                case "MA": Console.WriteLine("Massachusetts");
                    break;

                case "MI": Console.WriteLine("Michigan");
                    break;

                case "MN": Console.WriteLine("Minnesota");
                    break;

                case "MS": Console.WriteLine("Mississippi");
                    break;

                case "MO": Console.WriteLine("Missouri");
                    break;

                case "MT": Console.WriteLine("Montana");
                    break;

                case "NE": Console.WriteLine("Nebraska");
                    break;

                case "NV": Console.WriteLine("Nevada");
                    break;

                case "NH": Console.WriteLine("New Hampshire");
                    break;

                case "NJ": Console.WriteLine("New Jersey");
                    break;

                case "NM": Console.WriteLine("New Mexico");
                    break;

                case "NY": Console.WriteLine("New York");
                    break;

                case "NC": Console.WriteLine("North Carolina");
                    break;

                case "ND": Console.WriteLine("North Dakota");
                    break;

                case "OH": Console.WriteLine("Ohio");
                    break;

                case "OK": Console.WriteLine("Oklahoma");
                    break;

                case "OR": Console.WriteLine("Oregon");
                    break;

                case "PA": Console.WriteLine("Pennsylvania");
                    break;

                case "RI": Console.WriteLine("Rhode Island");
                    break;

                case "TN": Console.WriteLine("Tennessee");
                    break;

                case "TX": Console.WriteLine("Texas");
                    break;

                case "UT": Console.WriteLine("Utah");
                    break;

                case "VT": Console.WriteLine("Vermont");
                    break;

                case "VA": Console.WriteLine("Virginia");
                    break;

                case "WA": Console.WriteLine("Washington");
                    break;

                case "WV": Console.WriteLine("West Virginia");
                    break;

                case "WI": Console.WriteLine("Wisconsin");
                    break;

                case "WY": Console.WriteLine("Wyoming");
                    break;

                default: Console.WriteLine("No match");
                    break;


                    do
                    {
                        Console.WriteLine("Would you like to try again? ", answer);
                        counter = Convert.ToString(Console.ReadLine);
                    }
                    while (anwer > yes);


            }
            Console.WriteLine("\n");
            Console.WriteLine("\n");
            Console.Write("press -1 to Exit:");
            Int32.Parse(Console.ReadLine());
        }
    }
}
Posted
Updated 24-May-10 3:31am
v2

sloshedmr_bones wrote:
do
{
Console.WriteLine("Would you like to try again? ", answer);
counter = Convert.ToString(Console.ReadLine);
}
while (anwer > yes);


The braces define your loop. The while goes back to where it says do. Notice that the rest of your code, the stuff you want to run to 'try again' is outside the do/while loop and so will not be run. Also note that > is not an acceptable test for strings. != is the test for if answer does not equal yes. If yes is a constant, not a variable, it needs to be in quotes. If you want a do while loop to ever terminate, it needs to be checking a variable that is changed within the loop ( in this case, counter, not answer ). Console.ReadLine is a method, it needs () after it. It returns a string. Convert.ToString is not needed.

This code should not even compile, you should learn to read error messages and diagnose them, and post them here along with the line that generates them, if you want us to fix them for you.
 
Share this answer
 
Comments
[no name] 23-May-10 15:47pm    
string answer;

do
{
Console.Write("Would you like to try again? ", answer);
answer = Console.ReadLine();

}
while (answer != "yes");

now im getting a caution that the code is unreachable. I still haven't got an aswer from anyone on anything here. Something would be nice. I have moved the loop everywhere and it asks the question but it asks the question and then displays the answer when you answer the question.
You need to have basically all your code inside the loop. Something like this:

C#
string stateAbbrev;
            string answer;

            do
            {
                Console.WriteLine("Enter the state abbreviation. ");
                Console.WriteLine("Its full name will" + " be displayed");
                Console.WriteLine();

                stateAbbrev = Console.ReadLine();

                stateAbbrev = stateAbbrev.ToUpper();    //Converts the characters to uppercase

                switch (stateAbbrev)
                {
                    case "AL": Console.WriteLine("Alabama");
                        break;
                    case "AK": Console.WriteLine("Alaska");
                        break;
                    case "AZ": Console.WriteLine("Arizona");
                        break;
                    case "AR": Console.WriteLine("Arkansas");
                        break;
                    case "CA": Console.WriteLine("California");
                        break;
                    case "CO": Console.WriteLine("Colorado");
                        break;
                    case "CT": Console.WriteLine("Connecticut");
                        break;
                    case "DE": Console.WriteLine("Delaware");
                        break;
                    case "FL": Console.WriteLine("Florida");
                        break;
                    case "GA": Console.WriteLine("Georgia");
                        break;
                    case "HI": Console.WriteLine("Hawaii");
                        break;
                    case "ID": Console.WriteLine("Idaho");
                        break;
                    case "IL": Console.WriteLine("Illinois");
                        break;
                    case "IN": Console.WriteLine("Indiana");
                        break;
                    case "IA": Console.WriteLine("Iowa");
                        break;
                    case "KS": Console.WriteLine("Kansas");
                        break;
                    case "KY": Console.WriteLine("Kentucky");
                        break;
                    case "LA": Console.WriteLine("Louisiana");
                        break;
                    case "ME": Console.WriteLine("Maine");
                        break;
                    case "MD": Console.WriteLine("Maryland");
                        break;
                    case "MA": Console.WriteLine("Massachusetts");
                        break;
                    case "MI": Console.WriteLine("Michigan");
                        break;
                    case "MN": Console.WriteLine("Minnesota");
                        break;
                    case "MS": Console.WriteLine("Mississippi");
                        break;
                    case "MO": Console.WriteLine("Missouri");
                        break;
                    case "MT": Console.WriteLine("Montana");
                        break;
                    case "NE": Console.WriteLine("Nebraska");
                        break;
                    case "NV": Console.WriteLine("Nevada");
                        break;
                    case "NH": Console.WriteLine("New Hampshire");
                        break;
                    case "NJ": Console.WriteLine("New Jersey");
                        break;
                    case "NM": Console.WriteLine("New Mexico");
                        break;
                    case "NY": Console.WriteLine("New York");
                        break;
                    case "NC": Console.WriteLine("North Carolina");
                        break;
                    case "ND": Console.WriteLine("North Dakota");
                        break;
                    case "OH": Console.WriteLine("Ohio");
                        break;
                    case "OK": Console.WriteLine("Oklahoma");
                        break;
                    case "OR": Console.WriteLine("Oregon");
                        break;
                    case "PA": Console.WriteLine("Pennsylvania");
                        break;
                    case "RI": Console.WriteLine("Rhode Island");
                        break;
                    case "TN": Console.WriteLine("Tennessee");
                        break;
                    case "TX": Console.WriteLine("Texas");
                        break;
                    case "UT": Console.WriteLine("Utah");
                        break;
                    case "VT": Console.WriteLine("Vermont");
                        break;
                    case "VA": Console.WriteLine("Virginia");
                        break;
                    case "WA": Console.WriteLine("Washington");
                        break;
                    case "WV": Console.WriteLine("West Virginia");
                        break;
                    case "WI": Console.WriteLine("Wisconsin");
                        break;
                    case "WY": Console.WriteLine("Wyoming");
                        break;
                    default: Console.WriteLine("No match");
                        break;
                }

                Console.WriteLine();
                Console.WriteLine("Would you like to try again (yes/no)?");

                answer = Console.ReadLine();

                Console.WriteLine();
            }
            while (answer.ToUpper() == "YES");
 
Share this answer
 
ok if u sure u need to run porgram at least one time , u need do while
do
{statement
}while(condition)
 
Share this answer
 
Comments
William Winner 28-May-10 17:25pm    
Reason for my vote of 2
I'm sorry..how was this supposed to answer the question?
All of these answers look good. they are all right.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900