While Loop Choice

I am doing a basic Do-While loop choice program, in which I want to ask the user to decide whether to run the program again or not..I know what I am doing, but the program doesnt run again...it stops after the first execution everytime. I am using the Dev C++ compiler.

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
#include<iostream>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    int Length1, Length2 ;
    int choice1, choice2;
    int Area, Perimeter;
    int y, n;

    do 
    { //Start of Do/While Loop;
        cout<<"Choose one of the following\nThe choices are: "<<endl;
        cout<<"1) Area of a Rectangle"<<endl;
        cout<<"2) Perimeter of a Rectangle"<<endl;
        cin>>choice1;
        
                     cout<<"Enter the length of the two sides"<<endl;
                     cout<<"Length of side 1>>";
                     cin>>Length1;
                     cout<<"Length of side 2>>";
                     cin>>Length2;
                     
                                  switch(choice1)
                                                { //Start of Switch
                                                  case 1: 
                                                          {
                                                          Area = (Length1 * Length2);
                                                          cout<<"The Area is "<<Area;
                                                          }
                                                                     break;
                                                  case 2:
                                                          {
                                                          Perimeter = (Length1 + Length1 + Length2 + Length2);
                                                          cout<<"The Perimeter is "<<Perimeter;
                                                          }
                                                                     break;
                                                  default :
                                                          {
                                                          cout<<"You have entered an invalid choice";
                                                          }
                                                                     break;       
                                                 }
                     cout<<endl<<"Do you wish to calculate another value(y/n)?"<<endl;
                     cin>>choice2;
        } //end of do/while Loop;
        while (choice2 == y);
        
    cout<<endl<<endl;
    system("PAUSE");
    return 0;
}
Last edited on
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
#include<iostream>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    int Length1, Length2 ;
    int choice1;
    char  choice2;
    int Area, Perimeter;
    int y, n;

    do 
    { //Start of Do/While Loop;
        cout<<"Choose one of the following\nThe choices are: "<<endl;
        cout<<"1) Area of a Rectangle"<<endl;
        cout<<"2) Perimeter of a Rectangle"<<endl;
        cin>>choice1;
        
                     cout<<"Enter the length of the two sides"<<endl;
                     cout<<"Length of side 1>>";
                     cin>>Length1;
                     cout<<"Length of side 2>>";
                     cin>>Length2;
                     
                                  switch(choice1)
                                                { //Start of Switch
                                                  case 1: 
                                                          {
                                                          Area = (Length1 * Length2);
                                                          cout<<"The Area is "<<Area;
                                                          }
                                                                     break;
                                                  case 2:
                                                          {
                                                          Perimeter = (Length1 + Length1 + Length2 + Length2);
                                                          cout<<"The Perimeter is "<<Perimeter;
                                                          }
                                                                     break;
                                                  default :
                                                          {
                                                          cout<<"You have entered an invalid choice";
                                                          }
                                                                     break;       
                                                 }
                     cout<<endl<<"Do you wish to calculate another value(y/n)?"<<endl;
                     cin>>choice2;
        } //end of do/while Loop;
        while (choice2 ==' y' || choice2 == 'Y');
        
    cout<<endl<<endl;
    system("PAUSE");
    return 0;
}


I changed variable choice2 from int to char. And now everything work fine :)
oh i changed that myself too..but it doesnt do anything. Also, I changed y and n to charachters (which I should have done earlier), but it doesnt work...it stops the program after the first execution.
closed account (S6k9GNh0)
1. Dev-C++ isn't a compiler. It is an IDE. Also, Dev-C++ is old and unmaintained. You should use something more modern or it's successor, wxDev-C++.

2. iostream.h is not a C++ header. As a matter of fact, that shouldn't compile at all. All C++ headers do not have an extension, and C STD headers, are prefixed with "c" and extension removed. See below for more info.

3. conio is generally bad and very, very, decrepit. You also have no use for it here...

4. Onto the real problem as to why your program doesn't work: while (choice2 == y);. This doesn't make sense. y isn't initialized so it's undefined on whether it's anything at all.

If you change it to compare to a character, you'll still find that it doesn't work. Why? Because the cin istream still has data in it and it's dumping that data onto choice. So regardless of what you type in, it's going to be the same. Generally the solution is to clear the stream before each use. We can use strings, getline(), and stringstreams to solve all of our problems: http://www.cplusplus.com/forum/articles/6046/

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
#include <iostream>
#include <sstream>

using namespace std;

template <typename T>
void getInput(T& output)
{
	for (;;) 
	{
		std::string input;
		getline(cin, input);

		// This code converts from string to number safely.
		stringstream myStream(input);
		if (myStream >> output) //This will break if T is something bogus.
			break;
		cout << "Invalid value, please try again" << endl;
	}
}

int main()
{
	int Length1, Length2 ;
	int choice1;
	char choice2;
	int Area, Perimeter;

	do 
	{ //Start of Do/While Loop;
		cout << "Choose one of the following\nThe choices are: " << endl;
		cout << "1) Area of a Rectangle" << endl;
		cout << "2) Perimeter of a Rectangle" << endl;
		getInput(choice1);

		cout<<"Enter the length of the two sides"<<endl;
		cout<<"Length of side 1>>";
		getInput(Length1);

		cout<<"Length of side 2>>";
		getInput(Length2);

		switch(choice1)
		{
		case 1: 
			Area = (Length1 * Length2);
			cout<<"The Area is "<<Area;
			break;
		case 2:
			Perimeter = (Length1 + Length1 + Length2 + Length2);
			cout<<"The Perimeter is "<<Perimeter;
			break;
		default :
			cout<<"You have entered an invalid choice";
			break;       
		}

		cout<<endl<<"Do you wish to calculate another value(y/n)?"<<endl;
		getInput(choice2);
	}
	while (choice2 == 'y' || choice2 == 'Y');

	cout<<endl<<endl;
	return 0;
}


http://ideone.com/ovjsN


I'll also note this still isn't a very clean solution. Many things can go wrong.
Last edited on
I am really a beginner at C programming (less than a month since I have been taking classes for it), so I didnt know about what you told me, that I need to clear out the istream. That works out right in the program now, and alot of it even makes sense too now, except some of the part about the stringstream and istream. I will ask my tutor tommorow about them.

Also, thanks for the hint about the header files. It was a pain writing 4 header files each time..but somehow it never worked, or executed the code without writing all 4 of those files. But, these 2 files work just was fine too. The program executed simply, and had no problems. I will try out wxDev C++ on my system too now; the only reason I found about dev C++ was because my tutor uses the antiquated Turbo C++, which doesnt run on my system. But I'll try the update for it, the wxdev, and see how it works. I really want to get visual studios too, but I am waiting till I finish my course to get it.

Thanks alot for the help.
@ computerequip: "iostream.h" is a depricated version of the iostream header kept for backward compatibility. This is something that Dev-C++ did for some reason.
yes, but Dev C++ also doesnt accept iostream. It gives a warning if you use iostream.h, but it doesnt compile if you dont use it either.
I'm pretty new myself but this is what I was using...

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

using namespace std;

int main () {
	int iAgain = 1;

	while (iAgain == 1) {

	cout << "Oh Hai there..." << endl;
	cout << "Do you want to play again? 1 for Yes, 2 for No." << endl;
	cin.ignore();
	cin >> iAgain;
ducheck:
	if (iAgain <= 0) {
		cout << "That's not a valid answer lol, try again" << endl;
		cout << "Do you want to play again? 1 for Yes, 2 for No." << endl;
		cin.ignore();
		cin >> iAgain;
		goto ducheck;
	}
	if (iAgain >= 3) {
		cout << "That's not a valid answer lol, try again" << endl;
		cout << "Do you want to play again? 1 for Yes, 2 for No." << endl;
		cin.ignore();
		cin >> iAgain;
		goto ducheck;
	}
	}


	return 0;
}


the cin.ignore(); makes it so that if you enter 10.11 or whatever, it ignores that...I haven't gotten to that section in the book to check and make sure that it is an int not a long or whatever, but that would be easy. I would assume it would also be easy to do with char, I'm just lazy and already had that built. Also, clearly, OR hasn't registered in my c++ vocabulary yet.
Topic archived. No new replies allowed.