C/C++ Solve C++ Vector Question: #include, for Loop, getline(), vector.pop_back()

  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ Vector
AI Thread Summary
The discussion revolves around a C++ program that prompts users to input names and then eliminates a specified name from the list before displaying the remaining names in reverse order. Key issues identified include the incorrect initialization of the vector, which should have a fixed size of 99, and the need for proper handling of input to avoid skipping characters. The program also needs to check if the name to be eliminated is present in the list and handle case sensitivity to ensure accurate comparisons. Finally, the user is guided to modify their approach to use arrays instead of vectors as per instructor requirements, while also addressing the output of a message if the specified name is not found. The conversation emphasizes the importance of robust input handling and correct data structure usage in C++.
FallArk
Messages
127
Reaction score
0
I ran into some trouble with a C++ question. I need to make a program that will run like this:
Example run 1:
How many names do you want (max 99)? 5
Enter name #1: Adam Apple
Enter name #2: Betty Boop
Enter name #3: Charles Chaplin
Enter name #4: Debbie Dali
Enter name #5: Elaine Eggbert
What name do you want to eliminate? Adam Apple
Here is the list in reverse order, skipping Adam Apple...
Elaine Eggbert
Debbie Dali
Charles Chaplin
Betty Boop
ER]​

This is what I came up with :
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    int x = 0;
    int i = 0;
    vector<string> userName(x);
  cout << "How many names do you want (max 99) ? ";
  cin >> x;
  if (x > 99) {
      cout << "Out of memory!";
      return 0;
  }
  for (i = 0; i < x; ++i) {
      cout << "Enter name #" << i + 1 << ": ";
      cin.ignore();
      getline(cin,userName.at(i));
  }
}
I was trying to use a for loop to ask for inputs and then assign them to the vector. Then i can use the vectr.pop_back() command to eliminate values. But the program just end itself after displaying Enter name #1:. I'm not sure whether the getline is not used correctly or i messed up setting the vectors.
Thanks for the great help so far!
 
Technology news on Phys.org
The main problem with your code is that the vector userName is declared to have 0 components, basically then a useless vector. Even though the vector class is "dynamic", it is up to the programmer to make sure that a vector has enough components for its intended use. So replace the declaration with:
vector userName(99);

Next, I/O in C++ (as in other languages) can be tricky. Class cin's method getline reads all the characters on a line of input, including the terminating return character. However, this return character is not put into the target string. So when you put ignore into your loop, it will ignore the first character of the name typed by the user. However, when cin inputs the integer x, the terminating return character is not read. So before starting your loop to read the names, you want to get rid of this return char. Do this with an ignore(). If you don't do this, the first name you read will be just the return character, resulting in an empty string!

Here's what you wrote, but I tweaked it just a bit:

Code:
   int x, i;
   vector<string> userName(99);
   cout << "How many names do you want (max 99) ? ";
   cin >> x;
   if (x > 99) {
      cout << "Out of memory!";
      return 0;
   }
   cin.ignore();
   for (i = 0; i < x; ++i) {
      cout << "Enter name #" << i + 1 << ":";
      //      cin.ignore(); this would strip off the 1st char of name
      getline(cin, userName.at(i));
   }
   cout << "What name to ignore?";
   string skip;
   getline(cin, skip);
   /* Now use a for loop starting at x-1 that decrements to 0.
       Output the component only if it is != skip
   */

Finally, the above is not very "robust". The user can get unexpected results. For example, if she types the name Ali, but then types the name to be skipped as ali, Ali does not get skipped in the output. You can try and make the program better by using string methods and trying to think of all (stupid) things a user can do.
 
Last edited:
johng said:
The main problem with your code is that the vector userName is declared to have 0 components, basically then a useless vector. Even though the vector class is "dynamic", it is up to the programmer to make sure that a vector has enough components for its intended use. So replace the declaration with:
vector userName(99);

Next, I/O in C++ (as in other languages) can be tricky. Class cin's method getline reads all the characters on a line of input, including the terminating return character. However, this return character is not put into the target string. So when you put ignore into your loop, it will ignore the first character of the name typed by the user. However, when cin inputs the integer x, the terminating return character is not read. So before starting your loop to read the names, you want to get rid of this return char. Do this with an ignore(). If you don't do this, the first name you read will be just the return character, resulting in an empty string!

Here's what you wrote, but I tweaked it just a bit:

Code:
   int x, i;
   vector<string> userName(99);
   cout << "How many names do you want (max 99) ? ";
   cin >> x;
   if (x > 99) {
      cout << "Out of memory!";
      return 0;
   }
   cin.ignore();
   for (i = 0; i < x; ++i) {
      cout << "Enter name #" << i + 1 << ":";
      //      cin.ignore(); this would strip off the 1st char of name
      getline(cin, userName.at(i));
   }
   cout << "What name to ignore?";
   string skip;
   getline(cin, skip);
   /* Now use a for loop starting at x-1 that decrements to 0.
       Output the component only if it is != skip
   */

Finally, the above is not very "robust". The user can get unexpected results. For example, if she types the name Ali, but then types the name to be skipped as ali, Ali does not get skipped in the output. You can try and make the program better by using string methods and trying to think of all (stupid) things a user can do.

This is what I did:
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int x = 0;
int i = 0;
vector<string> userName(99);
string userSkip;
cout << "How many names do you want (max 99) ? ";
cin >> x;
if (x > 99) {
    cout << "Out of memory!";
    return 0;
}
for (i = 0; i < x; ++i) {
    cout << "Enter name #" << i + 1 << ": ";
    cin.ignore();
    getline(cin,userName.at(i));
}
cout << "What name do you want to eliminate? ";
getline(cin,userSkip);
cout << "Here is the list in reverse order, skipping " << userSkip << "...";
for (i = x - 1; i > 0; --i){
        /* for some reason, the userName.at(i) will not run, did i do something wrong? this for loop is confusing. */
    cout << userName.at(i) << endl;
    if (userSkip == userName.at(i)) {
        userName.at(i) = ""; // I don't know how to get rid of a value
    }
    else {
        cout << "The name [" << userSkip << "] was not encountered.";
    }
}
}

I need to get rid of values "userSkip" but i don't really know how, and the for loop is not executing properly.
Thanks for the help!
 
First, you ignored (not on purpose, I hope) my previous correction to your code that inputs the list of names. Again, as you wrote the input loop, every name after the first has its first letter removed!

Next, the original specification said nothing about deleting name userSkip from vector userName. However, if you want to do so, the following program does this. I recommend that you study this complete correct program. If you have questions about it, post again.

Code:
#include <iostream>
#include <vector>
#include <cctype>
#include <string>
using namespace std;

int main() {
   int x = 0;
   int i = 0;
   vector<string> userName(99);
   string userSkip;
   cout << "How many names do you want (max 99) ? ";
   cin >> x;
   if (x > 99) {
      cout << "Out of memory!";
      return 0;
   }
   cin.ignore();
   for (i = 0; i < x; ++i) {
      cout << "Enter name #" << i + 1 << ": ";
      getline(cin, userName.at(i));
   }
   cout << "What name do you want to eliminate? ";
   getline(cin, userSkip);
   cout << "Here is the list in reverse order, skipping " << userSkip << "...\n";
   int skipIndex = -1;
   for (i = x - 1; i >= 0; --i) {
      if (userSkip != userName.at(i)) {
         cout << userName.at(i) << endl;
      } else {
         skipIndex = i;
      }
   }
   if (skipIndex == -1) {
      cout << userSkip << " not in the list\n";
   } else { // physically remove name userSkip from the vector
      for (i = skipIndex; i < x - 1; i++) {
         userName.at(i) = userName.at(i + 1);
      }
      userName.resize(x - 1);
      // now verify userSkip was deleted from vector userName:
      cout << "List with " << userSkip << " deleted:\n";
      for (i = 0; i < userName.size(); i++) {
         cout << userName.at(i) << endl;
      }
   }
   return(0);
}
 
Whoops! Didn't see the comment on that loop.
One more question: Is there a way to output the list in reverse (I know how to do this) and if the user entered something that is not inside the vector, the program will output "This **** is not encountered" ?
update: I changed vector to array, I think that's what my instructor wants us to do.
This is what I have so far:
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
int x = 0;
int i = 0;
string userName[99];
string userSkip;
cout << "How many names do you want (max 99) ? ";
cin >> x;
if (x > 99) {
    cout << "Out of memory!";
    return 0;
}
cin.ignore();
for (i = 0; i < x; ++i) {
    cout << "Enter name #" << i + 1 << ": ";
    getline(cin,userName[i]);
}
cout << "What name do you want to eliminate? ";
getline(cin,userSkip);
cout << "Here is the list in reverse order, skipping " << userSkip << "..." << endl;

for (i = x - 1; i >= 0; --i) {
     if (userSkip != userName[i]) {
         cout << userName[i] << endl; // this is where I am confused.
      }
cout << "The name [" << userSkip << "] was not encountered.";
}

}
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
8
Views
4K
Replies
5
Views
3K
Replies
39
Views
4K
Replies
22
Views
3K
Replies
65
Views
6K
Back
Top