Thread: Heeeelp!

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    1

    Heeeelp!

    I'm writing a program for my C++ class that receives input and displays the fraction input. I can't figure out for the life of me why it is not working. I've got a syntax error somewhere. Please help a brotha out.

    Code:
    #include <iosotream>
    using namespace std
    
    void getFrac(int& , int&);
    
    int main()
    {
        int num,        // input - fraction numerator
            denom;      // input - fraction denominator
            
        cout <<  "Enter a common fraction "
             <<  "as 2 integers seperated by a slash: ";
             
             getFrac(num, denom);
             
             cout << "Fraction is " << numerator
                  << " / " << denomerator << endl;
                  
                  system("pause");
                  
             return 0;
    
    }
    // Reads a fraction.
    // Pre: none
    // Post: Returns fraction numerator through numerator
    //         Returns fraction denominator through denominator 
    void getFrac(int& numerator,     // OUT
                 int& denominator)   // OUT
    {
      char slash;   // temporary storage for slash
      cin >> numerator >> slash >> denominator;
    }
    Last edited by Dave_Sinkula; 07-21-2006 at 09:59 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Typos?
    Code:
    #include <iostream>
    using namespace std;
    //...
             cout << "Fraction is " << num
                  << " / " << denom << endl;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Also, why do you need a function to get the num and denom? Requirement? the:

    Code:
    cin >> numerator >> slash >> denominator;
    will require a key press of return after each variable, but what I would do is read the thing into a string, like so

    Code:
    string Fraction;
    cin >> Fraction;
    Then I would get two substrings of it, one just before the '/', and one just after, and convert them to integers. This may be a little more advanced than what's needed for your assignment, but that's what I'd do anyways.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by twomers
    Also, why do you need a function to get the num and denom?
    That's a valid question. The function's body is just a line of code.

    the:

    Code:
    cin >> numerator >> slash >> denominator;
    will require a key press of return after each variable
    This on the other hand is just wrong.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heeeelp
    By bzs1 in forum C Programming
    Replies: 5
    Last Post: 10-02-2008, 12:17 PM
  2. HEEEELP: Letters to ASCII decimal equivalents?
    By Mazerius in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2002, 09:56 AM
  3. Heeeelp.. Question...
    By NANO in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2002, 06:06 PM
  4. salary program heeeelp!
    By pancho in forum C Programming
    Replies: 6
    Last Post: 02-02-2002, 08:56 PM