LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2003, 10:34 AM   #16
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30

Code:
	if(a == 1);printf("%f+%f=%f",b,c,b+c);
	if(a == 2);printf("%f-%f=%f",b,c,b-c);
	if(a == 3);printf("%fx%f=%f",b,c,b*c);
	if(a == 4);printf("%f/%f=%f",b,c,b/c);
That will not have the desired effect.
It will check if a==1 and then regardless of the result run printf.
Don't put the ; after if.
 
Old 12-06-2003, 08:09 PM   #17
Inuyasha-kun
LQ Newbie
 
Registered: Nov 2003
Distribution: Red Hat Linux 8
Posts: 20

Rep: Reputation: 0
Thanks, that fixed the entire problem.

Do you know how to make the program to execute without the "./" ?
 
Old 12-06-2003, 09:35 PM   #18
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
Quote:
Originally posted by Inuyasha-kun
Thanks, that fixed the entire problem.

Do you know how to make the program to execute without the "./" ?
If its a user account, you can add the directory where the executable(s) lies to that users path, I have a "home made" executable directory for all the programs I make.

Tarts
 
Old 12-22-2003, 07:25 PM   #19
Inuyasha-kun
LQ Newbie
 
Registered: Nov 2003
Distribution: Red Hat Linux 8
Posts: 20

Rep: Reputation: 0
Since it was Christmas break, I decided to practice with C. i was wondering, why does this code give out so many warnings?
Code:
#include <stdio.h>
//A is the number to pick the operation
//B and C are the single-precision floating point variables that will be modified
int a;
float b,c;
int main()
{
	float e;
	printf("NCalc 1.0b for Linux\nPick an op\n1:Add\n2:Sub\n3:Mul\n4:Div\n:");
	scanf("%d",&a);
	printf("\nNumber 1:");
	scanf("%f",&b);
	printf("\nNumber 2:");
	scanf("%f",&c);
	if(a == 1) printf("%f+%f=%f\n",b,c,addit(e));
	if(a == 2) printf("%f-%f=%f\n",b,c,subit(e));
	if(a == 3) printf("%fx%f=%f\n",b,c,mulit(e));
	if(a == 4) printf("%f/%f=%f\n",b,c,divit(e));
	return 0;
}
void addit(float d)
{
	float d;
	d=b+c;
	return d;
}
void subit(float d)
{
	float d;
	d=b-c;
	return d;
}
void mulit(float d)
{
	float d;
	d=b*c;
	return d;
}
void divit(float d)
{
	float d;
	d=b/c;
	return d;
}
 
Old 12-22-2003, 09:47 PM   #20
afrm
LQ Newbie
 
Registered: Oct 2003
Location: Portugal
Distribution: rh9
Posts: 27

Rep: Reputation: 15
Use this command to compile your C programs :

gcc -Wall -ansi -pedantic -g <file_name1>.c <file_name2>.c ... -o <prog_name>
 
Old 12-23-2003, 12:04 AM   #21
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
Quote:
Originally posted by Inuyasha-kun
Since it was Christmas break, I decided to practice with C. i was wondering, why does this code give out so many warnings?
Code:
#include <stdio.h>
//A is the number to pick the operation
//B and C are the single-precision floating point variables that will be modified
int a;
float b,c;
int main()
{
	float e;
	printf("NCalc 1.0b for Linux\nPick an op\n1:Add\n2:Sub\n3:Mul\n4:Div\n:");
	scanf("%d",&a);
	printf("\nNumber 1:");
	scanf("%f",&b);
	printf("\nNumber 2:");
	scanf("%f",&c);
	if(a == 1) printf("%f+%f=%f\n",b,c,addit(e));
	if(a == 2) printf("%f-%f=%f\n",b,c,subit(e));
	if(a == 3) printf("%fx%f=%f\n",b,c,mulit(e));
	if(a == 4) printf("%f/%f=%f\n",b,c,divit(e));
	return 0;
}
void addit(float d)
{
	float d;
	d=b+c;
	return d;
}
void subit(float d)
{
	float d;
	d=b-c;
	return d;
}
void mulit(float d)
{
	float d;
	d=b*c;
	return d;
}
void divit(float d)
{
	float d;
	d=b/c;
	return d;
}
To start, you didn't declare the functions before you used them. Either make a header file, or include the same info at the start of your file. The output from gcc would be nice but that should solve a lot of your pbelms. Another is that you declare d twice inside your functions

void divit(float d)
{
float d;

What happens is whatver flat divit gets, it names d and continues. This is the same as declaring the same variable twice.

Also a suggestion try to name your variables well. Things end up getting mangled otherwise. Another thing is that you return something when you declared that function void. Declare it to return a float if you want to do that. That's everything I caught at first glance
The gcc error messages will show whaterver is left.
 
Old 12-23-2003, 02:19 PM   #22
clockworks
Member
 
Registered: Dec 2003
Location: texas
Distribution: fedora core 1, fedora core 2
Posts: 37

Rep: Reputation: 15
Quote:
Originally posted by aizkorri
To compile use this

gcc -o execname -c yourprogram.c

If you don't use the -o option it will create a a.out where the executabe is. To execute the program write ./execname.
this will compile yourprogram.c into an object file. i will not link it to the c lib and make it an executable.

what you want to type is:

gcc -o execname yourprogram.c

-- C
 
Old 12-23-2003, 02:29 PM   #23
clockworks
Member
 
Registered: Dec 2003
Location: texas
Distribution: fedora core 1, fedora core 2
Posts: 37

Rep: Reputation: 15
Quote:
Originally posted by Inuyasha-kun
Since it was Christmas break, I decided to practice with C. i was wondering, why does this code give out so many warnings?
Code:
#include <stdio.h>
//A is the number to pick the operation
//B and C are the single-precision floating point variables that will be modified
int a;
float b,c;
int main()
{
	float e;
	printf("NCalc 1.0b for Linux\nPick an op\n1:Add\n2:Sub\n3:Mul\n4:Div\n:");
	scanf("%d",&a);
	printf("\nNumber 1:");
	scanf("%f",&b);
	printf("\nNumber 2:");
	scanf("%f",&c);
	if(a == 1) printf("%f+%f=%f\n",b,c,addit(e));
	if(a == 2) printf("%f-%f=%f\n",b,c,subit(e));
	if(a == 3) printf("%fx%f=%f\n",b,c,mulit(e));
	if(a == 4) printf("%f/%f=%f\n",b,c,divit(e));
	return 0;
}
void addit(float d)
{
	float d;
	d=b+c;
	return d;
}
void subit(float d)
{
	float d;
	d=b-c;
	return d;
}
void mulit(float d)
{
	float d;
	d=b*c;
	return d;
}
void divit(float d)
{
	float d;
	d=b/c;
	return d;
}
omg, i don't even know where to begin...

1) you used the functions addit(), subit(), mulit(), divit() before you declared them.
2) in those functions mentioned about, you have "d" declared twice...as a formal parameter and as a local variable.
3) you claim those functions to return void, but you are really returning a float (i.e. you are returning d).

i think those three things are what are causing your warnings...now on what else is wrong.

1) "a", "b", and "c" are global, this is not good programming practice.
2) you functions don't make much sense. you should do away with the vars "b", "c", and "d" and define your functions like this:
Code:
float addit(float a, float b)	{
	return a + b;
}
what you >really< need to do is take a class from a >good< teacher, or get a >good< book on c and another good book on good/proper programming practices...

-- C
 
Old 12-23-2003, 09:16 PM   #24
teval
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Gentoo
Posts: 720

Rep: Reputation: 30
I think the best teacher is good code. Go on sourceforge, get a good book (I don't know any good C books, only C++ books), read, and look at the code. Then implement something similar, without looking at the code this time.
You'll learn a lot.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
which programming language is used to do tcp/ip programming?? gajaykrishnan Linux - Networking 9 12-21-2012 05:16 AM
im new to programming but..... boxerboy Programming 6 08-26-2005 06:17 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM
C programming oranj Linux - Networking 1 12-07-2004 12:40 AM
Qt Programming... jinksys Programming 1 08-06-2003 04:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration