Computing.Net > Forums > Programming > simple loop assignment

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

simple loop assignment

Reply to Message Icon

Original Message
Name: justin laruwe
Date: November 4, 2002 at 19:55:51 Pacific
Subject: simple loop assignment
OS: win xp
CPU/Ram: 128
Comment:

i can not figure out this problem can any of you help it should be easy its for my intro programing class?


Write a program that includes a function named checkNum. The program contains a loop that prompts the user to input an integer between or including 1 and 10. After the user has inputted a value, pass the value to the function checkNum. If the value is not between or including 1 and 10, the function should display the message "Try again". The loop in the main program should keep prompting the user to input a number until he has successfully inputted a number between or including 1 and 10.



Report Offensive Message For Removal


Response Number 1
Name: Tom
Date: November 4, 2002 at 20:48:51 Pacific
Reply: (edit)

int x = 0

cout>x
do

if(x >= 1 && x 10))


this should be it, could be wrong, seeming I haven't done c++ in ages.


Report Offensive Follow Up For Removal

Response Number 2
Name: Tom
Date: November 4, 2002 at 20:53:52 Pacific
Reply: (edit)

dunno why, but half it got deleted, here it is:

int x = 0;
cout>x;

do
{

if(x >= 1 && x 10))

this should be it, could be wrong, seeming I haven't done c++ in ages.



Report Offensive Follow Up For Removal

Response Number 3
Name: Tom
Date: November 4, 2002 at 20:54:30 Pacific
Reply: (edit)

For some reason it wont let me post it, sorry bud :(


Report Offensive Follow Up For Removal

Response Number 4
Name: Whisky Bob
Date: November 5, 2002 at 05:51:11 Pacific
Reply: (edit)

Try this:

// X = the vaule that the user typed in
// the previos function that you sed you
// have made

void checknum()
{

if (x >= 1 | x < 11)
{ cout << "Succes!!\n"; some_function(); }
else
{ cout << "Wrong input!! \n"; }
}

Im a little rusty on the IF stuff so
this might be wrong as well!!!

PS: If you are a complete newbie to C++
you need to know some other stuff.
And if thats the case contact me

whisky_bob@hotmail.com


Report Offensive Follow Up For Removal

Response Number 5
Name: Don Arnett
Date: November 5, 2002 at 08:27:14 Pacific
Reply: (edit)

The if should have an 'and' ('&&'), not a binary or ('|').


Report Offensive Follow Up For Removal


Response Number 6
Name: Tom
Date: November 5, 2002 at 08:54:09 Pacific
Reply: (edit)

it needs a do while loop, cause it has to keep doing it until you get a # that is suitable.


Report Offensive Follow Up For Removal

Response Number 7
Name: Ronin
Date: November 5, 2002 at 11:24:14 Pacific
Reply: (edit)

#include [iostream.h]

short checkNum(int);

int main(void)
{

short flag;
int input;

flag = -1;

for(;;)
{
cout [[ "Enter a number between 1 and 10: ";
cin ]] input;

flag = checkNum(input);

if(flag == 0) break;
}

return 0;
}

short checkNum(int number)
{
short flag;

if(number ] 10 || number [ 1)
{
flag = -1;
cout [[ "Try again" [[ endl;
}
else
{
flag = 0;
cout [[ "Bingo" [[ endl;
}

return flag;
}

Since this is an intro class, this example is very basic to say the least, yet it *could* offer you some insight as to how to approach the problem.

HTH


Report Offensive Follow Up For Removal

Response Number 8
Name: Christian
Date: November 5, 2002 at 22:48:42 Pacific
Reply: (edit)

"flag" should be boolean also checksum, and checksum can be shorter.


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software