how to show the user the list of book from which the user want and....

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int numchosen, bid=0, numbc=0, i=0, m, math_starting_number=6, m2=1, totalmath=0, num;
string booktitles[50]="No Book";
string mathematics[50];
mathematics[1]="Math1";
mathematics[2]="Math2";
mathematics[3]="Math3";
mathematics[4]="Math4";
mathematics[5]="Math5";
mathematics[6]="Return";
char name[20];
cout<<"Please Enter Your Name: "<<endl<<endl;
cin.getline(name,50);
system("cls");
loop:
do
{
cout<<"Welcome to My Personal Library "<<name<<endl<<endl;
cout<<"Please Select Book Classification: "<<endl<<endl;
cout<<"1. Mathematics"<<endl<<endl;
cout<<"2. Physics"<<endl<<endl;
cout<<"3. Chemistry"<<endl<<endl;
cout<<"4. Programming C++"<<endl<<endl;
cout<<"5. Exit"<<endl<<endl;
cout<<"Total Books Chosen: "<<i<<endl<<endl;
cout<<"No. " <<" Book Titles "<<endl;
for (int num=1;num<=i;num++)
{
cout<<num<<" "<<booktitles[i]<<endl;
}
cin>>numchosen;
switch (numchosen)
{
case 1:
{
system("cls");
cout<<"Choose Mathematics books from The List below: "<<endl<<endl;
for(int m=1;m<=math_starting_number;m++)
{
cout<<m<<" "<<mathematics[m]<<endl;
}
cout<<endl;
cin>>numbc;
switch (numbc)
{
case 1:
{
i++;
math_starting_number--;
booktitles[i]="math1";
m2++;
mathematics[1]=mathematics[m2];
mathematics[2]=mathematics[3];
mathematics[3]=mathematics[4];
mathematics[4]=mathematics[5];
mathematics[5]=mathematics[6];
break;
}
case 2:
{
i++;
booktitles[i]="math2";
math_starting_number--;
m2++;
mathematics[2]=mathematics[3];
mathematics[3]=mathematics[4];
mathematics[4]=mathematics[5];
mathematics[5]=mathematics[6];
break;
}
case 3:
{
i++;
booktitles[i]="math3";
math_starting_number--;
m2++;
mathematics[3]=mathematics[4];
mathematics[4]=mathematics[5];
mathematics[5]=mathematics[6];
break;
}
case 4:
{
i++;
booktitles[i]="math4";
math_starting_number--;
m2++;
mathematics[m2]=mathematics[1];
mathematics[4]=mathematics[5];
mathematics[5]=mathematics[6];
mathematics[4]=mathematics[5];
mathematics[5]=mathematics[6];

break;
}
case 5:
{
i++;
booktitles[i]="math5";
booktitles[i]="math3";
math_starting_number--;
m2++;
mathematics[m2];
mathematics[m2]=mathematics[4];
mathematics[1]=mathematics[m2];
mathematics[2]=mathematics[3];
mathematics[3]=mathematics[4];
mathematics[4]=mathematics[5];
mathematics[5]=mathematics[6];
break;
}
case 6:
{
system("cls");
goto loop;
break;
}

}
break;


}
case 2:
{
system("cls");
cout<<"Choose Physics books from The List below: "<<endl<<endl;
cout<<"1.Physics 1"<<endl<<endl;
cout<<"2.Physics 2"<<endl<<endl;
cout<<"3.Physics 3"<<endl<<endl;
cout<<"4.Physics 4"<<endl<<endl;
cout<<"5.Physics 5"<<endl<<endl;
cout<<"6.Return"<<endl<<endl;
cin>>numbc;
switch (numbc)
{
case 1:
{
i++;
booktitles[i]="physic1";

break;
}
case 2:
{
i++;
booktitles[i]="physic 2";

break;
}
case 3:
{
i++;
booktitles[i]="physic 3";
break;
}
case 4:
{
i++;
booktitles[i]="physic 4";
break;
}

case 6:
{
system("cls");
goto loop;
break;
}
}
break;
break;
}
case 5:
{
return 0;
break;
}
}

system("cls");

}while (numchosen<5);
cout<<"Please reenter the book you want to choose:"<<endl<<endl;
goto loop;
system("PAUSE");
return EXIT_SUCCESS;
}


half way done.. my problem is how to show the user the list of book from which the user want and the book list show again after the user choose another book..
p/s book chosen cannot be display anymore...

any idea about it?

due the characters are over 9000, therefore i have skipped the case 5,6, just help me think about the mathematics.. when i choose Math1, the book title showed math1(that's right), but when i choose math2 for next choice, the book titles will show no1. math2..... no2. math2... the previous math1 are replaced by math2...
1
2
3
4
for (int num=1;num<=i;num++)
{
cout<<num<<" "<<booktitles[i]<<endl;
}


Well, wouldn't that display the name of same book again and again ?
yes.. i'm beginner... i don't know how to solve this problem.. do you have any idea? need some help here...
Error: you're not #including <string>, which you need to use C++ strings, which you are using.

For your problem, I recommend creating a vector of book names. Have an infinite while loop that terminates when you choose the option to show something else than that section (do not use goto, it's dangerous). When you need to delete a book from the list, call vector.erase(vector.begin() + selection - 1) (assuming selection cannot be zero). Here's a few snippets to better understand vectors:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>
//You'll need to do that at the top of your file to use vectors. Note that vectors are part of the namespace std.
vector<type> name; //Creates a vector of type... type. 
//A vector is really not much more than a glorified array with functions that can change its own size.
name.at(int array-like_position); //Fetches an element from the vector. 
//This is a vector equivalent of an array's [] except that it's safer. 
//(if you go out of bounds using .at(), you get an error, this may or may not happen with an array).
name.push_back(type to_append); //Appends an element to a vector, expanding the vector as needed. 
//Nifty, you'll want to use this all the time.
name.erase(name.begin() + unsigned int offset); //Erases the first element from the vector...
//...which can be changed by adding a non-zero integer.
//name.begin() sets an internal iterator in the vector to the first element. Adding an integer to that iterator moves it.
name.clear(); //Erases the contents of the whole vector. 


-Albatross
Last edited on
can u help me to modify it by using vector that have mentioned to me... im beginner, my lecture havent start this vector topic yet..
Topic archived. No new replies allowed.