The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Weird kind of input question
Discuss Weird kind of input question in the C Programming forum on Dev Shed. Weird kind of input question C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 11th, 2003, 12:04 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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...
|

April 11th, 2003, 12:27 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
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 
|

April 11th, 2003, 12:43 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
doesn't work..tried it before. But thanks anyways. by the way..hirsch you da man!
|

April 11th, 2003, 01:28 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
Seems like I still donīt get this cin-cout-concept yet 
|

April 11th, 2003, 02:31 PM
|
|
|
Code:
cout << "Please enter a b c and d separated by spaces" << endl;
scanf( "%d %d %d %d", &a, &b, &c, &d );
|

April 11th, 2003, 02:43 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

April 11th, 2003, 05:09 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
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.
|

April 12th, 2003, 12:21 AM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
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
|

April 12th, 2003, 12:58 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|

April 12th, 2003, 01:15 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Quote: 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;
}
|

April 12th, 2003, 02:36 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
ok..thats actually more of what i am looking for. Thx scorp.
-andy
|

April 13th, 2003, 03:56 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
"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?
|

April 13th, 2003, 06:58 AM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
Im always looking to make my code shorter, more efficient, take up less memory, etc. Just like many C++ programmers try to do.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|