LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-06-2005, 05:36 PM   #1
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Rep: Reputation: 15
cant divide with C


i seem to have gotten into trouble with the most rudimentary function around;
#include <stdio.h>
main()
{ printf("%f", 4/3); }

is producing a long 3-line number outpour. (dividing works for me if the answer is a whole number)

problem two:
after changing everything in an "int" dominated program to a "char" dominated one, the program starts intaking only half of the amount of numbers i specify i want to put in .eg:
please enter char 1: please enter char 2:
please enter char 3: please enter char 4:
please enter char 5:

with the above, i specify five and it only scanfs 2 chars(char 2 and 4), skipping 1, 3, and 5 completely. when i change everything back to integers the program functions properly.

???????whats happening?

Last edited by skoot; 08-06-2005 at 05:41 PM.
 
Old 08-06-2005, 06:16 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Your compiler is certainly spitting an error message when the -Wall option (assuming you are using gcc) is used, fix first what it is complaining of before trying to run your program !
 
Old 08-07-2005, 04:23 AM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Your "4/3" is producing an int result, and you're telling printf() to treat it as a float. If you tell printf() to do the wrong thing, you shouldn't complain when it does the wrong thing! Try
Code:
printf(%f\n", 4.0/3);
I don't understand your second question, can you post a minimal code version of the program that exhibits the problem, please?
 
Old 08-07-2005, 05:59 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The second problem cause will probably be exhibited if -Wall gcc option is used too.
 
Old 08-07-2005, 09:34 AM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
You could always recast the int into a float as well


try

Code:
 

printf("%f",(float) 4 / 3);
 
Old 08-07-2005, 09:49 AM   #6
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Original Poster
Rep: Reputation: 15
dividing in c

what is the -Wall option?
already tried(float(4/3), shall try 4.0/3.
here is what the second problem relates to(real version in C of course;P):
main()
{
int a;
char *b;
scanf("number of values you want to enter"); /* this value is assigned to a */
b = (char*) calloc etc. etc.;
for(a-1 times do the following)
scanf("%c", (b+a));
for(a-1 times do the following)
{ printf("char %d = %c \n" , a+1, *(b+a)); }
}
 
Old 08-07-2005, 09:57 AM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
-Wall is a compiler option like such



user@Hell$ gcc -Wall test.c
 
Old 08-07-2005, 10:27 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Hmm, you tried "float(4/3)" when "(float)4/3" was suggested ...
Expect disappointments.

Please enclose your code sample between [ code ] tags, and post your real code, not pseudo code, where the possible mistakes are not visible.

Finally, compile your code with gcc -Wall option, and don't post again until no warning is fixed, or only if you do not manage to find how to fix one of these.
 
Old 08-07-2005, 03:23 PM   #9
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Original Poster
Rep: Reputation: 15
-Wall

okay. dividing has worked. thanks

for the second problem -Wall stated the following "errors":
1) line with '{' (below main() line)
2) bottom line with the corresponding '}' (showing that 1 and 2 are not errors)
3)the line: charpointer = ((char*)calloc(a, sizeof(char))); /* where 'a' is amount of times i want scanf to scanf the charpointer value, this line looks fine to me(?) */

(sorry about the pseudo-c in last post, didnt think it would offend)
 
Old 08-07-2005, 03:32 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Hard to comment without your real source code posted nor the real gcc messages.
 
Old 08-09-2005, 12:02 PM   #11
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
First of all u should convert 4/3 to float using (float)(4/3).
Then try to print it as float.......u r done.........
 
Old 08-09-2005, 01:32 PM   #12
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Your second problem has something to do with the \n charecter. When your done imputing
your charecter it uses the newline generated by enter in the next statement. doesent work with integers because \n is not an integer but this is only true if you are parsing
the imput witch without code then would be hard to determine.
 
Old 08-10-2005, 03:20 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
First of all u should convert 4/3 to float using (float)(4/3).
Then try to print it as float.......u r done.........
This is just plain wrong ...

There's no point converting after the division between integers is done.
 
Old 08-12-2005, 04:21 PM   #14
skoot
Member
 
Registered: Apr 2005
Distribution: Ubuntu
Posts: 70

Original Poster
Rep: Reputation: 15
\n

regarding the '\n' suggested by exvor:
1) wouldnt this only happen if im using the %s and not %c.
surely '\n' is not mistaken for a character(?)
2)please enter char onelease enter char two:
this is what is printed- the program doesnt even wait for me to enter anything before asking for the second char, so i havnt had the chance to even press enter at this time, cancelling out the chance of the program receiving a '\n', or am i wrong?
 
Old 08-13-2005, 07:36 AM   #15
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
\n is a charecter. getchar waits for you to hit enter to accept the charecter enterd this
enter press is put into the keyboard buffer and used next time getchar is called.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Divide up lines with string delimiter elmu Programming 3 10-07-2005 08:48 AM
Using a seperate router to divide network coindood Linux - Networking 4 09-14-2005 11:13 AM
how to divide two string variables user_linux Programming 3 06-20-2005 04:54 PM
Partimage & Divide by Zero KimVette Linux - Hardware 3 06-01-2005 10:18 AM
Divide a file into 2 parts bmbsa Linux - Newbie 3 08-13-2003 04:08 PM

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

All times are GMT -5. The time now is 08:57 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