Thread: char related problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Dhaka, Bangladesh, Bangladesh
    Posts
    4

    char related problem

    i made an odd loop. when i press 'y' , i want to continue the loop but it does not. any one can give me a solution?
    program is given below..

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {int n;
    char i='y';
    if(i=='y')
    {printf("Enter a number : ");
    scanf("%d",&n);
    printf("\nSquare of %d is %d",n,n*n);
    printf("\nWould you like to enter another  number? (y/n)");
    scanf("%c",&i);
    }
    getch();
    clrscr();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. If you want your program to loop then, well, you need to use a loop. "if" isn't a loop, it's just a branch point; there's no return. Your loops are called "for", "while", and "do while".
    2. Once you get that in, there is the fact that at the end of the block, the value of i must needs be '\n' (unless you were thinking way ahead and typed "4y" at the prompt). To make %c skip enter-key and read a "normal" character, you need to use a space: " %c".

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You didn't make a loop - an if() statement is not a loop.

    while ()
    do while()
    Are loops, which test at the start and end of the loop respectively.


    You should also have

    scanf(" %c",&i);Note the initial space in this format string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Additionally, void main is wrong. The minimum C program is:
    Code:
    int main(void){
    
         return (0);
    }
    Read about How to define main-FAQ

    Based on you using void main, getch, and conio.h I am going to assume you are also using Turbo C. It is time to get a new compiler. There are plenty of standard compilers out there that are free.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    ^ What these guys said. And indent your code, it's much easier to read with proper indentation.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer related problem
    By shashinitjsr in forum C Programming
    Replies: 2
    Last Post: 06-25-2011, 02:37 AM
  2. Widget problem [GTK RELATED]
    By darekg11 in forum C Programming
    Replies: 5
    Last Post: 05-09-2011, 12:25 PM
  3. A problem related to DB and C using SQLAPI++
    By redhunter in forum C Programming
    Replies: 0
    Last Post: 05-12-2004, 10:14 AM
  4. date-time related problem
    By Ruchikar in forum C Programming
    Replies: 1
    Last Post: 11-18-2002, 11:17 PM
  5. Class related problem
    By Gnoober in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2002, 10:11 PM

Tags for this Thread