Thread: cout fraction display

  1. #1
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35

    Cool cout fraction display

    Hello again all knowing and all seeing cprogramming peoples. I have a quick question. I would like to display or print out on the screen a fraction. I would be most happy if it looked like this:

    3/4


    I figure it shouldnt be to hard but im an 'intro' programmer with very little skills. Below is the code i wrote that got around the problem but I was wondering if I could achieve the above display in c++. If it cannot then just call me a pirate and i will scurvy outta here but if im wrong, like i usally am with these things, i would appreciate any help in the right direction. Thanks ahead

    Code:
    //get inputs for first fraction
    	cout<<"enter in the first numerator:";
    	cin>>num1;
    	
    	cout<<"Enter in the first denominator:";
    	cin>>den1;
    
    	cout<<"You entered in "<< num1 <<"/"<< den1 <<" as your first fraction"<<endl;
    My idea that has acheived notta:
    Code:
    //get inputs for first fraction
          cout<<"Enter in the first fraction:"<<num1<<"/"<<den2<<endl;
    Logically it doesnt work cause 0/0 is what comes out. but im almost certain this is waaaaay to long of a post about such a simple task. My research has netted my a whole lotta talk about class and public which is two things i tend to vere away from

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    You are trying to go for a masked input. That might be kind of tricky to get right in a console program. You could always read the fraction in as a string, then parse and convert to int. Something like this should do it...
    Code:
    char inbuf[255];  // using C-Style string handling
    cout << "Enter a fraction: ";
    cin >> inbuf;
    char* p = strchr(inbuf, '/');  // get pointer to the slash
    *p = 0;  // terminate the numerator part
    int num = atoi(inbuf);  // convert numerator
    int den = atoi(p+1);  // convert denominator
    be careful... I've added no error checking.

  3. #3
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    looks interesting. a bit to advanced for me right now as i have not really began studying C-Style handling yet. Thank you for your help though and i will keep this line of code in mind once i do start learning about it.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Wait a minute... that answer I gave you overlooked a good standard library function... this is a perfect situation to use sscanf...

    I am sure that by now you have been told that scanf is bad and you should avoid it. Well, it can be bad if it is not used well... but there is one thing it is very good for... masked input. It is good practice to first read input into a string then use sscanf to parse the string. Check this one out...

    Code:
    #include <cstdio>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char buf[255];
    	int numer=0, denom=0;
    
    	cout << "Enter a fraction: ";
    	cin >> buf;
    
    	sscanf(buf, "%d/%d", &numer, &denom);
    	cout << "Numerator: " << numer << " Denominator: " << denom << endl;
    
    	return 0;
    }

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why don't you just try something like this:
    Code:
    #include<iostream>
    
    int main()
    {
         int numer,denom;
         char nullch;
    
         std::cin>>numer>>nullch>>denom;
         std::cout<<"Numerator: "<<numer<<"\nDenominator: "<<denom<<std::endl;
    
         return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    very interesting.. Im currently in Intro C++ and im learning that there seems to be a personal vision to the individual coder when it comes to developing a program. There seems to be no wrong way of writing out code as long as the desired output is achieved. I finished my version yesterday and although it is a bit longer than the ones you fellow c++ ninjas wrote it still displayed quite nicely.(least i think so )

    so with out further ado i give you my crowing achievment .....drum roll please.....

    Code:
    cout<<"Enter in the First Numerator:           1 ";
    	cin>>num1;
    	cout<<"                              -----"<<endl;
    	cout<<"Enter in the First Denominator: 2";
    	cin>>den1;

    ya ya ya i know it looks super cheesy and im sure i did some cave man coding but i hope you guys got a good chuckle out of it like my teacher will definitley get but hey. If it works it works. hopefully soon i can understand the std:: and scannf stuff but i just started 14 weeks ago lol. good day all and i hope to be a productive member of this community someday

    oh and i would like to thank both of you for taking the time to reply

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Quote Originally Posted by major_small
    why don't you just try something like this:
    Code:
         std::cin>>numer>>nullch>>denom;

    OK... I knew there were ways to do it using the iostream, I just didn't know what they were. That's interesting... just disregard a character in the input.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    the way I would do it if I didn't want to declare a char just to get rid of something:

    Code:
    std::cin>>numer;
    std::cin.ignore();
    std::cin>>denom;
    @CheyenneWay : the whole std:: thing is teh standard... you'll learn it soon enough... if you don't, you're not following the standards... it's fine because you just need one extra line of code to make it all standards-compliant... this is what the code would look like with the one line:
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
         int numer,denom;
         char nullch;
    
         cin>>numer>>nullch>>denom;
         cout<<"Numerator: "<<numer<<"\nDenominator: "<<denom<<endl;
    
         return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help, cannot get char to display A+,F-, etc.
    By slickwilly440 in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2011, 02:06 AM
  2. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  3. My text doesn't display
    By joeprogrammer in forum Game Programming
    Replies: 11
    Last Post: 02-23-2006, 10:01 PM
  4. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM
  5. FAQ: Cout and textcolor question...
    By Laniston in forum FAQ Board
    Replies: 2
    Last Post: 12-19-2002, 10:54 AM