repeating part of a function..please very urgent!!!
Jun 9, 2013 at 5:57am UTC
i want to make a funcion to ask for mark and then keep asking if the mark is below
0 and above 100.
i want to know how to repeat just a part of a program..
ive did some coding but its seems to not doing the right thing, i dont know why.
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
#include <iostream>
#include <stdlib.h>
using namespace std;
int readValidMark();
int main()
{
readValidMark();
cout<<"\n" ;
system ("pause" );
return 0;
}
int readValidMark()
{
int mark;
cout<<"enter mark" ;
cin>>mark;
if ((mark>0 )&& (mark<100))
{
cout<<"the mark is" <<mark;
}
else
{
do
{
cout<<"renter mark" ;
cin>>mark;
}
while ((mark<0)&&(mark>100));
}
}
Jun 9, 2013 at 6:05am UTC
well look at your while loop. You repeat if a number is less than 0 and greater than 100 would be pretty hard. Try putting || on line 43.
Jun 9, 2013 at 6:14am UTC
thanks very sir for your help..
Jun 9, 2013 at 6:20am UTC
Recursive:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
int read_valid_mark()
{
int mark;
std::cout << "enter mark: " ;
if ( std::cin >> mark )
{
if ( mark >= 0 && mark <= 100 ) return mark ;
else std::cout << "error: out of range\n" ;
}
else // non-numeric input
{
std::cout << "error: not a number\n" ;
std::cin.clear() ; // clear error state
std::cin.ignore( 1000, '\n' ) ; // throw the junk away
}
return read_valid_mark() ; // try again
}
Last edited on Jun 9, 2013 at 6:21am UTC
Topic archived. No new replies allowed.