I am fairly new to C++, as I began learning it two days ago, with some very basic background in java and python. I was trying to write a code that could simply store, recall, and delete entries in a class I named address_book. I did it first without using classes (just using the addbook structure I created) and it worked fine, but when I tried to change it into a class, it threw errors while initializing some integer variables. I have no idea how to fix this or what went wrong. I tried moving the initialization of the variables to zero outside of that function but could not get the variables to equal zero outside that. If you wouldn't mind, please help. Thank you in advance for any help you may give me.
Here is the code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct addBook {
string name;
string address;
string number;
};
//accepts a string and returns with all letters lowercase
void tolowcase (string& str){
for (int a=0; a<str.length(); a++)
{
if (str[a] >= 0x41 && str[a] <= 0x5A)
str[a]+=0x20;
}
}
class address_book {
addBook * book1, * book2;
int var1 = 0, var2 = 0, choice;
string retstr, name, address, number;
public:
void newEntry(string, string, string);
void showEntry(string);
void delEntry(string);
string readEntry(string);
};
int main()
{
string choice,input,name,add,num;
address_book mybook;
cout << "Welcome to the address book editor\n" << endl;
while (choice != "quit" && choice != "4")
{
cout << "1) Create Entry\n2) Read Entry\n3) Delete Entry\
\n4) Quit\nInput Choice -> ";
getline (cin, choice);
tolowcase(choice);
if (choice == "1" || choice == "create" || choice == "create entry" || choice == "new")
{
cout << "\nName ->"; //get and store the name
getline (cin, name);
cout << "\nAddress ->"; //get and store the address
getline (cin, add);
cout << "\nNumber ->"; //get and store the number
getline (cin, num);
mybook.newEntry(name, add, num);
}
else if (choice == "2" || choice == "read" || choice == "read entry")
{
cout << "\nRead Which Entry Number? (all reads all entries)\n->";
getline (cin, input);
mybook.showEntry(input);
}
else if (choice == "3" || choice == "delete" || choice == "delete entry" || choice == "del")
{
cout << "\nDelete Which Entry Number? (all deletes all entries)\n->";
getline (cin, input);
}
else if (choice != "4" && choice != "quit")
cout << "Invalid choice\n" << endl;
}
return 0;
}
void address_book::newEntry(string name, string address, string number) {
book1 = new addBook[++var1]; //adds 1 to number of entries in book1
(book1+var1-1)->name = name;
(book1+var1-1)->address = address;
(book1+var1-1)->number = number;
cout << "\n" << endl;
if (var1>1) //if number of entries in book1 > 1
for (int a = 1; a < var1; a++)//uses entries in book2 to replace
{ //entries lost from book1
(book1+a-1)->name = (book2+a-1)->name;
(book1+a-1)->address = (book2+a-1)->address;
(book1+a-1)->number = (book2+a-1)->number;
}
book2 = book1;
var2 = var1;
}
void address_book::showEntry(string input) {
if (input == "all") // loops each entry and returns
for (choice = 0; choice < var1; choice++)// the entry in format
{
name = (book1+choice)->name; // stores entries information
address = (book1+choice)->address;// to corresponding display
number = (book1+choice)->number; // variable
cout << "\nName:\t\t" << name << "\nAddress:\t"
<< address << "\nNumber:\t\t" << number
<< endl << "\nPress Enter to continue...";
getline (cin, input);
cout << endl;
}
else
{
stringstream(input) >> choice; // if choice != all, selects
if (choice <= var1 && choice >0) // entry number in choice and
{ // prints its values
choice--;
name = (book1+choice)->name;
address = (book1+choice)->address;
number = (book1+choice)->number;
cout << "\nName:\t\t" << name << "\nAddress:\t"
<< address << "\nNumber:\t\t" << number << endl;
cout << "\nPress Enter to continue...";
getline (cin, input);
cout << endl;
}
else
cout << "\nInvalid Choice\n" << endl;
}
}
void address_book::delEntry(string input) {
if (input == "all")
{
book1 = book2 = new addBook[0];
var1 = var2 = 0;
}
else
{
stringstream(input) >> choice;
if (choice <= var1 && choice > 0)
{
book1 = new addBook[--var1];
for (int a = var2 = 0; a <= var1; a++)
{
if (a == (choice-1)) continue;
(book1+var2)->name = (book2+a)->name;
(book1+var2)->address = (book2+a)->address;
(book1+var2)->number = (book2+a)->number;
var2++;
}
book2 = book1;
var2 = var1;
cout << "\n" << endl;
}
else
cout << "\nInvalid Choice\n" << endl;
}
}
string address_book::readEntry(string input) {
return retstr;
}
The errors returned are the same for var1 and var2 and all 6 errors returned on line 23.
ISO C++ forbids initialization of member `var1'
making `var1' static
ISO C++ forbids in-class initialization of non-const static member `var1'
I am using CodeBlocks and Windows XP if that makes any difference at all.
I know it is likely not the most efficient way to do this, but I was trying to do it with what knowledge I have. If you have any questions, suggestions, tips, comments, or critiques please let me know, all comments are welcome. Thanks again for any help.