473,320 Members | 1,900 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,320 software developers and data experts.

Vectors in C++

I have an assignment where the program was written for me and we had to change all of the arrays to vectors. When I got the assignment back I didn't pass it off because I needed to populate a particular vector by using the push_back function, but I can't figure it out, so I'm going to post my whole program, but I think this is the only part I need help with.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main ( )
  7. {
  8.     // array of grades to be assigned
  9.     // 91 - 100 is an A
  10.     // 81 -  90 is a B
  11.     // 71 -  80 is a C
  12.     // 61 -  70 is a D
  13.     // Below 61 is an E
  14.     //char grades[] = {'A', 'B', 'C', 'D', 'E'};
  15.     vector<char> grades (5); //['A', 'B', 'C', 'D', 'E'];
  16.  
  17.     // declare a place to hold scores read from a file 
  18.     //int scores[10]={0};
  19.     vector<int> scores (10);
  20.  
  21.     // declare the stream object and open the file
  22.     ifstream theDataFile("c:\\scores.txt");
  23.     if (!theDataFile)
  24.     {
  25.         cout << "\nError opening file.";
  26.         exit(1);
  27.     }
  28.  
  29.     // read the scores into the array
  30.     int index = 0;
  31.     int aScore;
  32.     while(!theDataFile.eof( ))
  33.     {
  34.         theDataFile >> aScore;
  35.         if(!theDataFile.good( )) // the read failed ...
  36.         {
  37.             if (!theDataFile.eof( )) // and it was not an eof condition
  38.             {
  39.                 cout << "\nError reading file.";
  40.                 exit(1);
  41.             }
  42.             break; // it was an eof, so break out of the loop
  43.         }
  44.         // scores[index++] = aScore;
  45.         scores.push_back(aScore);
  46.     }
  47.  
  48.     // print out the values just read and give each a grade
  49.     for (int i = 0; i < index; i++)
  50.     {
  51.  
  52.         cout << scores[i] ;
  53.         if (scores[i] < 61)
  54.             cout << "-E" <<grades[4];  // grade is an 'E'
  55.         else if (scores[i] < 71)
  56.             cout << "-D" << grades[3];  // grade is a 'D'
  57.         else if (scores[i] < 81)
  58.             cout << "-C" << grades[2];  // grade is a 'C'
  59.         else if (scores[i] < 91)
  60.             cout << "-B" << grades[1];  // grade is a 'B'
  61.         else
  62.             cout << "-A" << grades[0];  // grade is an 'A'
  63.         cout << endl;
  64.     }
  65.  
  66.     system("PAUSE");
  67.     return 0;
  68. }
Oct 26 '08 #1
1 956
weaknessforcats
9,208 Expert Mod 8TB
Just create a vector and use push_back to add the grades.

Do not create a vector<char> grades(5) since that creates an array of 5 char and now you can't use push_back to intialize the elements.
Oct 27 '08 #2

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

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.