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.
Re: inputting a fraction in the form x/y
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
Re: inputting a fraction in the form x/y
Why not:
Code:
int x, y;
char a;
cin>>x>>a>>y;
Re: inputting a fraction in the form x/y
Quote:
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.
Re: inputting a fraction in the form x/y
Quote:
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.
Re: inputting a fraction in the form x/y
Maybe also try std::getline
Code:
string text;
while (getline(cin, text, '/'))
{
}