C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-29-2009, 02:40 AM   #1
Registered User
 
Join Date: May 2009
Posts: 6
f1 =P

*hmm..I can't seem to make this code to work,it always bypasses reading the "operation" and doesn't read it.I would apperciate it ,if you could help me out..thnx in advance =)


**program basically reads 2 float numbers from the user and a character '*', '-' , '/', '+' and does one of these operations and prints out the result..and also goes into a loop~

***operations is a subprogram..I think that covers it all..I know 1st post shouldn't be a question..sorry & thnx ..


Sample example ( user input in bold )
Enter first number 3.4
Enter second number 2.7
Enter operation +
The result of 3.4 + 2.7 = 6.1
Another operation ? ( Y/N ) Y
Enter first number 1.2
Enter second number 4.0
Enter operation /
The result of 1.2 / 4.0 = 0.3
Another operation ? ( Y/N) N

Code:
#include <stdio.h>
#include <conio.h>

void math (float a, float b,char f)
{
float result;
if ( f=='+')
result = a+b;
else if (f=='-')
result = a-b;
else if (f=='*')
result = a*b;
else if (f=='/')
result = a/b;
esle
print("wrong input");

printf("result = %f",result);
}

void main()
{
float x,y;
char operation,answer;

do{

printf("enter 1st number\n");
scanf("%f",&x);

printf("enter 2nd number\n");
scanf("%f",&y);

printf("enter operation \n");
scanf("%c",&operation);

math(x,y,operation);

printf("Do it again?\n");
scanf("%c",&answer);

}while((answer=='y')||(answer=='Y'));
getch();
}
Ulti is offline   Reply With Quote
Old 05-29-2009, 02:42 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Add a space before "%c" to make it " %c", and it will skip the newline left behind by the previous scanf() operation.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-29-2009, 02:46 AM   #3
Registered User
 
Join Date: May 2009
Posts: 6
....

lol..tht was the issue -.-...kinda annoying..well, thank you, but I wanna learn..so could u exlain a lil more in details..about why to leave space..i would be greatly thankful
thanks anyways =)
Ulti is offline   Reply With Quote
Old 05-29-2009, 02:54 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
scanf (and it's relatives) will read as much data as it can according to the format. So %f will read a floating point number, including sign, numbers, one decimal point and exponent. Once it can not read any more "number stuff" it leaves whatever is left in the input buffer. In the normal case, that maens that you have a newline left behind after the number has been read (because you hit enter to "enter" the number, and enter "is" newline ('\n')).

When you add a space to the format string, the space means "read any number of whitespaces" - and newline counts as whitespace.

For most types of input, whitespace is automatically "eaten" by the magical scanf whitespace muncher, but not for %c or %s.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-29-2009, 03:13 AM   #5
Registered User
 
Join Date: May 2009
Posts: 6
ok ,that helped alot ..now I understand a lil more about C.
Thanks,mr.Mats =)
Ulti is offline   Reply With Quote
Reply

Tags
bypass, code, operation, prog, subprogram

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Prime Numbers, Modulus Operator Help dcwang3 C Programming 34 02-07-2008 03:16 PM
MD5 hashing problems =( Uncle Rico C Programming 4 02-28-2005 10:31 AM
MD5 Algorithm Inquirer C Programming 2 12-28-2003 11:55 PM
F1 Key input Kebab Monster C Programming 4 03-20-2002 04:56 PM
F1 track analyser Unregistered Game Programming 1 01-22-2002 09:48 AM


All times are GMT -6. The time now is 01:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22