1

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jj Hagen
  • 37
  • 5
  • Please make sure you paste in your code correctly. You're missing the starts and end, along with the indentation for formatting. – TankorSmash Oct 12 '16 at 22:48
  • Does `string denominator = firstFraction.substr(pos); ` really make sense to you? If so, could you explain what you think it does? – David Schwartz Oct 12 '16 at 22:49
  • 1
    ^ You're missing a declaration of `firstFraction`. You can [edit] your question to include additional information. Also, we have to assume that `string` is indeed a `std::string` – Tas Oct 12 '16 at 22:50
  • i'm very new to c++ and this site as well. i apologize for any incorrect formatting. I've tried a bunch of stuff but this is just what gives me the closest result to what I'm looking for but not quite right. all i want is to set the top and bottom numbers of the fraction to two separate variable. – Jj Hagen Oct 12 '16 at 22:53
  • 1
    Take a look at [`std::stringstream`](http://en.cppreference.com/w/cpp/io/basic_stringstream) and [`std::getline(stream, variable, '/')`](http://en.cppreference.com/w/cpp/string/basic_string/getline) – user4581301 Oct 12 '16 at 22:55
  • @Tas Obviously namespaces are useless :P I guess that's why it seems that the first thing they teach people is how to avoid them. | @jj [Avoid `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Dan Mašek Oct 12 '16 at 22:56

3 Answers3

1

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0
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).

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
0

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

user4581301
  • 33,082
  • 7
  • 33
  • 54