473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Rewriting Program to Use One vector instead of two

Ignore the ??? part at the end.
Basically I have to combine names and values into one vector. How do I do that? Then, how would I pull them both out as two separate parts in order to display (names,values) on the screen?

Expand|Select|Wrap|Line Numbers
  1. Code done in TextPad
  2.  
  3. @d declare variables
  4. @{
  5. vector<string> names;
  6. vector<int> values;
  7. string n;
  8. int v;     // used to store the value entered after conversion
  9. string vs; // used to receive the value and check it
  10. @}
  11.  
  12. Use a while loop to read pairs until user enters {\bf No more}. Store the date into the vectors.
  13. If we wanted to only end loop if the user types {\bf No more} we would need to receive the value as a string, then check its contents. If the string contained any non-digits and was not {\bf No more}, then throw an exception.
  14. @d read name-value pairs from user
  15. @{
  16. // if users non-integer for v, loop ends
  17. while(cin >> n >> vs)
  18. {
  19.   // if vs is equal to "more",
  20.   // then end loop normally.
  21.   // istringstream can be used to convert from
  22.   // string to integer.
  23.   // If the conversation does not succeed,
  24.   // then vs did not contain a valid integer
  25.   // (i.e. some non-digits).
  26.   if( vs == "more" ) break;
  27.   if( vs[0] == '-' ) throw Invalid_Score(); // negative
  28.   for( int i = 0l; i < vs.size() ; ++i )
  29.   {
  30.     if( not isdigit(vs[i]) )
  31.     {
  32.       cerr << "What is this char doing in your integer? "
  33.            << vs[i] << endl;
  34.       throw Invalid_Integer();
  35.     }
  36.   }
  37.   istringstream iss(vs);
  38.   iss >> v;
  39.  
  40.   if( v > 100 ) throw Invalid_Score(); // too large
  41.   names.push_back(n);
  42.   values.push_back(v);
  43.   }
  44. cout << "Pairs read: " << names.size() << " " << endl;
  45. // Write out all the (name,score) pairs, 
  46. // one per line.
  47. for ( int i=0 ; i < names.size(); ++i )
  48.   cout << "(" << names[i] << "," << ??? << ")" << endl;
  49.   // ??? = The scores that correlate with the name
  50.   // Use v[i], vs[i], or something else?
  51.  
  52. @}
Sep 17 '12 #1
1 1619
weaknessforcats
9,208 Expert Mod 8TB
Can you not put a name/value pair into a class? Then use a vector of class.

Or does one name have multiple values?
Sep 18 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: wukexin | last post by:
I try some kind of compiler, my program compile succeeding, but run wrongly. Help me, see my program. If you have free time, please give me some suggestion about process command argument. Thank you...
19
by: Carlo Milanesi | last post by:
Mathematically speaking, a 'vector' is something you can add to another vector and multiply by a number. But in C++, the following code is illegal: std::vector<double> v1(3), v2(3); v1 + v2; //...
9
by: uotani.arisa | last post by:
Hi, Can someone tell me how to declare a pointer to a vector of pointers? I'm just not sure how to do this... I've tried essentially the following: vector<string *> * v; ....
10
by: nandor.sieben | last post by:
Is there a well tested implementation of a sorted vector container library to replace set containers? I need only something very basic: Use lower_bound and insert to add a new element if it's...
10
by: The Cool Giraffe | last post by:
I got a hint recently (guess where, hehe) and was directly pointed to the vector class. Now, i have no issues using that way but i'm concerned about the performance issue. Which is fastest...
15
by: ianweise | last post by:
hello, before i post my code for this, is there anyone out there at this moment? no sense in posting if no one is out there to read and answer it =P
7
by: Jason Heyes | last post by:
Why can't I use the find algorithm to find a program argument in the following way? std::find(argv, argv + argc, "-v") Thanks.
4
by: mathlete | last post by:
Hello, I'm just starting to learn C++, coming from a Java / bit of C background. I have some questions about how vectors of objects and pointers work in C++. Say I want to create a vector of...
23
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.