a program for displaying high and low number using do while

i'm trying to find out the highest and lowest number.. but compiler shows negative values in output when i enter a large number i.e 01234556

#include<iostream.h>
#include<conio.h>
void main()
{
int num,high,low,r,n,i,val;
clrscr()
cout<<"enter value";
cin>>val;
n=val;
high=n%10;
low=n%10;
n=n/10;
i=n;
do
{
r=i%10;
if(r>high)
high=r;
if(r<low)
low=r;
i=i/10;
}
while(i>=1)
cout<<"highest number is:"<<high<<endl;
cout<<"lowest number is:"<<low;
getch();
}
actually i've recently joined this forum and i've no idea how to write code while asking question.
Last edited on
I'm sorry, but I don't understand your question. Google translate was not helpful. Can you phrase it in English?

It looks to me like your program is trying to find the highest and lowest digit in a number. What problem are you having?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.

I'm sorry, but I don't understand your question. Google translate was not helpful. Can you phrase it in English?

It looks to me like your program is trying to find the highest and lowest digit in a number. What problem are you having?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.


What he said. BTW why is your title in English yet you ask a question in whatever that language is?

actually i've recently joined this forum and i've no idea how to write code while asking question.


When putting code in you use the <> format button on the right of text entry box. You can also use " button when quoting another person.

As for your code there are a few things wrong with it.
This is your code

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
 #include<iostream.h>
#include<conio.h>
void main()
{
	int num, high, low, r, n, i, val;
	clrscr()
		cout << "enter value";
	cin >> val;
	n = val;
	high = n % 10;
	low = n % 10;
	n = n / 10;
	i = n;
	do
	{
		r = i % 10;
		if (r>high)
			high = r;
		if (r<low)
			low = r;
		i = i / 10;
	}
	while (i >= 1)
	cout << "highest number is:" << high << endl;
	cout << "lowest number is:" << low;
	getch();


Here is the corrected code with comments.

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
#include <iostream> // should not include the ".h" at end
// this may be compiler dependant I'm not sure as I'm fairly new to 
// programming

#include <conio.h>

using namespace std; // you need to include the standard library namespace
// or alternatively all the std library functions need to include a std:: prefix.
// example std::cout << "whatever";

int main() // <-- main function returns a value of type int (integer)
{
	int num, high, low, r, n, i, val;
        // removed line clrscr() as it wasn't included in headers and notice 
        // a missing semi colon after the function call.
	cout << "enter value";
	cin >> val;
	n = val;
	high = n % 10;
	low = n % 10;
	n = n / 10;
	i = n;
	do
	{
		r = i % 10;
		if (r>high)
			high = r;
		if (r<low)
			low = r;
		i = i / 10;
	}
	while (i >= 1); // <-- while in do while loops require a semi colon after
                        // the expression. 
	cout << "highest number is:" << high << endl;
	cout << "lowest number is:" << low;
	_getch(); // apparently getch(); is depreciated and needed to be changed
                  // to _getch();
}


The results after running the corrected program was

highest number is:6
lowest number is:1

When the number you stated above was entered (01234556).

Why 0 wasn't the lowest I will leave up to you to figure out.

Final thoughts are this.

You should get a IDE with syntax highlighting as this will point out obvious code flaws.

I suggest getting something like codeblocks with a compiler.
Here is a link http://www.codeblocks.org/downloads/26

You need to grab the one with the mingw included as that is the IDE with compiler included.

Please mark this thread as solved (button above the thread name) if you accept this answer. As it just makes it easier for others to identify if there is a ongoing investigation. We are here to help, help us help you. :P
Last edited on
what is the difference between getch() and _getch()??
As a budding programmer you are going to have to do a lot of this and it will be a big part of learning to grow into a experienced programmer. There are many tools available to find such information, one such tool is google.

I have personally not used the getch function in my programming, but I was instructed by my IDE that it has been depreciated and _getch was infact the newer version. That is one of the perks of using a IDE that displays relevant information.
Last edited on
thank you
Topic archived. No new replies allowed.