Jump to content

a minor problem

- - - - -

  • Please log in to reply
7 replies to this topic

#1
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
#include<stdio.h>
main(){
char another;
int num;
do
{
printf("Enter a number ");
scanf("%d",&num);
printf("Square of %d is %d",num,num*num);
printf("\nWant to another number");
scanf("%c",&another);
}while(another=='y');

}
after printing the third printf the program ends abruptly and doesn't wait to take any input. why?

Edited by csepraveenkumar, 08 August 2010 - 04:47 AM.


#2
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
First of all, your code isn't indented. Indenting everything between brackets {} can really help with code readability in the future.
Also, main() should be declared to return an int, and should return 0 on successful operation.
A third thing to note is that scanf() is a really bad function to use for input, but I expect you'll learn the proper way to do input later.

But you still don't know what's wrong with your code. Well, the answer is simple. After you input a number, you press enter. Enter sends a newline to your program '\n'. Since scanf("%d") only reads a number, scanf("%c") reads the newline character. '\n' does not equal 'y', so your program quits out. Probably the simplest way to solve your problem is to add a getchar() call that reads the newline, so scanf("%c") can read user input properly.

Fixed code:
#include <stdio.h>


int main() {

    char another;

    int num;

    do

    {

        printf("Enter a number ");

        scanf("%d",&num);

        printf("Square of %d is %d\n",num,num*num);

        printf("Want to input another number? ");

        getchar();

        scanf("%c",&another);

    } while(another=='y');

    return 0;

}

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#3
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
no, that doesn't help.
after getchar the program again quits unexpectedly.

#4
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
a little help?

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You aren't fully validating the input on your scanf. A \n is probably failing the test for equality with 'y' and you drop out.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
May I ask which IDE are you using?
The one I use have no problem with either code (of course, different IDE treats things differently).

#include <stdio.h>

int main() {

    char another;
     int num;
    do
    {
        printf("Enter a number ");
        scanf("%d",&num);
        printf("Square of %d is %d\n",num,num*num);
          
        printf("Want to input another number? ");
          another = getchar();
    } while(getchar()=='y');
    return 0;
}


or even just this...
#include <stdio.h>

int main() {
     int num;
    do
    {
        printf("Enter a number ");
        scanf("%d",&num);
        printf("Square of %d is %d\n",num,num*num);
          
        printf("Want to input another number? ");
          getchar();
    } while(getchar()=='y');
    return 0;
}


#7
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
@jwxie518: your code works fine in my ide also. i am using eclipse in ubuntu lynx. also i've tried the code in terminal window.
what's wrong with my code.

#8
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
the problem is solved. though i am still confused about the fact that \n also has to be read separately. why isn't \n flushed after the input has been used?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users