Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Terminating a while loop with the letter q

    Im playing around with C and I am trying to exit a while loop with the letter q but i cant seem to get it to work.

    Code:
    /*
    Program: daystoweeks.c
    Author: Danny Stones
    Date: 11/10/2011
    */
    
    #include <stdio.h>
    
    int main(){
    int day;
    int week;
    int days;
    
    printf("Please enter the number of days: ");
    scanf("%d", &day);
    
    while (day!='q')
    {
    	week = day/7;
    	days = day % 7;
    	printf("%d weeks and %d days", week, days);
    	printf("\nPlease enter the number of days: ");
    	scanf("%d", &day);
    }                                                                                                                    
    return 0;
    }
    Thanks
    Danny

  • #2
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    You are trying to compare an integer, day, to character, q.

    I'm surprised this doesn't compile or blow up on you...
    Last edited by bobleny; 10-11-2011 at 05:42 PM.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    Omaha, NE
    Posts
    49
    Thanks
    1
    Thanked 6 Times in 5 Posts
    You can only compare integers with integers, doubles with doubles, and strings with strings... otherwise the while loop can never be matched up. Except maybe doubles and integers... but there are special rules with that.
    Web Design Omaha Ne
    Hold yourself responsible for a higher standard than anybody expects of you. Never excuse yourself.
    - Henry Ward Beecher

  • #4
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok that makes sense thanks for the replys so how can I solve the problem? I am still clueless on the matter

  • #5
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    258
    Thanks
    3
    Thanked 11 Times in 11 Posts
    You have a few options that I can see...

    You could make 'q' a number, such as -1.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int day;
    	int week;
    	int days;
    
    	do
    	{
    		printf("Please enter the number of days: ");
    		scanf("%d", &day);
    
    		week = day/7;
    		days = day % 7;
    		printf("%d weeks and %d days", week, days);
    
    	}while(day != -1);
    
    	return 0;
    }
    You could make day a string and parse the string for a 'q'. If a 'q' is not found, then check each character in the string for a character between '0' and '9'. If they are characters between '0' and '9', then convert them into an int.

    You could also create a main prompt like this:
    Code:
    What would you like to do?
    0.) Calculate days
    1.) Exit
    Enter a number:
    If they select 0, prompt them for the number of days. If they select 1, exit.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •