943,810 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 850
  • C++ RSS
Sep 16th, 2009
-2

guess number 3 times

Expand Post »
I'm having a problem on this one. using a for loop i need to make a user guess a number from 1-10 with only three tries,,, the correct number is 3 . It's running and all but it keeps showing the cout 'Your password is correct' & Program Over everytime. suggestions pls.

#include<iostream.h>
main()
{
int i, pass;

cout<<"\n\t Enter a number:";
for(i=1;i<=1;i++)
{
cin>>pass;
}
{
if (pass==3)
{
cout<<"\n\t The password you entered is correct";
}
else
{
cout<<"\n\t Enter a number:";
cin>>pass;
}
if (pass==3)
{
cout<<"\n\t The password you entered is correct";
}
else
{
cout<<"\n\t Enter a number:";
cin>>pass;
i++;
}
{
cout<<"\n\t Program Over!!!";
}
}
return 0;
}
Last edited by happygeek; Sep 17th, 2009 at 6:51 am. Reason: TEX tag removed
Reputation Points: -6
Solved Threads: 0
Light Poster
erialclaire_238 is offline Offline
26 posts
since Feb 2009
Sep 16th, 2009
1

Re: guess number 3 times

I'm having a problem on this one. using a for loop i need to make a user guess a number from 1-10 with only three tries,,, the correct number is 3 . It's running and all but it keeps showing the cout 'Your password is correct' & Program Over everytime. suggestions pls.
First of all: read this about using code-tags!! Then learn how to indent your code.

There's a lot wrong with this code. I've commented in red:

#include<iostream.h> // should be <iostream> without the .h
main() // should be int main()
{
int i, pass; // always give your vars a value: int i=0,pass=0;

cout<<"\n\t Enter a number:";
// this loop does nothing because i=1 and it loops until i is smaller then or equal to one, wich will happen on the first pas
for(i=1;i<=1;i++) 
{
cin>>pass;
} // you probably don't want this brace here. It ends your loop.
{ // <-- useless brace
if (pass==3)
{
cout<<"\n\t The password you entered is correct";
}
else
// From here on, everything is wrong. 
// You don't need to write the same code over and over again.
// That's what loops are for
{
cout<<"\n\t Enter a number:";
cin>>pass;
}
if (pass==3)
{
cout<<"\n\t The password you entered is correct";
}
else
{
cout<<"\n\t Enter a number:";
cin>>pass;
i++; // <--- don't do this. I is incremented by the for-loop
}
{// <-- useless brace
cout<<"\n\t Program Over!!!";
}// <-- useless brace
}// <-- useless brace
return 0;
}

I strongly recommend that you read this tutorial on loops. It is obvious that you don't understand them very well.

Here's something to get you started. Try it and play around with it.

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5.  
  6. int main(){
  7. for (int i = 0; i < 3; i++){ // loop 3 times
  8. int user_number = 0;
  9. cout << "Enter a number: ";
  10. cin >> user_number;
  11. cout << "Number " << i+1 << " entered was: " << user_number << '\n';
  12. }
  13. }

You're probably using the Ancient Turbo C++ compiler. If this is the case, change the above code to :

c++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2.  
  3. int main(){
  4. for (int i = 0; i < 3; i++){ // loop 3 times
  5. int user_number = 0;
  6. cout << "Enter a number: ";
  7. cin >> user_number;
  8. cout << "Number " << i+1 << " entered was: " << user_number << '\n';
  9. }
  10. }

And when you're done trying the code, upgrade your compiler to some free, and up-to-date
Last edited by Nick Evan; Sep 16th, 2009 at 6:02 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 16th, 2009
1

Re: guess number 3 times

Considering the problem you have set, your algorithm is completely wrong. And as already mentioned by niek_e, you're using the for loop incorrectly too!

Follow niek_e's advice and take a good look at using for loops.
His post shows you how to correctly use them..
And as he's already said, you should change void main to int main or risk Salems wrath heh heh!

Going back to your actual algorithm, what you need to be doing inside main() is something like:
1. initialise your variables, include a boolean flag to check whether the user was correct (initialise it to false).
2. set up your for loop to iterate three times
Inside the loop you need to do this:
- Ask user to enter a value between 1 and 10
- If the entered value matches the password (3?), set your flag to true and break out of the loop
- else if the value is less than one or greater than 10, display an error message.
- else inform the user that they guessed incorrectly..

(finally after the for loop)
3. check the value of your flag.
- if true, the user guessed correctly during the for loop.
- else user guessed incorrectly 3 times and failed!

Hope that's of some help!
Cheers for now,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Sep 23rd, 2009
0

tried it

I'm actually using an cpp 4.5 and
i think boolean flags aren't recognized.
I tried to do as you say and yes once the right number is entered the program gives the right output but if the user enters the wrong value thrice the message "program over" won't appear but the program does ends.
Last edited by erialclaire_238; Sep 23rd, 2009 at 8:20 pm. Reason: wrong use of code tags
Reputation Points: -6
Solved Threads: 0
Light Poster
erialclaire_238 is offline Offline
26 posts
since Feb 2009
Sep 23rd, 2009
0

Re: guess number 3 times

waah! wrong code tags again. sorry!
Reputation Points: -6
Solved Threads: 0
Light Poster
erialclaire_238 is offline Offline
26 posts
since Feb 2009
Sep 23rd, 2009
0

Re: guess number 3 times

cpp 4.5
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. int main()
  3. {
  4. int i=0, pass=0;
  5. cout<<"Enter a number between 1 to 10:";
  6. for(i=0; i<3; i++)
  7. {
  8. cin>>pass;
  9. if (pass==3)
  10. {
  11. cout<<"The passcode you entered is correct!"; break;
  12. }
  13. else if (pass!=3)
  14. cout<<"Enter a number between 1 to 10:";
  15. else
  16. cout<<"Program Over"; }
  17. return 0;
  18. }
Reputation Points: -6
Solved Threads: 0
Light Poster
erialclaire_238 is offline Offline
26 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: question on weather i should use a templat
Next Thread in C++ Forum Timeline: Comparing a misspelled (scrambled) word to a list of words





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC