LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2006, 07:58 AM   #1
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 285

Rep: Reputation: 32
scanf anomaly


Hi friends,
plz go through the code.

#include<stdio.h>
int main() {
char str[30];
char opt='y';

while(opt=='y') {
printf("enter str :");
scanf("%s",str); //u can also use gets(str)
printf(\nstr = %s",str);

printf("\n enter option(y/n) :");
scanf("%s",&opt);
}
}

result :
first time it's takes i/p for str and prints and then leaves the program.

Initially i was thinking a buffer problem and i used "fflush(stdin)" but it bahaved as before. But when i tried two "scanf("%s",str);" consecutively it works fine. I don't know why it's taking some arbitary value of c for the first scanf. Can anyone of u sort out the problem ?

With thanks in advance,
barun
 
Old 08-11-2006, 08:58 AM   #2
tharg
LQ Newbie
 
Registered: May 2005
Location: Antarctica
Distribution: Red Hat 9
Posts: 3

Rep: Reputation: 0
Hi, been a while since I did C.

I'm not sure if this is the right place to ask this type of question, you're probably better off posting here to comp.lang.c
 
Old 08-11-2006, 11:01 AM   #3
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Quote:
printf(\nstr = %s",str);
Aren't you missing some double quotes here? Or did you copy it wrong in the post?
Also, you should include a '\n' at the end of the print or flush stdout.

Last edited by timmeke; 08-11-2006 at 11:04 AM.
 
Old 08-11-2006, 12:08 PM   #4
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
Hi,
I could not reproduce the problem but it seems that scanf leaves the newline on the input stream. This causes the program to end if u use scanf("%c",&opt) (as it should be used). Furthermore u should make sure u dont read a string longer than the buffer because it causes the programm to segfault.

exman
 
Old 08-11-2006, 11:54 PM   #5
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 285

Original Poster
Rep: Reputation: 32
Yes i missed one quote while copying :

#include<stdio.h>

int main() {
char str[30];
char opt='y';

while(opt=='y') {
printf("\nstr ?");
scanf("%s",str);
printf("\nstr= %s\n",str);

printf("\n your opt(y/n) ? ");
scanf("%c",&opt);
}
}

This is my code ..
Yes i know my maximum string length is 30.
I am using Fedora 2.6 and gcc for compilation and have tested it more than two machines.
plz ur help needed.
 
Old 08-12-2006, 03:07 AM   #6
PingFloyd
Member
 
Registered: Jun 2006
Posts: 94

Rep: Reputation: 16
Here is what I did to get it working.

#include<stdio.h>

int main()
{
char str[30];
char opt='y';

while (opt=='y')
{
printf("enter str: ");
scanf("%s",str);
printf("str=%s\n",str);

printf("enter option(y/n): ");
scanf("%s",&opt);
}
return(0);
}

One thing I noticed, is that it doesn't matter if you hit "n" to quit since anything other than "y" seems to return false.

So I got to thinking of some ideas.

#include<stdio.h>

int main()
{
char str[30];
char opt='y';

while (opt=='y')
{
printf("enter str: ");
scanf("%s",str);
printf("str=%s\n",str);
do {
printf("enter option(y/n): ");
scanf("%s",&opt);

if (opt !='y' && opt !='n') {
printf("invalid option!\n");
}
} while (opt !='y' && opt!='n');

}
printf("goodbye...\n");
return(0);
}

I wonder if there is a better way to accomplish the same as thing but in a more kosher manner. Or was I going about things right way?

I'm not very experienced in C so I learned a little bit by trying to change the way it functions a little.
 
Old 08-12-2006, 12:59 PM   #7
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
The code is broken. opt is declared as a char, but you are reading a string into it:

Code:
char opt='y';
Code:
scanf("%s",&opt);
This is wrong, and will causes problems at some point.

Also note that scanf reads exactly what you tell it, and leaves everything else (including the newline, as exman pointed out) on the input stream. Frankly, it is a strange function that is often difficult to use properly. You might try fgets or getline instead, which has overflow protection. See man 3 getline (note that getline is a GNU extension).

Ideally, you'd want to read a single character into opt, but alas, this is not trivial on a unix terminal (you have to use raw mode input, and it's probably too much trouble in this case).

If you use getline, it will read an entire line, but this is no problem, just look at the first character to see if it's y, the rest of the line can be ignored. So, you'd change opt to a char pointer, and pass that to getline,
e.g.

Code:
char *opt = NULL; /* getline will allocate memory and store the pointer in opt */
...
getline(&opt, 1, stdin);
then check if opt[0] == y/n, don't forget to free opt later (not that it's a big deal in this small program but it's good to get into the habit of freeing everything that is allocated).

If you must use scanf, then change it to read in a single char, i.e. scanf("%c", &opt).

Last edited by soggycornflake; 08-12-2006 at 01:02 PM.
 
Old 08-12-2006, 08:42 PM   #8
PingFloyd
Member
 
Registered: Jun 2006
Posts: 94

Rep: Reputation: 16
Good to know. Thanks for the pointers (pun intended) .
 
Old 08-13-2006, 11:03 AM   #9
barunparichha
Member
 
Registered: Jun 2006
Location: Bangalore,india
Distribution: Linux(Redhat,fedora,suse,ubantu), Solaris (s8/s9/s10/nevada/open-solaris)
Posts: 285

Original Poster
Rep: Reputation: 32
i checked this code if i m using

scnaf("%s",&opt);

it works fine but not for "scanf("%c",&opt);".


U can also use two consecutive scanfs like

scnaf("%s",&opt);
scnaf("%s",&opt);


But still it's not clear what character it's taking in first scanf. and ofcourse how it works for "%s".

With thanks,
barun
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strlen anomaly barunparichha Linux - Newbie 6 08-10-2006 01:16 PM
CUPS anomaly barunparichha Linux - Software 0 07-03-2006 01:58 AM
PDF Writer anomaly corster Linux - General 1 06-02-2006 12:31 AM
cpu fan anomaly in notebook zacklemon Linux - Hardware 4 04-01-2005 10:43 PM
FC2T1 sound anomaly... MadOtis Fedora 1 03-23-2004 07:58 AM


All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration