#include <iostream>
#include <conio.h>
#include <string>
// Includes filestream
#include <fstream>
usingnamespace std;
// Objects can vary from class
class editText
{
// Public functions accessible outside class
public:
// Writing on a text file
void write()
{
string name[9];
string number[9];
char restart;
int count = 0;
int lastCount;
top:
cout <<"What is the person's name?" << endl << endl;
getline(cin, name[count]);
cin.clear();
cout << endl << "What is the person's phone number?" << endl << endl;
getline(cin, number[count]);
cin.clear();
lastCount = count;
cout << endl << "Is that all? y/n" << endl << endl;
cin >> restart;
cin.clear();
count++;
switch(restart)
{
case'n':
cout << endl;
goto top;
break;
default:
cout << endl;
goto bottom;
break;
}
bottom:
ofstream iReadBook ("PhoneBook.txt", ios::app);
int x;
for(x = 0; x < (lastCount + 1); x++)
{
if(iReadBook.is_open())
{
iReadBook << name[x] << " " << number[x] << endl << endl;
}
}
iReadBook.close();
}
// Reading a text file
void read()
{
system("CLS");
string sReadBook;
ifstream iReadBook ("PhoneBook.txt");
if (iReadBook.is_open())
{
while (iReadBook.good())
{
getline(iReadBook,sReadBook);
cout << sReadBook << endl;
}
iReadBook.close();
}
}
// Clearing a text file
void clear()
{
ofstream iReadBook ("PhoneBook.txt", ios::trunc);
iReadBook.open("PhoneBook.txt");
iReadBook.close();
}
// Semicolon ends class
};
void main()
{
char view;
char clear;
editText edit;
edit.write();
cout << endl << "Would you like to view your phonebook? y/n" << endl << endl;
cin >> view;
cin.clear();
switch(view)
{
case'y':
edit.read();
break;
default:
cout << endl;
break;
}
cout << "Would you like to clear your phonebook? y/n" << endl << endl;
cin >> clear;
cin.clear();
switch(clear)
{
case'y':
edit.clear();
_getch();
break;
default:
cout << endl;
_getch();
break;
}
}
Whenever I answer n to "are you done yet", it skips the first question, and if I answer anything other than a character, it skips "do you wanna read your phonebook". Some help would be nice =). Started a less than a week ago.
It's because getline leaves a newline character in the buffer, which is subsequently read by cin. You should clear it before using cin>> with another getline() i think.
Tell me if it works.
19: error: conio.h: No such file or directory
71: error: 'system' was not declared in this scope
97: error: '::main' must return 'int'
127: error: '_getch' was not declared in this scope