Problem with gets() function

View previous topic View next topic Go down

Problem with gets() function

Post by BIT0322-Hira on Sat Sep 10, 2011 4:54 am

I observed that when I use a scanf() then I can't take any input by using gets() function.Why? I don't know.Please help me.An example is in following code.Here I can't take any input by using gets()..
Code:

#include<stdio.h>

#include<string.h>
int main()
{
 
 char ch;
  char string [256];
 printf("Enter a character:");
 scanf("%c",&ch);
  printf ("Insert your full address: ");
  gets (string);
  printf ("Your address is: %s\n",string);
  return 0;
}

BIT0322-Hira
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 24
Points : 33

View user profile

Back to top Go down

Re: Problem with gets() function

Post by BIT0122-Amit on Sat Sep 10, 2011 6:45 am

ভালা জিনিষ জিগাইসো। জিনিষটা বুঝতে হলে flush, buffer নিয়ে একটু জানা লাগবে।
জিনিষটা হলো, scanf দিয়ে তুমি যখন ইনপুট নিচ্ছো, তখন একটা enter দিচ্ছো।
আচ্ছা, সেই enter দিলে কি হয়? একটা "\n", তাই না?
ধরো ইনপুট দিলে, এভাবে-

Enter a Character:
Hira
এবং এর পরে ঠাস করে এন্টার। scanf করবে কি, সেই এন্টার এর আগে যা ছিল সব নিয়ে এর পরে বার করবে প্রথমে কি ছিল, সেটা পাঠায়ে দিবে ch এর কাছে। কিন্তু এন্টার বেচারাকে তো scanf ধরে নাই। তার কি হবে?
সেই এন্টার তখন গিয়ে পড়বে gets এর ঘাড়ে। আর gets তো ইনপুট নিতে চায় অনেক, তাকে থামাতে হলে একটা এন্টার দরকার। scanf এর enter পেলেই সে খুশি মনে ফুটে।

কাজেই সমাধান?

এই লাইনটা ঢুকায়ে দাও scanf আর gets এর মাঝে
Code:
fflush(stdin);
তাহলে সেই লাফানো এন্টারটাকে ফ্লাস করে ভাগানো হবে, আর gets এর ঘাড়ে উল্টা পাল্টা চড়তে পারবে না।

কাজ হলে জানাবে, আর না বুঝলেও জানাবে। Smile

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Problem with gets() function

Post by BIT0322-Hira on Sat Sep 10, 2011 1:27 pm

It is not working.

BIT0322-Hira
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 24
Points : 33

View user profile

Back to top Go down

Re: Problem with gets() function

Post by BIT0122-Amit on Sat Sep 10, 2011 2:12 pm

Show me where you added the snippet, i.e. the full code.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Problem with gets() function

Post by BIT0322-Hira on Sat Sep 10, 2011 3:01 pm

Code:

#include<stdio.h>

#include<string.h>
int main()
{
 
 char ch;
  char string [256];
 printf("Enter a character:");
 scanf("%c",&ch);
fflush(stdin);
  printf ("Insert your full address: ");
  gets (string);
  printf ("Your address is: %s\n",string);
  return 0;
}

BIT0322-Hira
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 24
Points : 33

View user profile

Back to top Go down

Re: Problem with gets() function

Post by BIT0122-Amit on Sat Sep 10, 2011 3:10 pm

BIT0322-Hira wrote:
Code:

#include<stdio.h>

#include<string.h>
int main()
{
 
 char ch;
  char string [256];
 printf("Enter a character:");
 scanf("%c",&ch);
fflush(stdin);
  printf ("Insert your full address: ");
  gets (string);
  printf ("Your address is: %s\n",string);
  return 0;
}

hmm, that's weird. I copied and pasted your exact code in my IDE and it worked flawlessly.
Tell me your OS, and IDE. Also, tell me exactly what you did to compile and to run the object file.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Problem with gets() function

Post by BIT0309-Samsuddoha on Sat Sep 10, 2011 3:24 pm

OS-ubuntu, IDE-codeblocks

BIT0309-Samsuddoha
Pre-Alpha Release
Pre-Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 1
Points : 1

View user profile

Back to top Go down

Re: Problem with gets() function

Post by BIT0122-Amit on Sat Sep 10, 2011 3:30 pm

and the steps to compile and run?

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Problem with gets() function

Post by BIT0111-muctadir on Sun Sep 11, 2011 1:23 am

Code:
#include<stdio.h>

#include<string.h>
int main()
{

    char ch;
    char string [256];
    printf("Enter a character:");
    scanf("%c",&ch);
    //fflush(stdin);

    getchar();

    printf ("Insert your full address: ");
    gets (string);
    printf ("Your address is: %s\n",string);
    return 0;
}

Try this...

_________________________________________________________________
microsoft provides you windows........
ubuntu gives you the whole home..........
---------------------------------------------------------------------------------
Don't forget to visit my blog for more tutorials.....
avatar
BIT0111-muctadir
Expert
Expert

Course(s) :
  • BIT

Blood Group : B+
Posts : 233
Points : 395

View user profile

Back to top Go down

Re: Problem with gets() function

Post by BIT0130-Shakkhor on Sun Sep 11, 2011 1:45 am

I'm not sure if this is relevant, but are you trying to input a string with whitespaces using gets( )? Well, i have a better suggestion for that. Why don't you just use an inverted scanset in scanf( )?
Code:
scanf("\n%[^\n]",string);
This should do the trick. The whole code should look like this:

Code:
#include<stdio.h>
    #include<string.h>
    int main()
    {

        char ch;
        char string [256];
        printf("Enter a character:");
        scanf("%c",&ch);
        printf ("Insert your full address: ");
        scanf("\n%[^\n]",string);
        printf ("Your address is: %s\n",string);
        return 0;
    }

_________________________________________________________________
I'm not a driver with drinking problem.
I'm an alcoholic with driving problem.

Blog | বাংলা ব্লগ
avatar
BIT0130-Shakkhor
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 209
Points : 328

View user profile

Back to top Go down

Re: Problem with gets() function

Post by BIT0102-Mohaimin on Sun Sep 11, 2011 3:10 am

I thought I posted something here!!!

Ok, this link may help:
http://stackoverflow.com/questions/2366509/input-in-c-scanf-before-gets-problem

Shakkhor has already said what "I thought" I posted. This old post in this forum may help.
http://iitdu.forumsmotion.com/t1781-some-advanced-uses-of-scanf
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Re: Problem with gets() function

Post by bit0223-sujon on Sun Sep 11, 2011 9:39 am

topic replaced to " Programming and Brainstorming Chamber" as it is the appropriate section for this.

bit0223-sujon
Global Moderator-RC

Course(s) :
  • BIT

Posts : 98
Points : 218

View user profile

Back to top Go down

Re: Problem with gets() function

Post by BIT0322-Hira on Sun Sep 11, 2011 4:13 pm

BIT0122-Amit wrote:and the steps to compile and run?
I clicked the option compile and run.

BIT0322-Hira
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : O+
Posts : 24
Points : 33

View user profile

Back to top Go down

Re: Problem with gets() function

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum