d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C# Return To Beginning Of Loop Prematurely ?
12Next
Add Reply New Topic New Poll
Member
Posts: 11,183
Joined: Aug 23 2008
Gold: 1,890.00
Mar 6 2014 10:06am
I am trying to make a program where you are inside of a loop. If you input something wrong, it will give you an error message and ignore the rest of the loop and start over from the beginning.. a lot like break; is used in a switch function to skip all the other switch cases. I tried google but I don't think I'm phrasing it right. it keeps showing the continue; function which just continues with the next thing..

thanks.

an example of what I'm doing: (just a snippet, no class/method/variable declaring).


Code
for (k=1;; k++) {
Console.Write("Enter your first name: ");
fName = Console.ReadLine();
if (fName == "") {
// This is where you display error and the code sends you back to the beginning of the loop.
}
Console.Write("Enter your last name: ");
lName = Console.ReadLine();
Console.WriteLine(fName + " " + lName);
}


Please bear in mind that I am still at an intro level of C#.

Thanks. :D
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Mar 6 2014 10:22am
I'm not quite sure what you are trying to do yet. If you hit the error code, are you trying to skip the rest of the stuff? If so, you can just put continue; inside the if statement, and that will reset to the beginning of the loop

or if you want you can also do

Quote
for (k=1;; k++) {
Console.Write("Enter your first name: ");
fName = Console.ReadLine();
if (fName == "") {
// This is where you display error and the code sends you back to the beginning of the loop.
}
else{
Console.Write("Enter your last name: ");
lName = Console.ReadLine();
Console.WriteLine(fName + " " + lName);
}
}


This post was edited by oOn on Mar 6 2014 10:25am
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 6 2014 10:35am
use continue statement

Code
for (k=1;; k++) {
Console.Write("Enter your first name: ");
fName = Console.ReadLine();
if (fName == "") {
continue;
// This is where you display error and the code sends you back to the beginning of the loop.
}
Console.Write("Enter your last name: ");
lName = Console.ReadLine();
Console.WriteLine(fName + " " + lName);
}
Member
Posts: 11,183
Joined: Aug 23 2008
Gold: 1,890.00
Mar 6 2014 03:44pm
Thanks :D

finally fixed it:

Code

using System;

class startup {
static string name;
static string title;
static string author;
static string isbn;
public static void Main(string[] args) {
int k;
int error = 0;
for (k=1;; k++) { // This program is meant to be manually exited.
Console.WriteLine("Welcome! Please enter the following information to check out a book.");
ErrorMessages test = new ErrorMessages();
Console.Write("Enter your name: ");
name = Console.ReadLine();
error = test.EmptyInput(name);
if (error == 1) {
Console.Clear();
continue;
}
Console.Write("Enter the book's title: ");
title = Console.ReadLine();
error = test.EmptyInput(title);
if (error == 1) {
Console.Clear();
continue;
}
Console.Write("Enter the book's author: ");
author = Console.ReadLine();
error = test.EmptyInput(author);
if (error == 1) {
Console.Clear();
continue;
}
Console.Write("Enter the book's ISBN: ");
isbn = Console.ReadLine();
error = test.EmptyInput(isbn);
if (error == 1) {
Console.Clear();
continue;
}
Book update = new Book();
update.Display(name, title, author, isbn);
Console.Clear();
}
}
}


Code

using System.Windows.Forms;

class ErrorMessages {
public string testStringValue;
public int error;

public int EmptyInput(string testStringValue) {
if (testStringValue == "") {
error = 1;
MessageBox.Show("You have entered an invalid input", "Error - Invalid Input",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
error = 0;
}
return error;
}
}


Code

using System;
using System.Windows.Forms;

class Book {
public void Display(string name, string title, string author, string isbn){
MessageBox.Show("Name: " +name +"\n"+
"Title: " +title +"\n" +
"Author: " +author +"\n" +
"ISBN: " +isbn, "Book Info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}


This post was edited by kasey21 on Mar 6 2014 03:47pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 6 2014 04:10pm
Why are you using a for loop.

Just use a while(true) loop instead seeing how you are not even using the k integer value.

Edit this entire code is a mess. Since you are not asking the user to re-input a wronged variable you can simply do this:

Code
using System;

class startup
{
static string name;
static string title;
static string author;
static string isbn;
public static void Main(string[] args)
{
int k;
int errName = 0, errTitle = 0, errAuthor = 0, errISBN = 0;
while(true)
{
Console.WriteLine("Welcome! Please enter the following information to check out a book.");
ErrorMessages test = new ErrorMessages();

Console.Write("Enter your name: ");
name = Console.ReadLine();
errName = test.EmptyInput(name);

Console.Write("Enter the book's title: ");
title = Console.ReadLine();
errTitle = test.EmptyInput(title);

Console.Write("Enter the book's author: ");
author = Console.ReadLine();
errAuthor = test.EmptyInput(author);

Console.Write("Enter the book's ISBN: ");
isbn = Console.ReadLine();
errISBN = test.EmptyInput(isbn);

if(errName || errTitle || errAuthot || errISBN) {
Console.Clear();
continue;
}

Book update = new Book();
update.Display(name, title, author, isbn);
Console.Clear();
}
}
}



The only difference is that it will restart the loop after you entered in all your credentials.

I suggest you add a while loop for each input so that it will keep asking for input until it is correct:

Code
do {
name = Console.ReadLine();
} while(test.EmptyInput(name));


or something of the like.

This post was edited by AbDuCt on Mar 6 2014 04:23pm
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Mar 6 2014 04:18pm
Quote (AbDuCt @ Mar 6 2014 10:10pm)
Why are you using a for loop.

Just use a while(true) loop instead seeing how you are not even using the k integer value.



I remember in my intro class we were restricted to only use what we had "currently learned" and weren't able to use any code that we havent been taught yet..I'm guessing its a similar case
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 6 2014 04:21pm
Quote (oOn @ Mar 6 2014 06:18pm)
I remember in my intro class we were restricted to only use what we had "currently learned" and weren't able to use any code that we havent been taught yet..I'm guessing its a similar case


I'm sure they would of taught their students the usages of loops for various conditions all at the same time. If they taught them to hack apart a for loop like that I'd leave their school.

This post was edited by AbDuCt on Mar 6 2014 04:23pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 6 2014 06:06pm
Quote (oOn @ Mar 6 2014 05:18pm)
I remember in my intro class we were restricted to only use what we had "currently learned" and weren't able to use any code that we havent been taught yet..I'm guessing its a similar case


While loops are usually taught first.
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Mar 6 2014 10:21pm
Oddly enough our intro to java course taught for loops first

http://www.cs.utexas.edu/~scottm/cs312/schedule.htm

This post was edited by oOn on Mar 6 2014 10:22pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Mar 6 2014 10:43pm
Quote (oOn @ Mar 6 2014 11:21pm)
Oddly enough our intro to java course taught for loops first

http://www.cs.utexas.edu/~scottm/cs312/schedule.htm


Seems very backwards to me. Iterative loops are just special types of conditional loops. Logical flow of learning would be from conditional loops, to conditional loops that iterate over a collection, to iterative loops

Code
int i = 0, n = 5;
while(i < n)
{
do_stuff();
i++;
}


becomes

Code

for(int i = 0, n = 5; i < n; i++)
{
do_stuff();
}



While leads to more in depth discussion on when and where you use each loop, why a while is better vs a for loop or vice versa.

I would be curious to know your professor's answer as to why he feels for loops should be taught first. Seems like you are moving backwards in that case, doesn't it?
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll