LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2015, 07:18 AM   #1
NoWeDoR
Member
 
Registered: Jun 2015
Posts: 32

Rep: Reputation: Disabled
do while loop problem


Code:
#include <stdio.h>

int main()
{
	int choice;
	do
	{
		printf("Enter your choice: ");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
		{
			printf("Hello World\n\n");
			break;

		}
		case 2:
		{
			printf("Hello World2\n\n");
			break;
		}
		case 3:
		{
			printf("Hello World3\n\n");
			break;
		}
		default:
		{
			return 0;
			break;
		}
		}
	} while (choice >= 1 && choice <= 3);
	return 0;
}
1) Like a this program, at first, when you enter unmeaning values (like * - / + . , etc.) as choice it is stopping as it should be.
2) However, at first when you enter as choice 1,2 or 3 and then enter unmeaning values (like * - / + . , etc.) it is starting to loop forever.

Although at first isn't a thing like this, Why is it happening at second?
 
Old 07-15-2015, 07:29 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Forget scanf, use fgets and sscanf. Don't ignore the return values.
 
1 members found this post helpful.
Old 07-28-2015, 03:29 PM   #3
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
First lap the entered data matches d so is sucessfully matched to choice. Second lap the scanf matches no data. This time %d doesn't effect choice, but the previous value is still available to switch statement

Try choice=0; before scanf or more precisely catch the value returned by scanf and act accordingly. In this case anything other than 1 implies some sort of error.

Last edited by rhubarbdog; 07-28-2015 at 03:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
[SOLVED] Using while loop & select statement - Loop issues Kustom42 Programming 4 05-17-2013 08:43 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration