cin.get problems

This is a discussion on cin.get problems within the C++ Programming forums, part of the General Programming Boards category; I have searched the board but cant seem to find my problem sorry if im repeating old problems. I can ...

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    Post cin.get problems

    I have searched the board but cant seem to find my problem sorry if im repeating old problems.

    I can not seem to get the first cin.get(numerator); to get its input, seems to like.. skip it.. i tried clearing it even though the input before hand to me doesnt seem to be getting in the way. The function doesnt have any parameters.
    Code:
    int enterFraction(void)
    {   
        char numerator, denominator;
        
        cout << "\nEnter a fraction in the format A/B: ";
        
        cin.get(numerator);
        cin.ignore(50,'/');
        cin.get(denominator);
        
        // testing variables
        cout << numerator << "/" << denominator << endl;
    }
    thanks for your help.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    From what I remember, you can do this:
    Code:
    int num, den;
    cin.getline(num, '/');
    cin.getline(den);
    And that will get the numerator and denominator. Of course it's been a while, so I'm not 100%.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    -
    Join Date
    Feb 2003
    Posts
    47
    Yeah i cant use getline This is homework given to me.
    Thanks for your help though, i know how the board feels about homework i just need a kick in the right direction this function in relation to the entire homework is very minimal at best.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ok, I tried your code. It seems to work fine, except you are only getting in one char for each number. Try your code with 4/5. Try this:

    Code:
    int enterFraction(void)
    {   
        char numerator[12], denominator[12];
        
        cout << "\nEnter a fraction in the format A/B: ";
        
        cin.get(numerator, 12, '/');
        cin.ignore(50,'/');
        cin.get(denominator, 12);
        
        // testing variables
        cout << numerator << endl << "/"<< endl << denominator << endl;
    }
    You might have problems using them as numbers because the numbers are in string form. I think you can use the function atoi() to get the number from it. Look the function up.
    Last edited by neandrake; 04-22-2004 at 10:44 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    -
    Join Date
    Feb 2003
    Posts
    47
    Yep that works thank you.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by neandrake
    You might have problems using them as numbers because the numbers are in string form. I think you can use the function atoi() to get the number from it. Look the function up.
    Or change the values to
    Code:
    int numerator, denominator;
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Quote Originally Posted by WaltP
    Or change the values to
    Code:
    int numerator, denominator;
    If the extractor operators can be used. Otherwise, get() only works with a char, char*, or streambuf.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 12:58 AM
  4. Problems with cin.get()
    By Stevek in forum C++ Programming
    Replies: 5
    Last Post: 03-15-2003, 12:00 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21