#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    Weird kind of input question


    Lets say I had four variables:
    int a, b, c, d;

    I now want the user to input a value for all those variables:
    cout << "What would you like a,b,c,d to be?";

    Is there a command that will assign all those variables the value that the user typed in? I know their is a=b=c=d and many other ways you could do it after the getline or cin. But is there a function that will take what the user types and automatically assign it to all those variables?

    For example, this isn't valid syntax, but this would be the idea:

    int a,b,c,d;
    cout << "What number?";
    getline(cin, a,b,c,d);

    OR...

    cout << "What number?";
    cin >> a,b,c,d

    Thanks in advance.
    -andy
    hmmm...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    I am not good at C++, but from my understanding of streams you should try this:

    cout << "Please enter a b c and d separated by spaces";
    cin >> a;
    cin >> b;
    cin >> c;
    cin >> d;

    untested, as usual, and I take no responsibility if it formats your hard disk or blows up your pc :D
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    doesn't work..tried it before. But thanks anyways. by the way..hirsch you da man!
    hmmm...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    Seems like I still donīt get this cin-cout-concept yet :o
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Code:
       cout << "Please enter a b c and d separated by spaces" << endl;
    
       scanf( "%d %d %d %d", &a, &b, &c, &d );
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    M.Hirsch's suggestion worked fine for me:
    Code:
    #include <iostream.h>
    
    int main(void)
    {
        int a,b,c,d;
    
        cout << "Enter four digits: ";
        cin >> a;
        cin >> b;
        cin >> c;
        cin >> d;
    
        cout << endl << "You entered: " << a << " ";
        cout << b << " " << c << " " << d << endl;
        return 0;
    }
    Compiles and runs the same on Windows (MinGW g++) and Red Hat 7.0. Got the results I expected when I separated the numbers by a space or a new-line, however it got weird if I separated them with commas. I assume there's a way to specify the delimiter.

    Here are the sample runs:

    Windows:
    C:\dcw\PROJECTS\TEST>a
    Enter four digits: 1 2 3 4

    You entered: 1 2 3 4

    C:\dcw\PROJECTS\TEST>a
    Enter four digits: 1,2,3,4

    You entered: 1 7667204 -1 7667200

    C:\dcw\PROJECTS\TEST>a
    Enter four digits: 1
    2
    3
    4

    You entered: 1 2 3 4

    C:\dcw\PROJECTS\TEST>

    Red Hat:
    [wise@pc10593 misc]$ ./a.out
    Enter four digits: 1 2 3 4

    You entered: 1 2 3 4
    [wise@pc10593 misc]$ ./a.out
    Enter four digits: 1,2,3,4

    You entered: 1 134518984 134514385 -1073742648
    [wise@pc10593 misc]$ ./a.out
    Enter four digits: 1
    2
    3
    4

    You entered: 1 2 3 4
    [wise@pc10593 misc]$


    BTW, M.Hirsch, I never could see the reason for iostreams and never use them unless I am forced to. But then I also miss punch cards; they made really great book marks and paper Christmas wreaths.
    Last edited by dwise1_aol; April 11th, 2003 at 02:46 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    M.Hirsch,

    You understand cin/cout fine. I would do it a little differently and combine all the cin's into one:
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    { 
    	int a=0;
    	int b=0;
    	int c=0;
    
    	cout<<"Enter three integers separated by spaces:\n";
    	cin>>a>>b>>c;
    
    	cout<<"You entered: "<<a<<" "<<b<<" "<<c<<endl;
    	return 0;
    }
    However, I think andy3109 wants the user to input only one value and have it assigned to multiple variables, which I don't think the operator >> can do. I don't really see why that's a problem when you can just do this:

    int a, b, c;

    cin>>a;
    b=c=a;

    and it wouldn't make sense to define a cin function to do that because it wouldn't be as efficient.
    Last edited by 7stud; April 11th, 2003 at 05:21 PM.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    yea..i know..i just thought there might be one line that I could type that would perform the task without doing a=b=c (another line). Thanks anways.
    -andy
    hmmm...
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    :( :) :D
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Originally posted by andy3109
    yea..i know..i just thought there might be one line that I could type that would perform the task without doing a=b=c (another line). Thanks anways.
    -andy
    Well, you could sort of cheat and use the comma operator, if you had to :). The following works for me on g++ 3.2 on RedHat Linux 8.0:
    Code:
    #include <iostream>
    using namespace std;
    int main(void) {
      int a,b,c,d;
      cin >> d, a=b=c=d; // Use the comma :)
      cout << a << " " << b << endl;
      return 0;
    }
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    ok..thats actually more of what i am looking for. Thx scorp.
    -andy
    hmmm...
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "ok..thats actually more of what i am looking for."

    Why? Are you in some kind of competition to write a given program using the fewest number of lines?
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Im always looking to make my code shorter, more efficient, take up less memory, etc. Just like many C++ programmers try to do.
    hmmm...

IMN logo majestic logo threadwatch logo seochat tools logo