Thread: Ashamed to ask

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212

    Unhappy Ashamed to ask

    in C you can do this

    scanf("%fx + %fy", &x, &y);
    and on the input you type
    2x + 3y and x and y become 2,3 respectively.

    I tried this in C++

    cin >> x >> "x + " >> y >>"y";

    why won't it work the same?

    kwigibo

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    EDIT........Sorry I had something here but I realized that this is not what you want, I just don't get your question.....but then again I also suck.
    Last edited by incognito; 05-21-2002 at 07:58 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Sorry I might be kind of slow, but I read your post like 5 times and still don't get your point.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Point taken, it is worded badly.

    In C I can use scanf to skip over "expected input" such as the x+..y part. I tried a similar method in C++, which I thought would have the same effect. But it didn't how do I do it.

    run the code below, and it'll show what I mean. use input such as

    2x + 3y
    76x + 9y
    1x + 1y
    etc

    respectively you should get the outputs
    2,3
    76,9
    1,1

    Code:
    #include <stdio.h>
    
    int main() {
        int x,y;
        scanf("%dx + %dy", &x,&y);
        printf("\n\n%d\n%d\n",x,y);
        return 0;
    }

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    just use scanf. or if mixing input streams bothers you, read in a string and use a flavor of scanf to parse it.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Any suggestions on "a flavor of scanf"? and perhaps a small/simple example of its use which I could plugin to a file and run

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's a flavor.
    Code:
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    int main() {
        char line[80];
        int x,y;
        cin.getline(line,80);
        sscanf(line,"%dx + %dy", &x,&y);
        printf("\n\n%d\n%d\n",x,y);
        return 0;
    }
    There's probably a way to do this using the strstream class from the Standard C++ Library. However I don't use strstream enough to know how.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Our initial reactions to 9-11 two years ago.
    By FillYourBrain in forum A Brief History of Cprogramming.com
    Replies: 66
    Last Post: 09-15-2003, 06:23 PM
  2. ashamed to ask..
    By iain in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2002, 09:31 AM