Program goes into loop.

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();
}
]
After the last cin.get() add return 0;

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.
Last edited on
I think you have to prompt the user inside the loop
otherwise it won't exit.
These two lines:

1
2
cout<<"Do you want to buy a book? Please Answer Y/N : ";
cin>>Ans;

Should be after:

1
2
while(char Ans == 'Y') 
{ 


That's one bug I just spotted.
If there's more post away.
Last edited on
while( char Ans='Y' )

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).
I have made the required changes in the while loop but it still does not work?!?
After I put in the Title of the book, it goes into a loop.
should i change the' if 'checks values like (if category= '01') and change the int category as static int Category=('01', '02','03','04','05')
All of the numbers will be truncated to 1, 2, 3, 4, and 5. You might consider an array of strings or C-style strings.

std::string category = {"01", "02", "03", "04", "05" };

Also remember the double equals == when testing for equality. Testing for equality with c-style strings requires strcmp or a variation of it.
Last edited on
Made changes...problem still persists?!?
Post your new code in code tags. (Click the # sign to the right and put your code between the two tags.)
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
#include <cstdlib>
#include <iostream>


using namespace 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();
}
    
Last edited on
made changes and posted the code b/w the tags...
Try this: I revised it a little because I have some spare time.

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
#include <cstdlib>
#include <iostream>
#include <string>


using namespace std;

int main()
{
	int Serial_No;
	int Book_No;
	char Title[50];
	string Category = "";
	int Selling_Price;
	double 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;
		

		cout<<"Enter Book Number: ";
		cin>>Book_No;
		

		cout<<"Enter Book Title: ";
		cin>>Title;
		
		cin.sync();
		cin.clear();


	    cout<<"Enter category  as 01, 02, 03, 04 or 05 of the Book:";
		getline(cin, Category);
		cin.sync();
		cin.clear();

		cout<<"The Selling Price of the Book is: ";
		cin>>Selling_Price;
		  

			   // Second Character
		switch(Category[1])
		{
			case '1':
				Discount = 20.0/100.0;
				break;

			case '2':
				Discount=15.0/100;
				break;

			case '3':
				Discount=10.0/100;
				break;

			case '4':
				Discount = 5.0 / 100;
				break;

			default:
				cout << "No Discount for this Category.\n";
		}

		// Book purchase information
		cout << "\n\nThe book purchase information is as follows:\n";
		cout << "serial #: " << Serial_No << "\n";
		cout << "book #: " << Book_No << "\n";
		cout << "title: " << Title << "\n";
		cout << "category: " << Category << "\n";
		cout << "selling price: " << Selling_Price << "\n";
		cout << "discount: " << Discount << "\n\n";

		Net_Price = Discount * Selling_Price;
		cout<<"The Discounted net price for the mentioned book is: "<<Net_Price << endl;

		// Stop the infinite loop
		Ans = 'N';
                
	}

	if(Ans=='N')
	{
		cout<<"Thanks for your visit.\n\n";
	}

	cin.sync();
	cin.clear();
	cout << "Press any key to continue...\n";
	cin.get();

	return 0;  
}
This program gives the following compilor error :
In function'int main()':
81 [Warning]converting to 'int' from 'double'
I changed Net_Price into double...it worked perfectly fine:-)...thanks a ton eker676!...the program works perfectly fine.
Btw: That is not a compile error. It is just a warning that a narrowing conversion is occurring and a possible loss of data may occur.
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;

}
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;


while(char Ans == 'Y')
{
Last edited on
Topic archived. No new replies allowed.