Jul 19, 2011 at 5:39pm Jul 19, 2011 at 5:39pm UTC
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 133 134
#include <iostream>
#include <conio.h>
#include <string>
// Includes filestream
#include <fstream>
using namespace 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.
Last edited on Jul 19, 2011 at 5:40pm Jul 19, 2011 at 5:40pm UTC
Jul 19, 2011 at 5:43pm Jul 19, 2011 at 5:43pm UTC
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.
Jul 19, 2011 at 5:45pm Jul 19, 2011 at 5:45pm UTC
I thought that's what I did. I used cin.clear(); after everything input.
Jul 19, 2011 at 6:47pm Jul 19, 2011 at 6:47pm UTC
Can I get some help, guys? That doesn't work.
Jul 19, 2011 at 9:37pm Jul 19, 2011 at 9:37pm UTC
Thanks, it worked, but can I ask what the difference is between sync and clear?