c++ help

Discussion in 'OT Technology' started by Glennhuricane, Sep 14, 2007.

  1. Glennhuricane

    Glennhuricane New Member

    Joined:
    Apr 2, 2004
    Messages:
    6,070
    Likes Received:
    0
    Location:
    nj
    i'm having a hard time getting started on a project. I have to take input in the form "string int" and then sort the strings into categories depending on the int size. I don't know how to separate the string from the int. After that I was thinking of putting the strings in a queue.
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    if you're using cin or an input filestream, you can simply do this:

    char stringbuffer[128];
    int number;

    cin >> stringbuffer;
    cin >> number;

    As long as the string and int are seperated by whitespace, the whitespace is automatically ignored.
     
  3. Glennhuricane

    Glennhuricane New Member

    Joined:
    Apr 2, 2004
    Messages:
    6,070
    Likes Received:
    0
    Location:
    nj
    thanks, it's been a year and a half since i had a programming class. I'm starting to remember it again.
    I'm just having trouble finding out why this if statement is only true after i enter 2 periods. I want the loop to stop after one period is entered on a line by itself. i'm not asking anyone to do it, I'm just trying to understand why I have to hit '.' twice for it to work. thanks

    Code:
    string name;
    int number;
    bool flag;
    
    do
    {
            cin >> name;
            cin >> number;
    
            if(name == ".")
                flag = true;
                   
    }while(flag == false);
    
     
  4. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    it stops because it's waiting for you to input a value for the second cin statement

    you could see it happen better if your code was something like this:

    Code:
    string name;
    int number;
    bool flag;
    
    do
    {
            cout << "Enter the name:\r\n";
            cin >> name;
            cout << "Enter the number:\r\n";
            cin >> number;
    
            if(name == ".")
                flag = true;
                   
    }while(flag == false);
    
     
  5. Glennhuricane

    Glennhuricane New Member

    Joined:
    Apr 2, 2004
    Messages:
    6,070
    Likes Received:
    0
    Location:
    nj
    thanks. but the only thing is the project is really specific on the input being in the form "string int". i'll try to figure something out or just keep it how it is.
     

Share This Page