Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:59:12+00:00 2026-06-05T19:59:12+00:00

The following program expects user input in the mixed fraction format ‘whole_numbernumerator/denominator’ and assigns

  • 0

The following program expects user input in the mixed fraction format ‘whole_numbernumerator/denominator’ and assigns values to respective variables.

#include<iostream>
using namespace std;

int main()
{
    int whole, numerator, denominator;

    cout << "Input format: i<space>n/d" << endl;

    cin >> whole;
    cin.ignore(1000, ' ');
    cin >> numerator;
    cin.ignore(1000, '/');
    cin >> denominator;

    cout << whole << endl;
    cout << numerator << endl;
    cout << denominator << endl;

    return 0;
}

Input1:
123 345/678
Output1:
123
345
678

Input2:
1111111111 1111111111/1111111111
Output2:
1111111111
1111111111
1111111111

Input3:
2222222222 2222222222/222222222
Output3:
2147483647
0
0

I haven’t been able to figure out why the program doesn’t work for Input3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:59:13+00:00Added an answer on June 5, 2026 at 7:59 pm

    You’re overflowing the maximum value for a 32-bit integer (2^31-1 ~= 2.147b). Once this happens, cin doesn’t work properly until you clear the flag. You should be checking for errors, but a short-term solution is to make your number unsigned, or use a 64-bit one, like int64_t. You also don’t need to ignore the space, as cin will skip it by default.

    You can implement something like what’s found here to ensure valid input, but it needs to be tailored to fit your specific input format. Perhaps encapsulating the three into a single type with an overloaded operator that inputs each with respect to the formatting would make the syntax fit more nicely, so you could replace age in the example with a MixedNumber object.

    I would see something like this as a versatile method:

    template <typename T> //any type will work
    void getValidInput (T &var, std::string prompt = "Input: ") {
        while ((std::cout << prompt) && !(std::cin >> var)) { //if cin fails...
            std::cin.clear();                 //clear flag and discard bad input
            std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input; please re-enter.\n"; //let the user know
        } 
    }
    

    Then you could just have your program as follows:

    struct MixedNumber { //a data structure, so it's like using plain variables
        int64_t whole; 
        int64_t numerator;
        int64_t denominator;
    };
    
    std::istream &operator>> (std::istream &in, MixedNumber &num) { //so cin works
        in >> num.whole >> num.numerator;
        in.ignore(); //yes, you could enforce the format a bit more
        in >> num.denominator;
        return in;
    }
    
    int main() {
        MixedNumber num; //easy to "make" a mixed number, a constructor works well too
        getValidInput (num, "Input format: i<space>n/d: "); 
        std::cout << num.whole << '\n' << num.numerator << '\n' << num.denominator;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when compiling my program with GCC I get the following warning: format ‘%d’ expects
The following program has been running for about ~22 hours on two files (txt,
The following program compiles with ifort (version 12) but not with GFortran (up to
Consider following program: static void Main (string[] args) { int i; uint ui; i
In following program . I have one doubt. I have declared one global variable
The following program is a basic linked list divided in 3 classes. In the
The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include
Consider the following program which inserts a range of elements into a vector: vector<string>
I have the following program that loops through all properties of my variable: class
The output of the following program on my machine with ATI Firepro V8750 is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.