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

should this work

The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using
functions, if statement and logical operators.

/* ARITH1.C SIMPLE CALCULATOR PROGRAM */

#include<stdio.h>

float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;

int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);

call(d);
return 0;
}
char call(float math)
{

while(d != 'q'){
printf("Enter operand or q to exit: ");
scanf("%s",&d);

if(d == '+')
{ math = add(a,b); }
if(d == '-')
{ math = sub(a,b); }
if(d == '*')
{ math = multi(a,b); }
if(d == '/')
{ math = div(a,b); }
if(d == '%')
{ math = per(a,b); }
if(d != '+' && d != '-' && d != '*' && d != '/' && d != '%' && d != 'q')
{ printf("Operand not accepted try again\n"); continue; }
if(d == 'q')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
}

float add(float a, float b)
{
c = a + b;
return c;
}

float sub(float a,float b)
{
c = a - b;
return c;
}

float multi(float a, float b)
{
c = a * b;
return c;
}

float div(float a, float b)
{
c = a / b;
return c;
}

float per(float a, float b)
{
c = a * b / 100;
return c;
}

Nov 13 '05 #1
13 1502
amanayin writes:
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using functions, if statement and logical operators.

/* ARITH1.C SIMPLE CALCULATOR PROGRAM */

#include<stdio.h>

float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;

int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);

call(d);
I wouldn't do that. You are passing call() a char when it wants a float.
There is not reson to do that in code such as this. It may have no bearing
on the problem you say you *used to have*.

return 0;
}
char call(float math)
{

while(d != printf("Enter operand or q to exit: ");
scanf("%s",&d);

if(d == '+')
{ math = add(a,b); }
if(d == '-')
{ math = sub(a,b); }
if(d == '*')
{ math = multi(a,b); }
if(d == '/')
{ math = div(a,b); }
if(d == '%')
{ math = per(a,b); }
if(d != '+' && d != '-' && d != '*' && d != '/' && d != '%' && d != 'q') { printf("Operand not accepted try again\n"); continue; }
if(d == 'q')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
}

float add(float a, float b)
{
c = a + b;
return c;
}

float sub(float a,float b)
{
c = a - b;
return c;
}

float multi(float a, float b)
{
c = a * b;
return c;
}

float div(float a, float b)
{
c = a / b;
return c;
}

float per(float a, float b)
{
c = a * b / 100;
return c;
}

Nov 13 '05 #2
amanayin <ng******@netscape.net> wrote:
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should work
when i was compiling it i was getting warning messages but then suddenly
it started working. The reason i wrote the program was to get used to using
functions, if statement and logical operators.

/* ARITH1.C SIMPLE CALCULATOR PROGRAM */

#include<stdio.h>

float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
'div' is a bad name for a function, because there's a function with the
same name declared in stdlib.h. Not a problem yet, but if you have to
include stdlib.h later the two definitions will clash. Call it 'divide'
or the like.
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;

int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);

call(d);
You pass a character to a funtion that expects a double.
return 0;
}
char call(float math)
1. Why do you pass an argument at all?
2. Your function is declared to return a character.
Later on you return a double.
{

while(d != 'q'){
printf("Enter operand or q to exit: ");
scanf("%s",&d);
The "%s" conversion specifier is for reading strings. It will store any
user input plus a null character at &d, but there's only space for one
character. Use "%c" (overkill) or just d = getchar(); (recommended).

if(d == '+')
{ math = add(a,b); }
if(d == '-')
{ math = sub(a,b); }
if(d == '*')
{ math = multi(a,b); }
if(d == '/')
{ math = div(a,b); }
if(d == '%')
{ math = per(a,b); }
if(d != '+' && d != '-' && d != '*' && d != '/' && d != '%' && d != 'q')
{ printf("Operand not accepted try again\n"); continue; }
if(d == 'q')
{ puts("Exiting"); break; }
printf("Total is %.3f\n",math);
}
return math;
See above.
}

float add(float a, float b)
{
c = a + b;
return c;
}

float sub(float a,float b)
{
c = a - b;
return c;
}

float multi(float a, float b)
{
c = a * b;
return c;
}

float div(float a, float b)
See above.
{
c = a / b;
return c;
}

float per(float a, float b)
{
c = a * b / 100;
return c;
}


Besides of the mentioned problems your code looks fine AFAICT.

HTH

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3
On Sun, 19 Oct 2003 16:41:20 +0000 (UTC)
amanayin <ng******@netscape.net> wrote:
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should
work when i was compiling it i was getting warning messages but then
suddenly it started working. The reason i wrote the program was to get
used to using functions, if statement and logical operators.

/* ARITH1.C SIMPLE CALCULATOR PROGRAM */

#include<stdio.h>

float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;
Why declare these as globals? It is far easier to get things right if
you minimise the scope of variables.
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);

call(d);
call is declared as taking a float but you are passing a character. You
compiler should have warned you about this. Also d has not been
initialised and by passing it as a parameter you are evaluating it.
return 0;
}
char call(float math)
You never use the value passed in so why have it as a parameter?
{

while(d != 'q'){
d still has not been initialised so absolutely ANYTHING could happen
here.
printf("Enter operand or q to exit: ");
This may not actually be printed before the scanf. Read the FAQ for
further information.
scanf("%s",&d);
%s fetches a null terminated string, not a single character but you are
only passing the address of a single character, so this is guaranteed to
overflow on any input.
if(d == '+')
{ math = add(a,b); }
if(d == '-')
{ math = sub(a,b); }
if(d == '*')
{ math = multi(a,b); }
if(d == '/')
{ math = div(a,b); }
if(d == '%')
{ math = per(a,b); }
if(d != '+' && d != '-' && d != '*' && d != '/' && d != '%' && d
!= 'q')
{ printf("Operand not accepted try again\n"); continue; }
if(d == 'q')
{ puts("Exiting"); break; }
The break will exit the while loop making the condition completely
pointless once all the other bugs are fixed.
printf("Total is %.3f\n",math);
}
return math;
}

float add(float a, float b)
{
c = a + b;
return c;
Why are you using a global variable here? Why not just use
return a + b;
}


<snip>

I seriously think you need to do work through a decent text book. You
will find recommendations in the FAQ for comp.lang.c (Q 18.10)
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #4
Irrwahn Grausewitz <ir*******@freenet.de> wrote:

s/double/float/

--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #5
Mark Gordon <sp******@flash-gordon.me.uk> wrote:
amanayin <ng******@netscape.net> wrote:

<snip>
{
while(d != 'q'){


d still has not been initialised so absolutely ANYTHING could happen
here.


d has been initialized to 0 automatically on declaration.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6
On Sun, 19 Oct 2003 21:06:05 +0200
Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Mark Gordon <sp******@flash-gordon.me.uk> wrote:
amanayin <ng******@netscape.net> wrote:

<snip>
{
while(d != 'q'){


d still has not been initialised so absolutely ANYTHING could happen
here.


d has been initialized to 0 automatically on declaration.


You're right. I saw so many problems in how the code was written I saw
more problems than were there.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #7
Irrwahn Grausewitz wrote:
'div' is a bad name for a function,
because there's a function with the same name declared in stdlib.h.
Not a problem yet,


I think it is a problem.

My copy of the C89 last draft, has this:
4.1.2 Standard headers
All external identifiers declared in any of the headers are
reserved, whether or not the associated header is included.

--
pete
Nov 13 '05 #8
On Sun, 19 Oct 2003 19:34:50 +0100, Mark Gordon
<sp******@flash-gordon.me.uk> wrote:
On Sun, 19 Oct 2003 16:41:20 +0000 (UTC)
amanayin <ng******@netscape.net> wrote:
The program is simple and i know i should use switch instead of if
but i done it this way for a reason. But i am not sure if it should
work when i was compiling it i was getting warning messages but then
suddenly it started working. The reason i wrote the program was to get
used to using functions, if statement and logical operators.

/* ARITH1.C SIMPLE CALCULATOR PROGRAM */

#include<stdio.h>

float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
float per(float a, float b);
char call(float math);
float a,b,c,math;
char d;


Why declare these as globals? It is far easier to get things right if
you minimise the scope of variables.
int main(void)
{
printf("Enter 1st value: ");
scanf("%f",&a);
printf("Enter 2nd value: ");
scanf("%f",&b);

call(d);


call is declared as taking a float but you are passing a character. You
compiler should have warned you about this. Also d has not been


Why would you expect a warning when the conversion from char to float
is automatic and well defined?

<<Remove the del for email>>
Nov 13 '05 #9
pete <pf*****@mindspring.com> wrote:
Irrwahn Grausewitz wrote:
'div' is a bad name for a function,
because there's a function with the same name declared in stdlib.h.
Not a problem yet,


I think it is a problem.


You're right. I should have said: "Possibly not causing real trouble
yet, but definitely a problem ..."

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #10
Irrwahn Grausewitz wrote:

snip

while(d != 'q'){
printf("Enter operand or q to exit: ");
scanf("%s",&d);


The "%s" conversion specifier is for reading strings. It will store any
user input plus a null character at &d, but there's only space for one
character. Use "%c" (overkill) or just d = getchar(); (recommended).


If i use d = getchar(); or %c instead of %s i get the
following out put
Enter operand or q to quit: operand not accepted try again:
change it back to %s it works fine

Nov 13 '05 #11
Mark Gordon wrote:

char call(float math)


You never use the value passed in so why have it as a parameter?


so what should i change it to?

Nov 13 '05 #12
On Mon, 20 Oct 2003 18:27:43 +0000 (UTC)
amanayin <ng******@netscape.net> wrote:
Mark Gordon wrote:
char call(float math)


You never use the value passed in so why have it as a parameter?


so what should i change it to?


When you want a variable within a function you should declare it within
that function.

From memory something like

void call(void)
{
float math;
/* do stuff */
}

would have been better.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #13
Thanks for that

Nov 13 '05 #14

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
1
by: russ | last post by:
Been looking at some code..... #include <vector> #include <iostream> #include <algorithm> template <typename T> struct A { struct B {
4
by: RC | last post by:
I just got my first Access project that I am getting paid for. I have done other Access work for non-profits that I did not get paid for. I definitely have the work but we did not settle the...
6
by: Terry Bell | last post by:
We've had a very large A97 app running fine for the last seven years. I've just converted to SQL Server backend, which is being tested, but meanwhile the JET based version, running under terminal...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
5
by: Andy B | last post by:
I was just wondering, when you create dataContext methods, should you put business logic there to try and minimize pushing data through 2-3 layers of code? or should the business logic still go in...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.