What is wrong with my code when i press ctrl + z?

//TaylorZ Merchandis: TaylorZ Merchandise.cpp
//This program is designed to calculate the sales for TaylorZ Merchandise
#include <iostream>
#include <iomanip>


using namespace std;

int main()
{
//Variable Declaration
double code1=0;//The price for code TX5675
double code2=0;//The price for code UN3453
double code3=0;//The price for code WM3897
double code4=0;//The price for code MX3567
double code5=0;//The price for code TU9898
char codes; //Codes includesall the 5 codes in it
int quantity;//Number of t-shirt
double totalRetail;//Total retail sales

cout<<setw(65)<<"**********************************************************";
cout<<"\n"<<setw(65)<<"*Welocome to TaylorZ Merchandise Calculator For t-Shirts*"<<endl;//display welcome message

cout<<"\n"<<setw(40)<<"* TX5675--> A *\n"
<<"\n"<<setw(40)<<"* UN3453--> B *\n"
<<"\n"<<setw(40)<<"* WM3897--> C *\n"
<<"\n"<<setw(40)<<"* MX3567--> D *\n"
<<"\n"<<setw(40)<<"* TU9898--> E *\n"<<endl;//using alphabets to represent the codes

cout<<"Please input an alphabet which represent the code or ctrl+z to exit: "<<endl;//Prompt user to input a,b,c,d or e
cin>>codes;//data will be input by user

while(codes!=EOF)//ctrl + z to execute
{
switch(codes)
{
case'A':
case'a':
cout<<"Quantity for the t-shirt TX5675: ";//Prompt user to input quantity of t-shirt
cin>>quantity;//User will input data
code1 = 25.80 * quantity;//Formula to calculate total sales for code1
cout<<"\nTotal Sales For TX5675: $"<<code1;//display total sales for code1
break;//exit the loop

case'B':
case'b':
cout<<"Quantity for the t-shirt UN3453: ";//Prompt user to input quantity of t-shirt
cin>>quantity;//User will input data
code2 = 34.60 * quantity;//Formula to calculate total sales for code2
cout<<"\nTotal Sales For UN3453: $"<<code2;//display total sales for code2
break;//exit the loop

case'C':
case'c':
cout<<"Quantity for the t-shirt WM3897: ";//Prompt user to input quantity of t-shirt
cin>>quantity;//User will input data
code3 = 39.90 * quantity;//Formula to calculate total sales for code3
cout<<"\nTotal Sales For WM3897: $"<<code3<<endl;//display total sales for code3
break;//exit the loop

case'D':
case'd':
cout<<"Quantity for the t-shirt MX3567: ";//Prompt user to input quantity of t-shirt
cin>>quantity;//User will input data
code4 = 28.90 * quantity;//Formula to calculate total sales for code4
cout<<"\nTotal Sales For MX3567: $"<<code4<<endl;//display total sales for code4
break;//exit the loop

case'E':
case'e':
cout<<"Quantity for the t-shirt TU9898: ";//Prompt user to input quantity of t-shirt
cin>>quantity;//User will input data
code5 = 36.80 * quantity;//Formula to calculate total sales for code5
cout<<"\nTotal Sales For TU9898: $"<<code5<<endl;//display total sales for code5
break;//exit the loop

default:
cout<<"Please be more sensible and key in something that make sense."<<endl;
}
totalRetail=code1+code2+code3+code4+code5;//Formula for total retail sales
cout<<"\nTotal Retail Sales For t-Shirt: $"<<totalRetail<<"\n"<<endl;//Display total retail sales

cout<<"Please input an alphabet which represent the code or ctrl+z to exit: "<<endl;
cin>>codes;
}


}
1
2
3
char codes; 
//...
codes!=EOF
In C++, this macro corresponds to the value of char_traits<char>::eof().
http://www.cplusplus.com/reference/cstdio/EOF/
The End-of-File value is a special value used by many standard functions to represent an invalid character; Its value shall not compare equal to any of the values representable with char_type
http://www.cplusplus.com/reference/string/char_traits/eof/
Basically you cannot put EOF in a char reliably. Use stream state flags to get desired behavior:
1
2
3
4
while(not (std::cin >> codes).eof() )
{
    //...
}
Or just
1
2
3
4
while(std::cin >> codes)
{
    //...
}
Catches no tonly end of stream, but also stream failures
Topic archived. No new replies allowed.