looping get non stop

When i run this program,and choose "Y" to repeat the program,it skip the name and quantity line that prompt the user to enter data.
i try to change it to if else statement but it can't stop the repeating program.
i suppose to use the while looping but the problem was same,the program can't stop the looping.and it can't identified when i choose 'y' to end the program and keep repeating the program.

#include <iostream>
#include<string>
#include<istream>
using namespace std;

int main()
{

cout << "========================" << endl;
cout << "WELCOME" << endl;
cout << "========================" << endl;


return 0;
getchar();
}
Last edited on
std::cin.getline() can run into problems when used with std::cin >> var.
when you you type choice and enter '\n' is still sitting in input buffer and it is read automatically by getline(cin,a); in next loop. use std::cin.getline() to ignore that new line character. Also use == in while condition.


cout << "Would you like to quit?(Y/N)" << endl;
cin >> choice;
std::cin.ignore();
while (choice == 'N')
Last edited on
There were two problems I could find in your code.

First one was the while on the quit question was assigning a variable and not equating a variable. It would have a "==" instead of a "=".

The other thing is we don't call main recursively like you have with the return main().

A better Solution might look like:
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
int main()
{
     Purchase p;
     string a;
     int b;
     float c;
     char choice;

     do
     {
           cout << "========================" << endl;
           cout << "WELCOME" << endl;
           cout << "========================" << endl;

           cout <<"Enter Name :" <<endl;	
           getline(cin,a);
           cout <<"Enter Quantity:" <<endl;
           cin >> b;
           cout <<"Enter Price : RM" <<endl;
           cin >> c;

           p.set_data(a, b, c);

           p.calculate();

           cout << "========================" << endl;
           cout << "RECEIPT" << endl;
           cout << "========================" << endl;

           p.print();	

           cout << "Would you like to quit?(Y/N)" << endl;
           cin >> choice;
     
     } while (choice == 'N')

     return 0;
     getchar();
}


Hope that helps you.
oke. But why i can call the main like that?
is there any other way so i just use the while looping only. that one was do while looping.
thats why i call the main like that so i don't have to write the program again to repeat it.
when i choose "N", the looping skip as i mention above(sorry i change a little bit because i edit the program) and it didn't stop.
i also use the std::cin.getline() but it contain error, and i couldn't fix it.
When you have this in your prorgram:

return main();

you are not going back to the start of main. You are creating a WHOLE NEW main on top of the first one, taking up the same amount of memory again, and then when you do it again you make a WHOLE NEW one on top of those two, and so on and so on and so on, each time taking up more and more and more memory until eventually you run out of memory and the whole thing crashes.
Do you know what Recursion is? main() is a special function of C/C++. It is usually the Entry point of your code, in other word where you start. If you recursively call main the program will be in an endless loop because main doesn't return anything until it exits the program. The return value on Main goes back to the Program's caller, which usually is the Operating system.

You just want to run the code once?

if this is the case, my code would look like:
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
int main()
{
     Purchase p;
     string a;
     int b;
     float c;
     char choice;

     cout << "========================" << endl;
     cout << "WELCOME" << endl;
     cout << "========================" << endl;

     cout <<"Enter Name :" <<endl;	
     getline(cin,a);
     cout <<"Enter Quantity:" <<endl;
     cin >> b;
     cout <<"Enter Price : RM" <<endl;
     cin >> c;

     p.set_data(a, b, c);
     p.calculate();

     cout << "========================" << endl;
     cout << "RECEIPT" << endl;
     cout << "========================" << endl;

     p.print();	

     getchar(); // I am assuming this is just to hold the execution window open.

     return 0;
}
Last edited on
I am sorry it is std::cin.ignore(); . I edited my post.
Refer the below link. This may help you.

http://augustcouncil.com/~tgibson/tutorial/iotips.html
thank you. the std::cin.ignore(); works. =)
i already done if it just run one times and have no problem.
it just i must modified it as long as the user prompt to choose "N" so the program must repeat again.
Topic archived. No new replies allowed.