C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 11th, 2003, 12:04 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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...

Reply With Quote
  #2  
Old April 11th, 2003, 12:27 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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.

Reply With Quote
  #3  
Old April 11th, 2003, 12:43 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
doesn't work..tried it before. But thanks anyways. by the way..hirsch you da man!

Reply With Quote
  #4  
Old April 11th, 2003, 01:28 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
Seems like I still donīt get this cin-cout-concept yet

Reply With Quote
  #5  
Old April 11th, 2003, 02:31 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 18 h 58 m
Reputation Power: 12
Code:
   cout << "Please enter a b c and d separated by spaces" << endl;

   scanf( "%d %d %d %d", &a, &b, &c, &d );

Reply With Quote
  #6  
Old April 11th, 2003, 02:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
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.

Reply With Quote
  #7  
Old April 11th, 2003, 05:09 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #8  
Old April 12th, 2003, 12:21 AM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to 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

Reply With Quote
  #9  
Old April 12th, 2003, 12:58 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184

Reply With Quote
  #10  
Old April 12th, 2003, 01:15 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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;
}

Reply With Quote
  #11  
Old April 12th, 2003, 02:36 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
ok..thats actually more of what i am looking for. Thx scorp.
-andy

Reply With Quote
  #12  
Old April 13th, 2003, 03:56 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"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?

Reply With Quote
  #13  
Old April 13th, 2003, 06:58 AM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
Im always looking to make my code shorter, more efficient, take up less memory, etc. Just like many C++ programmers try to do.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Weird kind of input question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump