473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Using fgets() within a function returning an error.

Hi, I've been creating a simple calculator program that asks whether
you want to add, subtract, multiply or divide, asks you for input, and
then performs the calculation. Things seem to be going well enough, but
I've run into one problem. It seems when I use fgets() or gets() within
a function I wrote, it gives me no errors, but when run, doesn't pause
for input, but skips past it. I've tried running it inside main() and
it runs fine. Compiled using gcc 3.4.3.

Here's the code snippet involved. It's in a header file which is
included from main.c. If you run it, I believe that's what will happen.
From main, it simply calls add with add().


char bigbuff[101];

/* The rest of the code.*/

void add(void)
{
printf("What numbers do you wish to add?\n");
setupinput();
fgets(bigbuff, sizeof(bigbuff), stdin);
}

Thanks all.

Nov 15 '05 #1
1 1875
Aomighty wrote:
Hi, I've been creating a simple calculator program that asks whether
you want to add, subtract, multiply or divide, asks you for input, and
then performs the calculation. Things seem to be going well enough, but
I've run into one problem. It seems when I use fgets() or gets() within
a function I wrote, it gives me no errors, but when run, doesn't pause
for input, but skips past it. I've tried running it inside main() and
it runs fine. Compiled using gcc 3.4.3.
Best forget that gets() exists.
Note: fgets() is not necessarily the best function for the job, either
-- unfortunately, the standard library contains no good function for
that job.
Here's the code snippet involved. It's in a header file which is
included from main.c. If you run it, I believe that's what will happen.
From main, it simply calls add with add().

Please give us a fighting chance: Strip your code down to a minimal
example i.e. the minimum program that still exhibits your problem.
Throwing us a snippet which may or may not contain the _actual_
error is not helpful.
BTW: function definitions are _not_ for header files.
char bigbuff[101];
Unnecessary magic number:
#define INPUTSTRLEN 100
char bigbuff[inputlen + 1];
/* The rest of the code.*/

void add(void)
{
Note: This interface tells me that the function relies on global
data. This is an indicator for not-so-good (vulgo: bad) design.
printf("What numbers do you wish to add?\n");
setupinput();
What does setupinput() do? Can the error be there?
Is it possible that you leave some '\n' out there on
stdin?
fgets(bigbuff, sizeof(bigbuff), stdin);
sizeof bigbuff is sufficient.
You did not check
- the return value of fgets()
- call ferror() to find out whether there were problems

You may want to check strlen(bigbuff) and maybe fputs(bigbuff, stdout)
to start finding the error.

Back to setupinput(): It should contain something along the
lines of
int c;
while ((c = getchar()) != EOF)
if (c == '\n')
break;
}

Thanks all.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
10
by: Stuart Anderson | last post by:
I am looking to use Ken Stephenson's CirclePack program (http://www.math.utk.edu/~kens/) with some tiling programs written by Cannon, Floyd, Parry. The programs I want to use are subdivide.c...
26
by: Peter Mount | last post by:
Hello What's the syntax for using fgets() to store a string in memory? I understand that fgets() can solve the problem of storing a string that has more characters than the size of the declared...
20
by: Paul D. Boyle | last post by:
Hi all, There was a recent thread in this group which talked about the shortcomings of fgets(). I decided to try my hand at writing a replacement for fgets() using fgetc() and realloc() to read...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
1
by: Skeets | last post by:
i'm having some php problems. i have the following code on my main page: ------------ $fp = fsockopen("127.0.0.1", 49152, $errno, $errrstr, 30); if (!$fp) { echo "socket error: $errstr...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
26
by: Bill Cunningham | last post by:
I was talking with someone about fgets and he said that fgets puts the \n in a string but not \0. I decided to test this assumption because my documentation didn't say if fgets put \0 after a...
24
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.