I'm having a problem which is the output keeps repeating infinitely unless I click the cmd.

I'm a freshmen in the IT industry, and I need to report about this system.
This system is a chairs and tables rental, which is the topic for my case study.

Im having a problem which the output of the system keeps repeating infinitely after I typed the wrong variable.

In this particular code, I know that int is for numbers only, but after typing 'a' in the date part and entering it, it makes the error which repeats infinitely.

cout<<"\t\t\t\t Date : Dec. ";
cin>>date;

Here is the code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main(){
char choice1,choice2,choice3,rep;
string name,address;
int q1, q2, q3,date,ddate,chair,total,total1,total2,total3;

cout<<"\t\t\t\t Tables and Chairs Rental"<<endl;
cout<<"\t\t\t\t//////////////////////////////////////////"<<endl;
cout<<"\t\t\t\t|"<<setw(12)<<"Items"<<setw(20)<<"Price"<<setw(9)<<"|"<<endl;
cout<<"\t\t\t\t|----------------------------------------|"<<endl;
cout<<"\t\t\t\t|"<<setw(12)<<"Chair"<<setw(19)<<"P10"<<setw(10)<<"|"<<endl;
cout<<"\t\t\t\t|"<<setw(12)<<"Table"<<setw(19)<<"P30"<<setw(10)<<"|"<<endl;
cout<<"\t\t\t\t|"<<setw(15)<<"Table Cloth"<<setw(16)<<"P20"<<setw(10)<<"|"<<endl;
cout<<"\t\t\t\t//////////////////////////////////////////"<<endl;
cout<<"\t\t\t\t Name : ";
cin>>name;
cin.ignore();
cout<<"\t\t\t\t Address : ";
cin>>address;
cout<<"\t\t\t\t Date : Dec. ";
cin>>date;
ddate=date+2;
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t|"<<setw(28)<<"Your Order/s : "<<setw(13)<<"|"<<endl;
cout<<"\t\t\t\t|________________________________________|"<<endl;
cout<<"\t\t\t\t"<<setw(15)<<" Want to order chairs? (Y)Yes (S)Skip"<<endl;
cout<<"\t\t\t\t - ";
cin>>choice1;
if(choice1=='Y' || choice1=='y'){
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
cout<<"\t\t\t\t Chairs | Quantity : ";
cin>>q1;
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
total1=q1*10;
}
else if(choice1=='S' || choice1=='s'){
total1=0;
cout<<"\t\t\t\t ________________________________________"<<endl;
}
else{
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t\t\tWrong input."<<endl;
cout<<"\t\t\t\t\t\tTry Again."<<endl;
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<main();
cout<<endl;
}
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t"<<setw(15)<<" Want to order tables? (Y)Yes (S)Skip"<<endl;
cout<<"\t\t\t\t - ";
cin>>choice2;
if(choice2=='Y' || choice2=='y'){
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
cout<<"\t\t\t\t Tables | Quantity : ";
cin>>q2;
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
total2=q2*30;
}
else if(choice2=='S' || choice2=='s'){
total2=0;
cout<<"\t\t\t\t ________________________________________"<<endl;
}
else{
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t\t\tWrong input."<<endl;
cout<<"\t\t\t\t\t\tTry Again."<<endl;
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<main();
cout<<endl;
}
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t"<<setw(12)<<"Want to order table cloth? (Y)Yes (S)Skip"<<endl;
cout<<"\t\t\t\t - ";
cin>>choice3;
if(choice3=='Y' || choice3=='y'){
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
cout<<"\t\t\t\t Table Cloth | Quantity : ";
cin>>q3;
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
cout<<endl;
total3=q3*20;
}
else if(choice3=='S' || choice3=='s'){
total3=0;
cout<<"\t\t\t\t ________________________________________"<<endl;
}
else{
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t\t\tWrong input."<<endl;
cout<<"\t\t\t\t\t\tTry Again."<<endl;
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<main();
cout<<endl;
}
cout<<endl;
cout<<endl;
total=total1+total2+total3;

system("CLS");
cout<<endl<<endl;
cout << "\t\t\t\t Tables and Chairs Rental Invoice"<<endl;
cout << "\t\t\t\t ///////////////////////////////////////"<<endl;
cout << "\t\t\t\t Invoice No."<<" : #"<<setw(9)<<8302793<<endl;
cout << "\t\t\t\t Customer Name "<<" :"<<setw(10)<<name<<endl;
cout << "\t\t\t\t No. of Chairs "<<" :"<<setw(10)<<q1<<endl;
cout << "\t\t\t\t No. of Tables "<<" :"<<setw(10)<<q2<<endl;
cout << "\t\t\t\t No. of Table Cloth "<<" :"<<setw(10)<<q3<<endl;
cout << "\t\t\t\t Your Rental Amount is "<<" :"<<setw(10)<<total<<endl;
cout << "\t\t\t\t Caution Money "<<" :"<<setw(10)<<"0"<<endl;
cout << "\t\t\t\t Advanced "<<" :"<<setw(10)<<"0"<<endl;
cout << "\t\t\t\t________________________________________"<<endl;
cout << "\t\t\t\t Total Rental Amount is "<<" :"<<setw(10)<<total<<endl;
cout << "\t\t\t\t Delivery Date "<<" :"<<setw(9)<<"Dec. "<<ddate<<endl;
cout << "\t\t\t\t Delivery Address"<<" :"<<setw(5)<<address<<endl;
cout << "\t\t\t\t________________________________________"<<endl<<endl;

cout<<"\t\t\t\tForgot something? (Y)Yes (N)No"<<endl;
cout<<"\t\t\t\t- ";
cin>>rep;
if(rep=='Y' || rep=='y'){
cout<<endl;
system("CLS");
cout<<main();
}
else{
system("CLS");
cout<<endl<<endl<<endl<<endl;
cout<<"\t\t\t\t\tThank you for using our system!";
cout<<endl<<endl<<endl;
}
return 0;
}
Hello Xerronim,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting and some well placed blank lines it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


When you enter a date what exactly are you entering and expecting to use?

Also for the "name" "std::getline(std::cin, name);" may be a better choice especially if there is a space in the name.

This will take a little while to reformat your code into something that is readable.

Andy
date is an int.

So cin >> date attempts to retrieve a number from the input.

You entered 'a' , so no number is retrieved. 'a' remains there in the input buffer. This also puts cin into an error state.

You can clear the error state: cin.clear();

This will leave 'a' still in the input buffer, and the next time you try to read from the input, that letter will be read. This is bad.

So you also need to clear the input buffer: cin.ignore(numeric_limits<streamsize>::max(), '\n'); would do it pretty solidly. Needs #include <limits>

Also, this:
cout<<main();
No no no. NEVER EVER CALL MAIN. main is not for you to call. Never ever ever call main. This is bad. If you want your code to loop, use a loop.
How will this
1
2
3
4
5
6
7
else{
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t\t\tWrong input."<<endl;
cout<<"\t\t\t\t\t\tTry Again."<<endl;
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<main();
cout<<endl;
loop back to this?
1
2
3
4
5
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
cout<<"\t\t\t\t Chairs | Quantity : ";
cin>>q1;
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
total1=q1*10;
Hello Xerronim,

Xerronim wrote:

How will this
1
2
3
4
5
6
7
else{
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<"\t\t\t\t\t\tWrong input."<<endl;
cout<<"\t\t\t\t\t\tTry Again."<<endl;
cout<<"\t\t\t\t ________________________________________"<<endl;
cout<<main();
cout<<endl;

loop back to this?
1
2
3
4
5
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
cout<<"\t\t\t\t Chairs | Quantity : ";
cin>>q1;
cout<<"\t\t\t\t - - - - - - - - - - - - - - - - - - - - "<<endl;
total1=q1*10;



Good question, but first do you think it would be a good idea to get the program to compile, (without errors), and run?

You are using "std::strings" in your program, but missed including the header file "<string>".

I see that you are using many (\t)s in the code. Do not count on this spacing things out properly.

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
#include <cctype>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

#include <cstdlib> // <--- Not sure yet if this is needed.

//using namespace std;

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::setw;

int main()
{
	constexpr int CHAIR_PRICE{ 10 };

	char choice1{}, choice2{}, choice3{}, rep{}, junk{};
	int q1{}, q2{}, q3{};
	int date{}, ddate{}, chair{}, month{}, day{}, year{};
	int total{}, total1{}, total2{}, total3{};
	string name, address;

	cout << std::string(40,' ') << "Tables and Chairs Rental\n";
	cout << "\t\t\t\t//////////////////////////////////////////\n";
	cout << std::string(32,' ') << "|" << setw(12) << "Items" << setw(20) << "Price" << setw(9) << "|\n";

	cout << std::setw(32)<< ' ' << "|----------------------------------------|\n";

	cout << std::string(32, ' ') << "|" << setw(12) << "Chair" << setw(19) << "P10" << setw(10) << "|\n";
	cout << std::string(32, ' ') << "|" << setw(12) << "Table" << setw(19) << "P30" << setw(10) << "|\n";
	cout << std::string(32, ' ') << "|" << setw(15) << "Table Cloth" << setw(16) << "P20" << setw(10) << "|\n";
	cout << std::string(32, ' ') << "//////////////////////////////////////////\n";

	cout << '\n' << std::string(32, ' ') << "Name : ";
	std::getline(std::cin, name);
	//cin.ignore();

	cout << std::string(32, ' ') << "Address : ";
	std::getline(std::cin, address);

	cout << std::string(32, ' ') << "Date (MM/DD/YYYY): ";
	cin >> month >> junk >> day >> junk >> year;

Try not to use "magic" numbers. Line 19 is for total1 = q1 * CHAIR_PRICE;. This way there is only one place to change the price and you do not have to go looking through the program to find every place that needs changed.

This should give you an idea of what you can do. It is just a suggestion.

Lines 39 and 43 are likely to have spaces in what is typed on the keyboard. Formatted input std::cin >> name will extract from the input buffer the first white space or "\n" whichever comes first. If there is a space anything left will stay in the input buffer until the next call for input and then it will not wait for input, but use whatever is in the input buffer. "std::getline()", or unformatted input will extract everything until it finds the "\n", i.e.,( Enter).

Line 46 is just a guess at what you could do. Since you have not answered my earlier question I do not know what you want.

First you need to get the program to compile, then get the input correct, then finally you can work on looping the program. First you need something to work with.

Hope that helps,

Andy

Edit: typo
Last edited on
Topic archived. No new replies allowed.