user3361821
user3361821

Reputation: 35

Validating User Input and updating a variable inside a loop

I'm trying to have a user enter a file path as a string (but for learning purposes I'd like to treat the path as any string variable, but I am open to suggestions for file path validation). The issue I am having and cannot seem to figure out is once inFilepath is updated from Console.ReadLine(), it only will show up in the Console.WriteLine() statement that concatenates it. Trying to display it anywhere else displays "", which I know indicates that the variable was not updated since its assignment as "". I believe this is a scope issue, but I am at a loss as to why, in the end I just need to access the updated inFilepath in the same scope as its declaration.

string correctPath = "no";
string inFilepath = "";
bool flag = false;
bool inFlag = false;
while (!flag) {
    Console.WriteLine("Please Enter the Tour file path now...");
    inFilepath = Console.ReadLine();
    inFilepath += "\\";
    while (!inFlag) {
        Console.WriteLine("Is this the correct filepath: " + inFilepath + "  ?" +
        "\n Enter 'no' if it is incorrect or 'yes' if it is correct.");
        correctPath = Console.ReadLine();
        if (correctPath == "yes") {
            inFlag = true;
            flag = true;
        }
        else {
            break;
        }
    }
}
Console.WriteLine("path: ", inFilepath);

Upvotes: 0

Views: 146

Answers (1)

Jason
Jason

Reputation: 3960

Your problem is here, you are passing the param arg, but not using/displaying it

Console.WriteLine("path: ", inFilepath);

You need to add {0}, like

Console.WriteLine("path: {0}", inFilepath);

Upvotes: 1

Related Questions