While loop and user input help!

Check for user input before continue the loop

Page 1 of 1

11 Replies - 7208 Views - Last Post: 22 September 2009 - 01:20 AM Rate Topic: -----

#1 jamesl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-September 09

While loop and user input help!

Post icon  Posted 09 September 2009 - 08:26 AM

Hi
First off this is my first time posting a problem so sorry for anything i have not displayed correctly. Secondly i am sort of a beginner in C programming.
I'm having an issue with a program i am writing. Heres what i'd like to do, i'd like to run a print statement, then ask the user if they want to continue, then proceed with another print statement and so forth until i reach the end of my statements i'd like to produce to the reader then it will return from the method.
eg.

1. print statement
2. ask user if they wish to continue (if so continue, if not return from method)
3. another print statement
4. ask user if they wish to continue (if so continue, if not return from method)
5. another print statement
6. return

The only way i think i can get this to work is if i use a while loop and this is my code

int continueLooping = 1;
char input;

void chordProgressions(int continueLooping, char input) {

  printf("Chord progressions are a sequence of chords when played in sequence, sound nice and (most of the time) resolve to the I or V\n");
  
  while (continueLooping = 1) {
  
	printf("The first chord progression is \nI, IV, V, I\n This chord progression is very popular in all genres of music and was first popularized in blues\n");
		
	continueLooping = continueLoop(input); //This is where i want to verify if the user wishes to continue
   
	//Continues with more print statements and user verification afterward
  
  }



and my verification method

int continueLoop(char input) {

   printf("Continue?(y/n): \n");
   scanf("%c", &input);

   if (input = 'y') {
  
	return 1;
   }
   if (input = 'n') {
	
	return 0;
   }
   else {
   
   printf("input invalid!");

   }
   return;
}



The main problem is that the while loops runs twice and i dont get to input if i wish to continue.
Output looks like this, and my input just keeps running the loop from there on without exiting.
[url="http://img529.imageshack.us/i/outputerror.jpg/"]http://img529.imageshack.us/i/outputerror.jpg/[/url]

Sorry if i am unclear :(
-Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: While loop and user input help!

#2 viveks89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 06-January 09

Re: While loop and user input help!

Posted 09 September 2009 - 08:58 AM

The fault here is with the checking statement in while...
while(continueLooping = 1) // This assignes but not checks the condition



instead use condition staement this way
while(continueLooping == 1) // This checks for condtion


Was This Post Helpful? 0
  • +
  • -

#3 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: While loop and user input help!

Posted 09 September 2009 - 08:58 AM

1st, welcome to D.I.C. :)
2nd, in your check you have this problem

   if (input = 'y') {
 
	return 1;
   }
   if (input = 'n') {
   
	return 0;
   }



input = 'n' and input = 'y' is assigning the value of 'n' and 'y' to input. You should use logical operation of == which will return yes or no. Also what vivek said.

This post has been edited by Mowgef: 09 September 2009 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 jamesl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-September 09

Re: While loop and user input help!

Posted 09 September 2009 - 09:24 AM

View PostMowgef, on 9 Sep, 2009 - 07:58 AM, said:

1st, welcome to D.I.C. :)
2nd, in your check you have this problem

   if (input = 'y') {
 
	return 1;
   }
   if (input = 'n') {
   
	return 0;
   }



input = 'n' and input = 'y' is assigning the value of 'n' and 'y' to input. You should use logical operation of == which will return yes or no. Also what vivek said.


Thank you all for your answers! I forgot about == i thought that was entirely a java thing thanks!
Was This Post Helpful? 0
  • +
  • -

#5 jamesl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-September 09

Re: While loop and user input help!

Posted 10 September 2009 - 02:25 AM

Ok so i've changed = to == but the loop continues to run without stopping for the user to input. Maybe its just not possible in a while loop? One thing i've noticed is that the loop runs twice and not infinitely. Also when i do input the 'y' or 'n' the loop just proceeds to run twice again. :\
Was This Post Helpful? 0
  • +
  • -

#6 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: While loop and user input help!

Posted 10 September 2009 - 02:56 AM

Um, shouldn't you put "else" in front of if(input == 'n') ?
Was This Post Helpful? 0
  • +
  • -

#7 jamesl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-September 09

Re: While loop and user input help!

Posted 10 September 2009 - 03:16 AM

Yes i should thanks. Also i think i found out the problem. I suppose it was detecting enter as an input character so i should have use this %c%*c instead of just %c?
Was This Post Helpful? 0
  • +
  • -

#8 krisku  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 65
  • Joined: 13-December 08

Re: While loop and user input help!

Posted 10 September 2009 - 03:16 AM

Hmm, why you use scanf() to read a character from input stream? Try with functions like: getch(), getchar()...

About problem:
while(continueLooping != 0) // This checks for condtion

???

This post has been edited by krisku: 10 September 2009 - 03:19 AM

Was This Post Helpful? 0
  • +
  • -

#9 jamesl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-September 09

Re: While loop and user input help!

Posted 10 September 2009 - 05:01 AM

New to C and programming sorry
Was This Post Helpful? 0
  • +
  • -

#10 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: While loop and user input help!

Posted 10 September 2009 - 05:46 AM

Also, you can improve this part of your code...
if (input == 'y' || input == 'Y') {
 
	return 1;
   }
   else if (input == 'n' || input == 'N') {
   
	return 0;
   }



Now, even if the user enters y or n in capital letter, the function will still return their respective values.
Was This Post Helpful? 0
  • +
  • -

#11 jamesl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-September 09

Re: While loop and user input help!

Posted 10 September 2009 - 06:52 AM

Awesome thanks.
Was This Post Helpful? 0
  • +
  • -

#12 april198474  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-September 09

Re: While loop and user input help!

Posted 22 September 2009 - 01:20 AM

After the "While" and "if", you can not use "=" to compare,you should use "==".bescase input = 'no' is always true ,so you can only loop twice and connot continue.

And I feel

if (input = 'y') {

return 1;
}
if (input = 'n') {

return 0;
}
else {

printf("input invalid!");

}
can be

if (input = 'y') {

return 1;
}
else if (input = 'n') {

return 0;
}
else {

printf("input invalid!");

}
it will be more effective.

__________________________

April
I am a scummy, sneaky spammer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1