473,382 Members | 1,247 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,382 software developers and data experts.

runtime error-gnu c compiler

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

Feb 10 '06 #1
4 2260
"emmba" <em****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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?
Feb 10 '06 #2
It's fixed! Thank you so much. Now I just feel silly.

Feb 10 '06 #3
emmba wrote:
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

--
Bill
Feb 10 '06 #4
On 9 Feb 2006 18:48:22 -0800, "emmba" <em****@gmail.com> wrote:
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
Feb 11 '06 #5

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

Similar topics

5
by: Bob Bamberg | last post by:
Hello All, I have been trying without luck to get some information on debugging the Runtime Error R6025 - Pure Virtual Function Call. I am working in C++ and have only one class that is derived...
5
by: Nathan Bloom | last post by:
Hi, I have a secured database that runs fine on the computer the database is installed on. I have several workstations with access runtime installed that also need access to the database. ...
2
by: Paul | last post by:
I'm hoping someone can help me with the problem. I have a database where the default value for a date field in a table is =date() This works perfectly in my table and on my form. But when I...
12
by: Markus Ewald | last post by:
I'm just experimenting around with the VisualC++ 2005 Express Edition Beta and found some strange behavior for which I can't seem to find a workaround. What I've done is set up two static library...
0
by: Yelena Varshal via AccessMonster.com | last post by:
Hello We have a shortcut for our MS ACCESS application that uses a /Runtime switch because we may have some users that use Runtime. The application worked fine in Access 2000 and was tested with...
8
by: g_man | last post by:
I am trying trap Runtime error 3022 (duplicates) in the click event of a command button that closes the form. I have code in the Form_Error event that does a good job of providing a more meaningful...
7
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement...
7
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then...
3
by: Jim Armstrong | last post by:
Hello all - This is driving me crazy. I have a table called tblClients - very simple, has the following fields: taxID (PK) ClientName SalesName The main form of my application allows a...
1
by: BL3WC | last post by:
Hi, I'd created a MDE under Access 2003. It is now under testing stage. Some of the users will use Access 2003 runtime and some will use Access 2007 runtime to run this MDE. I installed the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.