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 */
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 */