I'm trying to make a program where you can make a list then manipulate it in several ways. I have this code so far and I'm wondering why the if statements arn't working correctly. When I type create it displays the correct text. Then when I type in an item to be added it just displays the list and then the text again.
//Listing Program
//Add,remove,shuffle and search for items in a user created list
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> list;
vector<string>::iterator iter;
string choice;
string item;
string exit;
cout<< "Welcome to the list making program." <<endl;
cout<< "Type:"<<endl;
cout<< "create to make a list (Do this first)" <<endl;
cout<< "add to add an item to an existing list" <<endl;
cout<< "delete to remove an item from the list" <<endl;
cout<< "shuffle to shuffle the list" <<endl;
cout<< "search to search for an item on the list" <<endl;
cout<< "display to show the current list" <<endl;
cout<< "quit to close the program" <<endl;
cin>> choice;
while(choice!= "quit")
{
if(choice == "create" )
{
cout<< "Press enter after each item, type done to display the list and exit creation." <<endl;
cin>> item;
list.push_back(item);
if(item == "done")
list.pop_back();
for (iter= list.begin(); iter!= list.end(); ++iter)
{
cout<< *iter <<endl;
}
}
}
return 0;
}
Help with C++
http://ideone.com/hlu8f
You should be using code tags so your code is properly displayed on the page.
You prime your while loop but never cin to choice inside of it therefore it will never be able to quit or do anything other than create. You should never use "using namespace std" in global scope as this will import all the symbols used in the std namespace and name conflicts can occur. Your item string should be declared locally inside of the loop this way it is always constructed and destructed at the end of the scope. It seems as though you tried to reduce your code for a smaller paste size but idk.
You should be using code tags so your code is properly displayed on the page.
You prime your while loop but never cin to choice inside of it therefore it will never be able to quit or do anything other than create. You should never use "using namespace std" in global scope as this will import all the symbols used in the std namespace and name conflicts can occur. Your item string should be declared locally inside of the loop this way it is always constructed and destructed at the end of the scope. It seems as though you tried to reduce your code for a smaller paste size but idk.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement