|
-
April 4th, 2009, 07:56 PM
#1
inputting a fraction in the form x/y
I want the user to be able to input a fraction in the form "x/y" where x and y are ints and then store the x as a variable numerator and y as a variable denominator. Iv been trying to use cin.gets and ignores but i cant figure this out. I know its probly easy. Thanks for any help.
-
April 5th, 2009, 02:33 AM
#2
Re: inputting a fraction in the form x/y
-
April 5th, 2009, 07:45 AM
#3
Re: inputting a fraction in the form x/y
Read a string from the keyboard
Search for an occurrence of the character '/'
Create 2 new strings: one with number before '/' and one with number after '/'
Convert the 2 new strings to numbers with atoi() or atol() and store them as your object's numerator and denominator, respectively
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 5th, 2009, 07:53 AM
#4
Re: inputting a fraction in the form x/y
Why not:
Code:
int x, y;
char a;
cin>>x>>a>>y;
-
April 5th, 2009, 06:48 PM
#5
Re: inputting a fraction in the form x/y
 Originally Posted by Tronfi
Why not:
Code:
int x, y;
char a;
cin>>x>>a>>y;
I want to be able to type for example; 1/2 then press enter. Not have an enter after each entry.
-
April 5th, 2009, 06:49 PM
#6
Re: inputting a fraction in the form x/y
 Originally Posted by yamahammer342
I want to be able to type for example; 1/2 then press enter. Not have an enter after each entry.
That is what Tronfi's example does.
-
April 6th, 2009, 06:11 AM
#7
Re: inputting a fraction in the form x/y
Maybe also try std::getline
Code:
string text;
while (getline(cin, text, '/'))
{
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|