Printing values from a switch
The switch case doensnt seem to work. It prints the values after I added them and I canĀ“t figure out why.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Phonebook
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Phonebook
{
public:
Phonebook() : name(""), number(0) {}
Phonebook(const string &_name, int _number) : name(_name), number(_number) {}
Phonebook(const Phonebook &rhs) : name(rhs.name), number(rhs.number) {}
Phonebook operator=(const Phonebook &rhs)
{
if (&rhs == this)
{
return *this;
}
name = rhs.name;
number = rhs.number;
return *this;
}
//Metod: Sets the necessary info
void SetInfo(const string &_name, int _number)
{
name = _name;
number = _number;
}
const string &Name() const { return name; }
int Number() const { return number; }
private:
string name;
int number;
};
//Function Declarations
void fillPhonebook(vector<Phonebook>&myPhonebook);
//fillVector
//@param vector<Class>)& - the vector to be filled
void printPhonebook(vector<Phonebook>&myPhonebook);
//printVector - prints the information of all students
//@param const vector<Student>& - students in class
int i = 0;
int deleteFromVector;
int main()
{
vector<Phonebook>myPhonebook;
while(true)
{
cout << " - Press 1 to add a person and number" << endl;
cout << " - Press 2 to print all persons and numbers" << endl;
cout << " - Press 3 to delete person and number" << endl;
cout << " - Press 4 to quit" << endl;
int choice;
cin >> choice;
switch(choice)
{
case 1:
{
fillPhonebook(myPhonebook);
}
case 2:
{
cout << "Print all names and numbers" << endl;
printPhonebook(myPhonebook);
}
case 3:
{
cout << "Which index do you want to delete? (1-10)" << endl;
}
case 4:
break;
}
}
return 0;
}
// Function Definitions
void fillPhonebook(vector<Phonebook>&myPhonebook)
{
string name;
int number;
// Flush makes sure that the buffer is cleared and the characters are written to their destination
cout << "Enter persons name: " << flush << endl;
// Enter values into vector
cin >> name;
cout << "Enter persons number: " << flush << endl;
// Enter values into vector
cin >> number;
Phonebook Phonebook(name, number);
myPhonebook.push_back(Phonebook);
cout << endl;
/*myPhonebook.push_back(Phonebook(name, number));*/
}
void printPhonebook(vector<Phonebook>&myPhonebook)
{
for (int i = 0; i < myPhonebook.size(); i++)
{
cout << "Name: " << myPhonebook[i].Name() << endl;
cout << "Number: " << myPhonebook[i].Number() << endl;
cout << endl;
}
}
|
you need break statements for all cases, otherwise it will call all cases under the one you've selected.
Last edited on
Thanks mutexe!
Topic archived. No new replies allowed.