7 Replies - 710 Views - Last Post: 25 February 2013 - 06:39 AM Rate Topic: -----

#1 El Zorro Gris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 13

How to Receive integer and character inputs in one line?

Posted 24 February 2013 - 08:34 PM

I'm writing a program where I need to get the names and bowling scores of five players and sort them in order and output the top three. For now, I need to know how to get someones name and their score on one cin line and keep those values for future sorting.
For example, in an ideal world it would be
int score1,score2,score3,score4,score5;
string name1,name2,name3,name4,name5;

cout<<"Enter name and score\n";
cin>> name1 >>score1;


blahblahblah

but that doesn't work. Sooo any tips?

By the way, sorry I screwed up the code tags, I'm in a hurry

This post has been edited by GunnerInc: 24 February 2013 - 08:35 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How to Receive integer and character inputs in one line?

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: How to Receive integer and character inputs in one line?

Posted 24 February 2013 - 08:47 PM

Well, you could always use an array for the same and add each score to different indexes of the array.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: How to Receive integer and character inputs in one line?

Posted 24 February 2013 - 08:48 PM

C style structs enable you to do this i.e. mix primitive types within a general type which is the struct name. Are you familiar with structs? If not you could get the name and score as a string with getline() and store them in an array or vector of strings. The hard bit is separating the string scores from the names and converting them into integers if this is required.
Was This Post Helpful? 0
  • +
  • -

#4 michael072  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 100
  • Joined: 08-February 11

Re: How to Receive integer and character inputs in one line?

Posted 24 February 2013 - 10:00 PM

int score1,score2,score3,score4,score5;
string name1,name2,name3,name4,name5;

cout<<"Enter name and score\n";
cin >> name1;
cin >> score1;



Was This Post Helpful? 0
  • +
  • -

#5 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 272
  • Joined: 27-July 12

Re: How to Receive integer and character inputs in one line?

Posted 24 February 2013 - 10:14 PM

You can create a struct like this
struct player
{
int score;
string name;
}; // this has to be outside of any function body



Afterwards , you can use player as you use any data type (i.e. int, char, double, etc)

so you could create an array of players like this

player arrayPlayers[NUMBER];


Afterwards all you have to do is to read and store the data . You can access a speficic field of a struct by using '.' so to read the data for all the players you could do something like

for (int i = 0 ; i < NUMBER ; i++)
{
   cout << "Enter name: " ; cin >> arrayPlayers[i].name ;
   cout << "Enter score: " ; cin >> arrayPlayers[i].score ;
} 


So that should be it :) Also, you can get more info on Data Structures here.

This post has been edited by TwoOfDiamonds: 24 February 2013 - 10:15 PM

Was This Post Helpful? 1
  • +
  • -

#6 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: How to Receive integer and character inputs in one line?

Posted 24 February 2013 - 11:04 PM

Ignore this post. Mistake.

This post has been edited by IngeniousHax: 24 February 2013 - 11:05 PM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,684
  • Joined: 16-October 07

Re: How to Receive integer and character inputs in one line?

Posted 25 February 2013 - 05:16 AM

What you have is correct. Here's a quick and dirty test program:
#include <iostream>

using namespace std;

int main() {
	int score1,score2,score3,score4,score5;
	string name1,name2,name3,name4,name5;

	cout<<"Enter name and score\n";
	cin >> name1 >> score1;

	cout << "You entered: \n"
		<< "Name: " << name1 << "\n"
		<< "Score: " << score1 << "\n";

	return 0;
}



Now, let's test it:
[baavgai@DIC]$ ./test 
Enter name and score
John 123
You entered: 
Name: John
Score: 123
[baavgai@DIC]$ ./test 
Enter name and score
John Smith 123
You entered: 
Name: John
Score: 0
[baavgai@DIC]$



What this shows is that string only scoops up a single word with the default cin implementation.

If you want strings with spaces, use getline instead would be better.

Now, for what you're doing, look at the suggestions to use a struct. Also an array. If the instructions are to use neither, at least consider a function to consistently get input from the user.

e.g.
void loadNameScore(string &name, int &score);

int main() {
	int score1,score2,score3,score4,score5;
	string name1,name2,name3,name4,name5;
	
	loadNameScore(name1, score1);
	loadNameScore(name2, score2);



Even if you use an array, a loop, a struct, whatever, putting that kind of functionality in a function is still a good idea.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#8 El Zorro Gris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-February 13

Re: How to Receive integer and character inputs in one line?

Posted 25 February 2013 - 06:39 AM

Thanks all for the info. on structs, I'll research this and hopefully have a working program by the end of today.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1