Jump to content

  • Log In with Google      Sign In   
  • Create Account


Simple Vector list problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Stickmunkee   Members   -  Reputation: 108

Like
0Likes
Like

Posted 07 February 2013 - 12:38 PM

I'm beginning in C++ and am working my way through a book to learn it. I've come across a simple problem I don't know how to fix.

The program is a simple system where you type in the title of a game, which the computer adds to a list and then shows you the list.

The issue is: If you enter a title like "Unreal Tournament" it treats "Unreal" and "Tournament" as separate titles like this:

Unreal

 

Tournament

.

I've tried different ways of using endl and \n in different locations, but that seems to bear no effect.

My question is, still using vectors, how can I get it to list multi-word titles correctly?

 

A separate issue I'm having is my attempt to add an "erase title" function. How can I get the program to search for a title input by the user and then remove that title?

Attached Thumbnails

  • Gamelist Problem.PNG


Sponsor:

#2 King Mir   Members   -  Reputation: 1955

Like
1Likes
Like

Posted 07 February 2013 - 12:43 PM

Instead of using cin >> var, use getline(cin,var).

 

Also, don't intermix the two, as this can lead to some confusing problems.



#3 fastcall22   Crossbones+   -  Reputation: 4255

Like
1Likes
Like

Posted 07 February 2013 - 12:49 PM

The issue is: If you enter a title like "Unreal Tournament" it treats "Unreal" and "Tournament" as separate titles like this:

Use std::getline.

How can I get the program to search for a title input by the user and then remove that title?

Use std::find to search your list, then use std::vector::erase to erase that element from the vector.
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#4 rip-off   Moderators   -  Reputation: 8119

Like
1Likes
Like

Posted 07 February 2013 - 01:41 PM

Code is typically represented in text in popular programming languages. Placing it in an image is not a good idea, because we cannot easily copy and paste the code onto our computers and try it out.

 

You can use [code][/code] tags around your code, which will present it in a nice syntax highlighted box:

#include <iostream>
 
int main() {
    std::cout << "Just like this!" << std::endl;
}


#5 Stickmunkee   Members   -  Reputation: 108

Like
0Likes
Like

Posted 07 February 2013 - 01:56 PM

Thank you, all of this is helpful.

And I will remember to post code as text in the future :)



#6 Chad Smith   Members   -  Reputation: 1108

Like
1Likes
Like

Posted 07 February 2013 - 04:25 PM

I just wanted to expand on the "Use getline instead of "cin>>" answers.  While they're correct I feel it'd be appropriate to expand more on them so you know what is actually happening and why it is.

 

 cin extraction stops reading as soon as if finds any blank space character, so in this case we will be able to get just one word for each extraction. This behavior may or may not be what we want; for example if we want to get a sentence from the user, this extraction operation would not be useful.

In order to get entire lines, we can use the function getline, which is the more recommendable way to get user input with cin:

 

Example:

 

// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
 
 
output:
What's your name? Juan Soulie
Hello Juan Soulie.
What is your favorite team? The Isotopes
I like The Isotopes too!

 

 

using cin>> will leave what was left after the space you typed in the buffer.  So next time it is called it sees something in the buffer and reads it.

 

EDIT: This was taken from here: http://www.cplusplus.com/doc/tutorial/basic_io/

This should give you a good way of how C++ Console I/O works.


Edited by Chad Smith, 07 February 2013 - 04:33 PM.


#7 Stickmunkee   Members   -  Reputation: 108

Like
0Likes
Like

Posted 12 February 2013 - 06:09 PM

Thank you Chad. That explanation is helpful 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS