Jj Hagen
Jj Hagen

Reputation: 37

How to split a fraction into two integers c++

I'm working on an assignment. I need to have the user input a fraction in #/# format . How can I set the top and bottom to two separate variables?

Here's a chunk of code I've tried, but I keep getting nothing for the second variable:

#include <iostream>
#include <conio.h>
#include <cstdio>
#include <regex>

using namespace std;

int main() {
    string firstFraction;

    cout << "Enter your first real Fraction: " << endl;
    firstFraction = cin.get();

    string delimiter = "/";

    string numerator = firstFraction.substr(0,firstFraction.find(delimiter));

    size_t pos = firstFraction.find("/");
    string denominator = firstFraction.substr(pos); 

    cout << numerator << " / " << denominator << endl;
    _getch();
    return 0;
}

Upvotes: 1

Views: 1799

Answers (3)

user4581301
user4581301

Reputation: 33932

This could be as simple as

#include <iostream>
#include <sstream>
#include <string>
//using namespace std; dangerous! Use with caution

int main()
{
    int num; // want a number as numerator
    int denom; // and a number as denomenator
    char divsign; // and a character to hold the / 

    std::cout << "Enter your first real Fraction: " << std::endl;
    // user input contains at least a numerator a / and a denominator
    // anything less fails. anything more will slip through. If this is a
    // problem, add another >> to see if there is more in the stream
    if (std::cin >> num >> divsign >> denom && // all input read successfully
        divsign == '/') // and the division operator was present
    { // got what we need. Print it.
        std::cout << num << " / " << denom << std::endl;
    }
    else
    { // bad input. insult user.
        std::cout << "Bogus user input. No fraction for you" << std::endl;
    }
    return 0;
}

It has a number of potential failure cases, such as:

999999999999 / 2
Bogus user input. No fraction for you

Integer overflow. The input was too big. And

1/1dfjklghaljkgadlfhjgklahd
1 / 1

Crap after the last character

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 596377

Try something like this:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    string fraction;

    cout << "Enter your first real Fraction: " << endl;
    getline(cin, fraction);

    istringstream iss(fraction);

    string numerator, denominator;
    getline(iss, numerator, '/');
    getline(iss, denominator);

    cout << numerator << " / " << denominator << endl;

    cin.get();
    return 0;
}

Upvotes: 1

Daniel Jour
Daniel Jour

Reputation: 16156

firstFraction = cin.get();

Print firstFraction after this line and see whether it contains what you thought it contains. Looking in the reference (something you should be doing!) ...

[..] Reads one character and returns it if available. [..]

... we learn that you're only reading a single character (of unformatted input). How do you intend to split the resulting (single character) string?

There are multiple ways of doing it correctly, which one to choose depends a lot on your needs. A short, non-complete list:

  • std::getline the whole line, and then searching for / within it
  • std::getline until the next /, std::getline until end of line. (I don't really recommend this)
  • Formatted input, for example :

    #include <iostream>
    using namespace std;
    
    int main() {
        unsigned int nominator, denominator;
        char sep;
        cin >> nominator >> sep >> denominator;
    
        if (!cin || sep != '/') {
            cerr << "Well... you know, that failed somehow." << endl;
            return 1;
        }
        cout << "Fraction: " << nominator << "/" << denominator << endl;
        return 0;
    }
    

    Though this also allows input like

    3   / 4
    

    and

    3
    /
    4
    

And of course, you should abstract this, e.g. make a fraction class, and write a (member) function read_fraction (and also provide a suitable operator>> if you want).

Upvotes: 0

Related Questions