|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
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
|
|||
|
|||
|
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 ![]()
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
||||
|
||||
|
doesn't work..tried it before. But thanks anyways. by the way..hirsch you da man!
|
|
#4
|
|||
|
|||
|
Seems like I still donīt get this cin-cout-concept yet
![]() |
|
#5
|
|||
|
|||
|
Code:
cout << "Please enter a b c and d separated by spaces" << endl; scanf( "%d %d %d %d", &a, &b, &c, &d ); |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
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 |
|
#9
|
|||
|
|||
![]() |
|
#10
|
||||
|
||||
|
Quote:
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;
}
|
|
#11
|
||||
|
||||
|
ok..thats actually more of what i am looking for. Thx scorp.
-andy |
|
#12
|
|||
|
|||
|
"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? |
|
#13
|
||||
|
||||
|
Im always looking to make my code shorter, more efficient, take up less memory, etc. Just like many C++ programmers try to do.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Weird kind of input question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|