I have written the following program which goes into a loop and does not come out of it.Can anyone guide me on this?
[
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int Serial_No;
int Book_No;
char Title[50];
int Category=(01,02,03,04,05);
int Selling_Price;
int Discount=0;
int Net_Price;
char Ans;
cout<<"Do you want to buy a book? Please Answer Y/N : ";
cin>>Ans;
while(char Ans='Y')
{
cout<<"Enter Serial Number:";
cin>>Serial_No;
cin.ignore();
cout<<"Enter Book Number: ";
cin>>Book_No;
cin.ignore();
cout<<"Enter Book Title: ";
cin>>Title;
cin.ignore();
cout<<"Enter category of the Book:";
cin>>Category;
cin.ignore();
cout<<"The Selling Price of the Book is: ";
cin>>Selling_Price;
cin.ignore();
if(Category=01)
{
Discount=20/100;
}
else
{
if(Category=02)
{
Discount=15/100;
}
else
{
if(Category=03)
{
Discount=10/100;
}
else
{
if(Category=04)
{
Discount=5/100;
}
else
{
if(Category=05)
{
Discount=0;
}
}
}
}
}
cout<<"The book purchase information is as follows:"<<"\n";
cout<<Serial_No;
cout<<Book_No;
cout<<Title;
cout<<Category;
cout<<Selling_Price;
cout<<Discount;
Net_Price=Discount*Selling_Price;
cout<<"The Discounted net price for the mentioned book is: "<<Net_Price;
}
if(Ans='N')
{
cout<<"Thanks for your visit."<<"\n";
}
cin.get();
}
]
Edit: In your while loop change it to this while(Ans == 'Y')
Btw when you are checking for equality you need to use == not =. If you use = the stuff on the right hand side will be assigned to the left hand side and it will automatically be true.
will always be true assuming it is legal syntax, since it declares a variable and assigns it the value 'Y', which when converted to boolean, evaluates to true.
(You have many errors in the above code; all of your if() checks are actually assignments rather than comparisons; int Category=(01,02,03,04,05); probably does not do what you think it does; 10/100 for example is done using integer division which evaluates to 0; and oh by the way, not that it matters for your program, but prefixing a leading zero on a number causes the compiler to treat it as an octal value, not a decimal value, so 05 is 5 octal (as I said, none of your values yield a different value in octal than in decimal so it will happen to work for you).
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main()
{
int Serial_No;
int Book_No;
char Title[50];
std::string Category=("01", "02", "03", "04", "05" );
int Selling_Price;
int Discount=0;
int Net_Price;
char Ans;
cout<<"Do you want to buy a book? Please Answer Y/N : ";
cin>>Ans;
while(Ans=='Y')
{
cout<<"Enter Serial Number:";
cin>>Serial_No;
cin.ignore();
cout<<"Enter Book Number: ";
cin>>Book_No;
cin.ignore();
cout<<"Enter Book Title: ";
cin>>Title;
cin.ignore();
cout<<"Enter category as 01, 02, 03, 04 or 05 of the Book:";
cin>>Category;
cin.ignore();
cout<<"The Selling Price of the Book is: ";
cin>>Selling_Price;
cin.ignore();
if(Category=="01")
{
Discount=20/100;
}
else
{
if(Category=="02")
{
Discount=15/100;
}
else
{
if(Category=="03")
{
Discount=10/100;
}
else
{
if(Category=="04")
{
Discount=5/100;
}
else
{
if(Category=="05")
{
cout<<"No Discount for this Category.";
}
}
}
}
}
cout<<"The book purchase information is as follows:"<<"\n";
cout<<Serial_No;
cout<<Book_No;
cout<<Title;
cout<<Category;
cout<<Selling_Price;
cout<<Discount;
Net_Price=Discount*Selling_Price;
cout<<"The Discounted net price for the mentioned book is: "<<Net_Price;
}
if(Ans=='N')
{
cout<<"Thanks for your visit."<<"\n";
}
return 0;
cin.get();
}
if(Category=01) /* Never use this = mark to in a condition,, this is not give u acepted result,, This is an asignment mark (=) not the equl sign to chek the condition with equl sign you have to use == this sign,,,
within the loop also you have to use this correct mark,,
other wise program does not give ur exepted results *
if(Category==01)
{
Discount=20/100;