Thread: Confused: while loop and characters

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Confused: while loop and characters

    I'm working on a project for school that prints 4 triangles after taking a size input and character from the user. I want to make a while loop that outputs an error message if the user inputs a value less than 1 or a character when they are asked what size they want the triangles to be. I was able to set up a loop that continues to prompt the user for an integer value if they enter something less than 1(shown here); but, if a character is entered the program goes into an infinite loop. Can anyone point me in the right direction on how to fix my while loop so that it doesn't go into a infinite loop if a character is entered?

    Code:
    #include <stdio.h>
    #include<ctype.h>
    void flush(void)
    {
    while (getchar()!='\n');
    }
    int main(void)
    {
    int size, i, j, valid_input;
    char c, answer;
    do
    {
    Printf(“enter triangle size \n”);
    Scanf(“%i” ,&size);
    while(size <= 0){
        printf("size must be greater than 0 \n");
        printf("\n");
        printf("enter triangle size \n");
        scanf("%i" ,&size);
        }
        flush();
    Thanks in advance and sorry if my question isn't clear...been working on this problem all day and I am feeling a little

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    How are you getting out of the "do" loop beginning on line 11?
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Quote Originally Posted by TheBigH View Post
    How are you getting out of the "do" loop beginning on line 11?
    Sorry, should have mentioned that isn't all the code just the part containing the loop I was having trouble with so that "do" loop is taking care of at the end of the program.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest using fgets and sscanf.
    NOTE: C is a case sensitive language; you have multiple mistakes in your code caused by smart quotes I suggest NEVER writing code in MS Word or related software.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop that detects too many characters
    By breakboye in forum C Programming
    Replies: 3
    Last Post: 01-24-2012, 05:15 PM
  2. Replies: 10
    Last Post: 12-16-2008, 10:28 AM
  3. While loop with multiple characters
    By arctic_blizzard in forum C Programming
    Replies: 16
    Last Post: 09-20-2008, 12:25 PM
  4. confused by Do While loop
    By elton_fan in forum C Programming
    Replies: 1
    Last Post: 11-01-2007, 10:25 AM
  5. For Loop question for printing characters
    By trprince in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 09:01 PM