Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
chapter4 exercise19
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
TianTian  
View profile  
 More options Sep 8 2012, 8:41 pm
From: TianTian <syrttg...@gmail.com>
Date: Sat, 08 Sep 2012 20:41:46 -0700
Local: Sat, Sep 8 2012 11:41 pm
Subject: chapter4 exercise19
Hi everyone, I am learning the chapter4. I meet the exercise 19. My
question is how to write code to add values to two vectors: names and
scores using the statement like while(cin >> temp)? Another question is
how to end loop when meet the "NoName 0"? Thanks!
Tian Tian

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Art Werschulz  
View profile  
 More options Sep 14 2012, 10:16 am
From: Art Werschulz <a...@comcast.net>
Date: Fri, 14 Sep 2012 10:16:38 -0400
Local: Fri, Sep 14 2012 10:16 am
Subject: Re: chapter4 exercise19
Hi.

On Sep 8, 2012, at 11:41 PM, TianTian <syrttg...@gmail.com> wrote:

> Hi everyone, I am learning the chapter4. I meet the exercise 19. My question is how to write code to add values to two vectors: names and scores using the statement like while(cin >> temp)? Another question is how to end loop when meet the "NoName 0"? Thanks!

Since nobody else has responded ...

I don't have the book with me, so I don't know all the particulars of this exercise.int> scores.  Based on your description, the following code does the job:

#include <bjarne/std_lib_facilities.h>

int main()
{
   vector<string> names;
   vector<int> scores;
   cout << "Enter names and scores, terminate by \"NoName 0\":\n";
   string the_name;
   int the_score;
   while (cin >> the_name >> the_score) {
      if (the_name == "NoName" && the_score == 0)
         break;
      names.push_back(the_name);
      scores.push_back(the_score);
   }
   cout << "\nYour input: \n\n";
   for (int i = 0; i < names.size(); i++)
      cout << names[i] << ": " << scores[i] << endl;

}

The while-guard has the added benefit of exiting the loop if there's an eof on cin,  If you don't think that's a benefit, move the cin statement into the loop, and change the loop header to
        while (true) {

Art Werschulz (8-{)}   "Metaphors be with you."  -- bumper sticker
GCS/M (GAT): d? -p+ c++ l u+(-) e--- m* s n+ h f g+ w+ t++ r- y?
Internet: agw STRUDEL comcast.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »