Looping (do,while)

I'm using loop in my program and it works just fine using do and while, but my problem is the word "Wrong entry.....blah blah blah" (on the last part) doesn't disappear and still there on the screen even though I already input the correct entry.

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
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
float l,w;
char choice;
do
{
	gotoxy(25,6);
	cout<<"PROGRAM #4 - AREA CALCULATOR";
	gotoxy(20,10);
	cout<<"Find the:";
	gotoxy(25,13);
	cout<<"a. Area of Reactangle";
	gotoxy(25,15);
	cout<<"b. Area of Parallelogram";
	gotoxy(25,17);
	cout<<"c. Area of Triangle";
	gotoxy(20,21);
	cout<<"Enter choice:  ";
	cin>>choice;

	if(choice=='a'||choice=='A')
	{
	gotoxy(28,25);
	cout<<"Area of Rectangle";
	gotoxy(29,28);
	cout<<"Enter length: ";
	cin>>l;
	gotoxy(29,30);
	cout<<"Enter width:  ";
	cin>>w;
	gotoxy(31,33);
	cout<<"AREA =  "<<l*w;
	break;
	}

	else if(choice=='b'||choice=='B')
	{
	gotoxy(28,25);
	cout<<"Area of Parallelogram";
	gotoxy(30,28);
	cout<<"Enter length:   ";
	cin>>l;
	gotoxy(30,30);
	cout<<"Enter width:    ";
	cin>>w;
	gotoxy(33,33);
	cout<<"AREA =   "<<l*w;
	break;
	}

	else if(choice=='c'||choice=='C')
	{
	gotoxy(28,25);
	cout<<"Area of Triangle";
	gotoxy(28,28);
	cout<<"Enter height:   ";
	cin>>l;
	gotoxy(28,30);
	cout<<"Enter base:     ";
	cin>>w;
	gotoxy(31,33);
	cout<<"AREA =   "<<(l*w)/2;
	break;
	}

	else
	{
	gotoxy(25,25);
	cout<<"Wrong Entry! That is not in the choices.";
	gotoxy(25,27);
	cout<<" Press Q to quit and C to continue.";
	cin>>choice;
	}
}

while(choice=='C'||choice=='c');
if(choice=='q'||choice=='Q')
{return 0;}


Area of Rectangle
Wrong entry! That is not in the choices.
Press Q to quit and C to continue.
Enter length: 5
Enter width:  9
AREA= 45


^the output is somehow like that. it overwrites the statement/text.
How would i make the statement "wrong entry....blah blah blah" disappear after entering the correct input?
What is this code segment?

gotoxy()


Also don't use as it is non standard.

clrscr();

Use

system("cls")
Last edited on
Try this, but I would maybe write this as a function and not use the do while loop.

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
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
		float l,w;
	char choice;
	do
	{
		system("cls");
		cout << "PROGRAM #4 - AREA CALCULATOR" << endl;
		cout << "Find the:" << endl;
		cout << "a. Area of Reactangle" << endl;
		cout << "b. Area of Parallelogram" << endl;
		cout << "c. Area of Triangle" << endl;
		cout << "Enter choice:  ";
		cin >> choice;

		if(choice=='a'||choice=='A')
		{
			
			cout << "Area of Rectangle" << endl;
			cout << "Enter length: ";
			cin >> l;
			cout << "Enter width:  ";
			cin >> w;
			cout << "AREA =  " << l * w << endl;

		}

		else if(choice=='b'||choice=='B')
		{
			
			cout << "Area of Parallelogram" << endl;
			cout << "Enter length:   ";
			cin >> l;
			cout << "Enter width:    ";
			cin >> w;
			cout << "AREA =   " << l * w << endl;

		}

		else if(choice=='c'||choice=='C')
		{
			
			cout<<"Area of Triangle" << endl;
			cout<<"Enter height:   ";
			cin>>l;
			cout<<"Enter base:     ";
			cin>>w;
			cout<<"AREA =   " << (l * w) / 2 << endl;
						
		}

		else
		{
			
			cout<<"Wrong Entry! That is not in the choices." << endl;
			cout<<"Press Q to quit and C to continue: ";
			cin >> choice;
		}
	}
	while(choice=='C'||choice=='c');
		
		return 0;
}
1
2
3
while(choice=='C'||choice=='c');
if(choice=='q'||choice=='Q')
{return 0;}

what's the meaning of this? can you explain?
it doesn't work. you just change the indentions and put endl instead of gotoxy. for your info, gotoxy is use to position or align your output on the screen and clrscr(); is used so that output would not be repeated on the screen everytime you would run it. what i want to solve is that "wrong entry!...blah blah blah" would disappear after i enter a correct entry so that texts/statements on the output screen would not be overwrite or positioned over each other.
but anyway, thank you still for your effort..
and for others, please help me to solve this. this problem is still not solved....
closed account (Dy7SLyTq)
@chipp: its an infinite loop look at the end of line one
i'm actually ask more to its intention, why he would do something like that? i think it doesn't do any good...
it doesn't work. you just change the indentions and put endl instead of gotoxy. for your info, gotoxy is use to position or align your output on the screen and clrscr(); is used so that output would not be repeated on the screen everytime you would run it. what i want to solve is that "wrong entry!...blah blah blah" would disappear after i enter a correct entry so that texts/statements on the output screen would not be overwrite or positioned over each other.
but anyway, thank you still for your effort..
and for others, please help me to solve this. this problem is still not solved....


Well I am using Visual Studio 2010 and it worked fine for me. It didn't display the "Wrong Entry! That is not in the choices.", I also removed the gotoxy b/c outside of the class it shouldn't be used. At least thats what the book's I have been reading suggested. Anyhow since my fix didn't fix your issue, I hope someone more knowledgeable here will help.
Last edited on
line 65 in the original code is not an infinite loop, it is the while part of the do loop. This is one reason why I really dislike do loops, unless there is a situation where they are required. It is easy to miss this because the while statement isn't on the same line as the closing brace of the do loop.

I prefer to do this sort of thing like this :

http://www.cplusplus.com/forum/beginner/104553/2/#msg564228


When using chars instead of ints, one can make use of the toupper function to make the logic easier. That is, there is only one thing to compare to, instead of two things - lower and upper case.

Also notice that there is a Quit case, and a default case to catch bad input.

And, it is often clearer to have each case call a function, rather than have a big long switch statement. The same applies to if-else-if-else statements.

system("cls")

This is not a good idea either, have a read of this article by Duoas

http://www.cplusplus.com/forum/articles/11153/


Hope all goes well.
I know what your problem is on the last else control flow loop add in continue; after cin >> choice. By the way the user cannot see their answer so put system ("PAUSE"); at the end of all of the decisions so the have to press a key to continue the rest of the program hence giving them enough time to see their answers.
Cheer
123 while(choice=='C'||choice=='c');
if(choice=='q'||choice=='Q')
{return 0;}


what's the meaning of this? can you explain?


@chipp it is used for looping (or repeating the function without retyping it). i need that coz what if someone would put a wrong entry/input.
___________
btw, i'm using turbo c++ :)
when i use system(""); it says error..
And did you read the link I posted?
turbo c++? I recommend either codeblocks or visual studio. They are both free and I find them both very reliable. I made the two changes I previously stated in codeblocks and the program ran perfectly fine. If your compiler says system ("cls"); or system ("PAUSE"); is an error then it is just junk.
As I said above, doing system anything is a bad idea, read the article by Duoas I linked.

And turbo C++ is really ancient, but some places like India unfortunately require it for school exams, so the students are forced to use it.

I use KDevelop or QtCreator on Fedora 17 Linux.
//This ran flawlessly!:D
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <cstdio>

using namespace std;

int main()
{
float l,w;
char choice;
do
{
system ("cls");
cout << "PROGRAM #4 - AREA CALCULATOR" << endl;
cout << "Find the:" << endl;
cout << "a. Area of Reactangle" << endl;
cout << "b. Area of Parallelogram" << endl;
cout << "c. Area of Triangle" << endl;
cout << "Enter choice: ";
cin >> choice;

if(choice == 'a'||choice == 'A')
{

cout << "Area of Rectangle" << endl;
cout << "Enter length: ";
cin >> l;
cout << "Enter width: ";
cin >> w;
cout << "AREA = " << l * w << endl;
system ("PAUSE");

}

else if(choice == 'b'||choice == 'B')
{

cout << "Area of Parallelogram" << endl;
cout << "Enter length: ";
cin >> l;
cout << "Enter width: ";
cin >> w;
cout << "AREA = " << l * w << endl;
system ("PAUSE");

}

else if(choice == 'c'||choice == 'C')
{

cout<<"Area of Triangle" << endl;
cout<<"Enter height: ";
cin>>l;
cout<<"Enter base: ";
cin>>w;
cout<<"AREA = " << (l * w) / 2 << endl;
system ("PAUSE");

}

else
{

cout<<"Wrong Entry! That is not in the choices." << endl;
cout<<"Press Q to quit and C to continue: ";
cin >> choice;
continue;
}
}
while(choice=='C'||choice=='c');

return 0;
}
Last edited on
zreuille98 wrote:
//This ran flawlessly!:D


It may have done, but I will shamelessly say that mine is better !

When submitting code please always use the code tags. Select your code then press the <> button on the right under format.
Thank you for the advice TheIdeasMan. Also, That is perfectly fine due to the fact that I am only 15 lol. I hope to be as pragmatic of a programmer as you all someday in the future. Let us know if you have any more issues mmep93.
Cheers
Thank you all for your help! I already solved it...
I put clrscr(); and I guess it's just the same with system("CLS")Thank you again :D

And before I close this topic, I just want to ask if the reason why mine says error on system("CLS") and sytem("PAUSE") is because I didn't indicate the correct library for it? is that right?

P.S. I am using Turbo C++ because it is the one being used in our school but I have also used CodeBlocks before. :)
Last edited on
Topic archived. No new replies allowed.