Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7.   int i, k;
  8.   string names[99];
  9.   int lengthOfNames = 0;
  10.   string name, choice;
  11.   bool found;
  12.   do {
  13.       cout<<"Insert a name (or press 'finish' to proceed)> ";
  14.       cin>>name;
  15.       if (name != "finish") {
  16.           names[lengthOfNames] = name;
  17.           lengthOfNames++;
  18.       }      
  19.      
  20.   }
  21.   while(name!="finish");
  22.   cout<<"1) See all names"<<endl<<"2) Search by name"<<endl<<"3) Select all after certain name"<<endl<<"4) Select all before certain name"<<endl<<"5) Change a name"<<endl;
  23.   cout<<"What do you want to do?"<<endl;
  24.   cin>>choice;
  25.   if (choice == "1") for (i=0; i<lengthOfNames; i++) cout<<names[i]<<endl;
  26.   if (choice == "2") {
  27.       cout<<"Insert a name:";
  28.       cin>>name;
  29.       found = false;
  30.       for (i=0; i<lengthOfNames; i++) {
  31.           if (names[i] == name) {
  32.              found = true;
  33.              break;
  34.           }      
  35.       }      
  36.       cout<<"Position: "<<i<<endl;
  37.   }
  38.   if (choice == "3") {
  39.       cout<<"Insert a name:";
  40.       cin>>name;
  41.       found = false;
  42.       for (i=0; i<lengthOfNames; i++) {
  43.           if (names[i] == name) {
  44.              found = true;
  45.              break;
  46.           }      
  47.       }      
  48.       cout<<"Position: "<<i<<endl;
  49.       for (k=i+1; k<lengthOfNames; k++) cout<<names[k]<<endl;
  50.   }
  51.  
  52.  
  53.  
  54.   if (choice == "4") {
  55.       cout<<"Insert a name:";
  56.       cin>>name;
  57.       found = false;
  58.       for (i=0; i<lengthOfNames; i++) {
  59.           if (names[i] == name) {
  60.              found = true;
  61.              break;
  62.           }      
  63.       }      
  64.       cout<<"Position: "<<i<<endl;
  65.       for (k=0; k<i; k++) cout<<names[k]<<endl;
  66.   }
  67.  
  68.   if (choice == "5") {
  69.       cout<<"Insert a name:";
  70.       cin>>name;
  71.       found = false;
  72.       for (i=0; i<lengthOfNames; i++) {
  73.           if (names[i] == name) {
  74.              found = true;
  75.              cout<<"Insert the new name: ";
  76.              cin>>name;
  77.              names[i] = name;
  78.              cout<<"Done! The new name is "<<name<<" and the position is "<<i<<endl;
  79.              break;
  80.           }      
  81.       }      
  82.      
  83.   }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement