guess number 3 times

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 26
Reputation: erialclaire_238 has a little shameless behaviour in the past 
Solved Threads: 0
erialclaire_238 erialclaire_238 is offline Offline
Light Poster

guess number 3 times

 
-2
  #1
Sep 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,862
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: guess number 3 times

 
1
  #2
Sep 16th, 2009
Originally Posted by erialclaire_238 View 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.
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.

  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 :

  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 niek_e; Sep 16th, 2009 at 6:02 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 319
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 57
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: guess number 3 times

 
1
  #3
Sep 16th, 2009
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.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 26
Reputation: erialclaire_238 has a little shameless behaviour in the past 
Solved Threads: 0
erialclaire_238 erialclaire_238 is offline Offline
Light Poster

tried it

 
0
  #4
Sep 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 26
Reputation: erialclaire_238 has a little shameless behaviour in the past 
Solved Threads: 0
erialclaire_238 erialclaire_238 is offline Offline
Light Poster

Re: guess number 3 times

 
0
  #5
Sep 23rd, 2009
waah! wrong code tags again. sorry!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 26
Reputation: erialclaire_238 has a little shameless behaviour in the past 
Solved Threads: 0
erialclaire_238 erialclaire_238 is offline Offline
Light Poster

Re: guess number 3 times

 
0
  #6
Sep 23rd, 2009
cpp 4.5
  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. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC