Jump to content
  • Advertisement
Sign in to follow this  
Agreenknight

beginning c++ question

This topic is 4857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using dev cpp compiler. What I'm trying to do is write a program that will allow me to add, view and remove game titles. I don't think I have the vectors down. #include <iostream using namespace std; int main() { vector<string> title list; cout << "1. Add a title."\n; cout << "2. See list."\n; cout << "3. remove a title."\n vector<string>::iterator myIterator; vector<string>::const_iterator iter; int choice; cout << "Choice: "; cin >> choice; switch (choice) ( case 1: cout <<"Would you like to add a title to the list?." cout <<"y for yes and n for no:\" cin >> title list; title list.insert(title list.begin()); case 2: cout <<"Your game titles:\" for (iter = inventory.begin(); iter != inventory.end(); ++iter) cout << *iter << endl; case 3: cout <<"Which title would you like to remove?" cin << title list title list.pop_back(); }

Share this post


Link to post
Share on other sites
Advertisement
Foremost you need to end each case with a break; statement otherwise each case will trigger the following cases as well. Furthermore, you cannot have spaces in identifier names (such as title list) and your program needs more logic (like finding the right title, taking into account the user's response yes/no, etc).

Illco

Share this post


Link to post
Share on other sites
you didnt explain your problem...

okay, i noticed some things.
Quote:

#include <iostream

you didnt close the #include

#include <iostream>
Quote:

vector<string> title list;

you cant have an identifier with a space in it

Quote:

cout << "1. Add a title."\n;


the \n should be inside the quotes.

i personally prefer

cout << "1. Add a title." << endl;

endl is like cout, its predefined for you.

Quote:

switch (choice)
(
case 1:
cout <<"Would you like to add a title to the list?."
cout <<"y for yes and n for no:\"
cin >> title list;
title list.insert(title list.begin());

case 2:
cout <<"Your game titles:\"
for (iter = inventory.begin(); iter != inventory.end(); ++iter)
cout << *iter << endl;

case 3:
cout <<"Which title would you like to remove?"
cin << title list
title list.pop_back();


switch statemnets are like so

switch( variable )
{ // these kind of brakets!
case 1:
//code!

break; // this is very important.

}// you left out a terminating bracket for your switch



the breaks have to be there.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
the \n should be inside the quotes.

i personally prefer

cout << "1. Add a title." << endl;

endl is like cout, its predefined for you.


I'm going to have to disagree on that one. endl has additional functionality that you really shouldn't be using unless you need it. endl in fact appends a newline to the stream and then flushes the output buffer, which overrides the default behavior of waiting for the buffer to fill. This flush does slow down the process if you happen to be printing on the order of several million lines.

Not an issue in this case, but still something to be aware of.

Share this post


Link to post
Share on other sites
Quote:
Original post by tychon
Quote:
Original post by rip-off
the \n should be inside the quotes.

i personally prefer

cout << "1. Add a title." << endl;

endl is like cout, its predefined for you.


I'm going to have to disagree on that one. endl has additional functionality that you really shouldn't be using unless you need it. endl in fact appends a newline to the stream and then flushes the output buffer, which overrides the default behavior of waiting for the buffer to fill. This flush does slow down the process if you happen to be printing on the order of several million lines.

Not an issue in this case, but still something to be aware of.


i agree, but in this case there wont be a difference. i normally dont se "using namespace std;" so i tend to either do "using std::endl;" or "extern char endl;"
char endl = '\n';

i kindof forgot that i even do that any more...

Share this post


Link to post
Share on other sites
Thank you to all that applied especially rip-off, anyway.
#include <iostream>

using namespace std;
int main()
{
vector<string> titlelist;

cout << "1. Add a title."\n;
cout << "2. See list."\n;
cout << "3. remove a title."\n

vector<string>::iterator myIterator;
vector<string>::const_iterator iter;

int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
(
case 1:
cout <<"Would you like to add a title to the list?."
cout <<"y for yes and n for no:\"
cin >> titlelist;
titlelist.insert(titlelist.begin());

case 2:
cout <<"Your game titles:\"
for (iter = titlelist.begin(); iter != titlelist.end(); ++iter)
cout << *iter << endl;

case 3:
cout <<"Which title would you like to remove?"
cin << title list
titlelist.pop_back();

break;
}
}
Errors
line 6 '
line 6 Each
line 6 parse
line 8 stray
line 8 parse
line 9 stray
line 9 parse
line 10 stray
line 10 parse
line 13 parse

Well, I think you guys get the point. What am I missing here? I'm sure I've probably left something out of the program.




Share this post


Link to post
Share on other sites
As has already been posted, you need to include the std header for vector in order to use it. I also notice you're not including the std header for string either. Add these to the top of your program:

#include <vector>
#include <string>


Also, to post in a fixed-font, place your code between [ code ] tags [ /code ] (without spaces). To get a source box - with syntax highlighting - place your code between [ source ] tags [ /source ] (without the spaces).

Additionally, your switch statement syntax is wrong. See rip-off's post, and look here for a more thorough explaination.

Share this post


Link to post
Share on other sites
First thing: your '\n' characters are still outside the quotes in several places.
Another thing: a switch block is opened with {, not (.
Yet another thing: you are forgetting the 'n' after the '\' in \n.
You also have to put a 'break' after EVERY case statement.

edit: Darn. Beaten...

Share this post


Link to post
Share on other sites
Also, post (and for that matter, read) the whole error messages. They normally tell you exactly what the problem is, given a little effort (and perhaps a dictionary).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!