I am currently working on a program that will write two pieces of information to file, via a variable. The two pieces of data stored are the following:
The persons name is stored in a variable array called ‘name’, and the house is stored in a character variable called ‘housecolour’.
I need help on the if statement for the housecolour, this being to only allow the user to enter a character ‘G’ or ‘Y’. Any other character will lead to an Error message being displayed.
Only with the code I have written, the ‘Y’ get accepted but ‘G’ doesn’t why is this?
.::Code Enclosed below::.
Code:
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
int main (void)
{
char name[20];
char housecolour;
int ln;
char choice;
ofstream data (".\\Students.txt");
do
{
gotoxy(52,14);
cout << (char)218 << setw(9) << setfill((char)196) << (char)191 << endl;
gotoxy(51,15);
cout << (char)218 << (char)217 << setw(9) << setfill(' ') << (char)192 << setw(13) << setfill((char)196) << (char)191 << endl;
ln = 16;
do
{
gotoxy(51,ln);
cout << (char)179 << setw(23) << setfill(' ') << (char)179 << endl;
ln++;
} while (ln != 19);
gotoxy(51,19);
cout << (char)192 << setw(23) << setfill((char)196) << (char)217 << endl;
gotoxy(54,15);
cout << "STATUS";
gotoxy(53,17);
cout << "Saved Successfully";
gotoxy(9,11);
cout << "Name: ";
gotoxy(9,12);
cout << "House Colour: ";
gotoxy(15,11);
cin >> name;
do
{
gotoxy(23,12);
cin >> housecolour;
housecolour = toupper (housecolour);
if (housecolour != 'Y' && 'G')
{
gotoxy(53,17);
textcolor(LIGHTRED);
cprintf ("Error ");
}
} while (housecolour != 'Y' && 'G');
gotoxy(53,17);
textcolor(YELLOW);
cprintf ("Awaiting Input");
data << name << ' ';
data << housecolour << endl;
gotoxy(53,17);
textcolor(GREEN);
cprintf ("Saved Successfully");
gotoxy(31,17);
} while (choice != 'N');
data.close();
}