I've got myself in a real state with a simple assignment. I'm very very new to this and just when I thought I had got there of course I hadn't. I've been told that I need to allow the user to restart through every step in this ordering process. I found a way of using bool to allow the user to do this, but I'm thinking I must have messed up my loop because suddenly 'restart' isn't picked up any more. I know it will be something simple that I'm missing but can anyone help at all please?

Here's the code, apologies that I haven't neatened it up yet. I'm really really new to this and finding it very tough going so bear with me please and thanks in advance -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Pizza

{

class quality

{

static void Main(string[] args)

{

bool On;

On = true;

while (On == true)

{

Console.WriteLine("Welcome to Charlie's Pizza Pie Factory. Press enter to begin");

string readThisLine;

readThisLine = Console.ReadLine();

if (readThisLine == "quit")

{

Console.WriteLine("This Program is ready to be closed Press enter to Exit the program");

On = false;

break;

}

else if (readThisLine == "restart")

{

Console.Clear();

}

int catPizza = 0;

string[] cat_menu = { "Regular 1.50", "Best Brand 3.00", "Supreme 5.00" };

double[] qualprice = { 1.50, 3.00, 5.00 };

int diameter;

const double PI = 3.141592654;

double baseprice;

double pizzaprice;

DateTime date = DateTime.Now;

Console.WriteLine();

Console.WriteLine(date.ToString("r"));

Console.WriteLine("\n");

Console.WriteLine("Let's make some pizza. Here are your quality choices:");

catPizza = SelectFromMenu(cat_menu);

// Now read in the Pizza diameter - done by method checksize.

diameter = checksize(1200);

Console.WriteLine("Your pizza is {0}. Press enter for your pizza price (or 'restart' to change your order)", diameter + "mm");

Console.ReadLine();

// Now calculate the price of the pizza.

// baseprice = qualprice [catPizza-1];

// First convert the diameter in mm to a radius in metre. ie x 0.5 and divide by 1000.

// Then work out the area: Math.Pi*r*r where r=radius in metres.

// Then add 30 (price per sq metre) x area + price for the quality

baseprice = (diameter / 0.5) / 1000;//size is diameter in mm so divide by 2

// divide by 100 to get metreage

pizzaprice = ((PI * (baseprice * baseprice)) * 30) + qualprice[catPizza - 1];

Console.WriteLine("Your pizza costs {0:C}. The time of this order is " + (date), pizzaprice);

Console.WriteLine("Press enter to save a copy of this order");

Console.ReadLine();

{

string OutFn = @"C:\TEMP\pizzaProgram.txt"; // File name (string literal)

// Create StreamWriter for the output file - opens file.

StreamWriter OutF = new StreamWriter(OutFn, true);

// NB Parameter 2: True means append. False=Overwrite.

OutF.WriteLine("Pizza Order Total {0:C}," + (date), pizzaprice, OutFn);

OutF.Close(); // Close file, flush buffers, release memory.

Console.WriteLine("Output file written and closed.");

Console.ReadLine(); // Pause to see output

string OutDir = @"C:\TEMP\";

// Create the output directory if it doesn't exist.

if (!Directory.Exists(OutDir)) Directory.CreateDirectory(OutDir);

}

// int size = Console.ReadLine();

// if (checksize)

// return;

// Console.WriteLine("Thank you. You have ordered a {0}mm pizza", size);

//Console.ReadLine();

}

}

// Method WriteMenu: Writes menu and reads selection

// Validates it, returns the selection

static int SelectFromMenu(string[] menu)

{

int cat = 0;

for (int i = 0; i < menu.Length; i++)

Console.WriteLine("{0} {1}", i + 1, menu[i]);

do

{

Console.Write("Please enter the item number: (or 'restart' to cancel and restart): ");

try

{

cat = int.Parse(Console.ReadLine());

}

catch

{

cat = -1;

}

if ((cat > menu.Length) || (cat < 1)) Console.WriteLine("Try again");

} while ((cat < 0) || (cat > menu.Length));

Console.WriteLine("Thank you, you have selected {0} {1}.", cat, menu[cat - 1]);

return cat;

}

static int checksize(int max) // Method to read, validate and return an integer within range.120 to 1000

{ // It handles exceptions and also checks for a positive integer.

int size;

{

do

{

Console.WriteLine("Please enter your pizza size - anything between 120 and 1000mm (or 'restart' to change your order:)");

try

{

size = int.Parse(Console.ReadLine());

}

catch

{

size = 0;

}

if (size < 120 || size > max) Console.WriteLine("Okay, let's try again");

}

while (size < 120 || size > max);

return size;

} // end of ReadInt.

}

}

}

is it the select from menu part that you are stuck with?

Well, a couple of things:

1) After you've selected from the first choices, you go into the "checksize" function. Here, you state that you can enter 'restart' to restart the ordering process. Your function is expecting a number (diameter of the pizza) - but if the user enters 'restart', they just get to keep trying on entering a diameter.

2) On line 84, you execute a 'Console.ReadLine()" but you aren't capturing the line the user entered. If they did indeed enter 'restart', you will never know it.

You're most of the way there, just a bit of cleanup and you should be good.

Where should I put the 'restart'. I've moved things around but I think I might be back where I started again. Am I calling the diameter method in the wrong place? I'll keep trying and thanks

Suggestion - put each of your data gathering activities into its own method (you've already done that for one or two of them. Use a method signature like this:

bool SelectFromMenu(string[] menu, ref int iCat)

and call it like:

if (!SelectFromMenu(cat_menu, ref catPizza)))
{
    continue;
}

You pass an initialized version of "catPizza" into your method (say, -1). If the method returns false, the user entered "restart" - hence the "continue" statement - it will take you to the top of your "while" loop. Otherwise, the method will have set "catPizza" to the value the user selected, and you continue processing.

Set your other methods up similarly, and you have a mechanism to escape if the user wants to start over.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.