runtime error-gnu c compiler

E

emmba

Hello. If anyone could help me, I would be very grateful. My program
will compile, but when I run it, the function stringOperation is not
returning the correct value.

The premise of my assignment is to "Write a program that inputs four
strings that represent floating point values, converts the strings to
double values, and sums them.

Here is my code:

#include <stdio.h>
#include <string.h>

double stringOperation();

int main() {

double sum1;
double sum2;
double sum3;
double sum4;

system( "clear" );

/* display program headings */
printf( "\n\n\tCIS161 prog11.c By Emma Evans\n\n" );

/* prompt user to enter strings */
printf( "\tPlease enter 4 strings.\n\n\t\tString 1: " );
sum1 = stringOperation();
printf( "\t\tString 2: " );
sum2 = stringOperation() + sum1;
printf( "\t\tString 3: " );
sum3 = stringOperation() + sum2;
printf( "\t\tString 4: " );
sum4 = stringOperation() + sum3;

printf( "\n\tAfter converting the strings to double values,\n" );
printf( "\tI have calculated their sum." );

printf( "\n\tThe total of the strings is %.2f", sum4 );

printf( "\n\n\tEnter to exit..." );
getchar();

system( "clear" );

return 0;
} /* end function main */

double stringOperation() {

char string[ 50 ];
double fl = 0;
double sum = 0;

gets( string );

/* change string to float */
fl = atof( string );

/* add fl to sum */
sum += fl;

return sum;

} /* end stringOperation */
 
S

stathis gotsis

emmba said:
Hello. If anyone could help me, I would be very grateful. My program
will compile, but when I run it, the function stringOperation is not
returning the correct value.

The premise of my assignment is to "Write a program that inputs four
strings that represent floating point values, converts the strings to
double values, and sums them.

Here is my code:

#include <stdio.h>
#include <string.h>

#include <stdlib.h>, maybe this solves the problem?
 
B

Bill Gutz

emmba said:
It's fixed! Thank you so much. Now I just feel silly.

Don't feel silly just yet.

PC-lint says this:

Warning 421: Caution -- function 'gets(char *)' is considered dangerous
 
B

Barry Schwarz

Hello. If anyone could help me, I would be very grateful. My program
will compile, but when I run it, the function stringOperation is not
returning the correct value.

The premise of my assignment is to "Write a program that inputs four
strings that represent floating point values, converts the strings to
double values, and sums them.

Here is my code:

#include <stdio.h>
#include <string.h>

double stringOperation();

One of the purposes of a prototype is to specify the parameters of the
function so the compiler can check (and convert if necessary) the
arguments in the actual call. This should be
double stringOperation(void);
int main() {

double sum1;
double sum2;
double sum3;
double sum4;

The way you use them, you don't need all four. You could use sum1
everywhere you use one of the other sum variables.
system( "clear" );

I wonder how many systems besides mine don't have a clear command. If
you avoid system specific features you allow more people to help you.
/* display program headings */
printf( "\n\n\tCIS161 prog11.c By Emma Evans\n\n" );

/* prompt user to enter strings */
printf( "\tPlease enter 4 strings.\n\n\t\tString 1: " );
sum1 = stringOperation();
printf( "\t\tString 2: " );
sum2 = stringOperation() + sum1;
printf( "\t\tString 3: " );
sum3 = stringOperation() + sum2;
printf( "\t\tString 4: " );
sum4 = stringOperation() + sum3;

printf( "\n\tAfter converting the strings to double values,\n" );
printf( "\tI have calculated their sum." );

printf( "\n\tThe total of the strings is %.2f", sum4 );

printf( "\n\n\tEnter to exit..." );
getchar();

system( "clear" );

return 0;
} /* end function main */

Useless comments only clutter up your code. If you go to the trouble
of adding a comment, it should say something not immediately obvious
from looking at the code. If your function main had been several
pages long, then the comment would be reasonable.
double stringOperation() {

char string[ 50 ];
double fl = 0;
double sum = 0;

gets( string );

gets is not a very safe method of obtaining input. Look up fgets in
your reference. Names beginning with str and a lower case letter are
reserved for the implementation. Get in the habit of not using them.
/* change string to float */
fl = atof( string );

atof has very poor error handling. Look up strtod in your reference.

If your compiler did not produce a diagnostic about no prototype in
scope for atof, you should up the warning level. If it did, you
should not ignore it.
/* add fl to sum */
sum += fl;

Since sum is always 0.0, why not simply return fl?
return sum;

} /* end stringOperation */


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top