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;
}

Edited 7 Years Ago by happygeek: TEX tag removed

Comments
OMG, after 20 posts you even don't know you shouldn't use TEX-tags to write your post in. Apart from that, your code is one big mess. Follow the suggestions given below and try to learn from it so that this doesn't happen again in future.
> 20 posts and still no clue about tags

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.

#include <iostream>

using std::cin;
using std::cout;

int main(){
    for (int i = 0; i < 3; i++){ // loop 3 times
        int user_number = 0;
        cout << "Enter a number: ";
        cin >> user_number;
        cout << "Number " << i+1 << " entered was: " << user_number << '\n';
    }
}

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

#include <iostream.h>

int main(){
    for (int i = 0; i < 3; i++){ // loop 3 times
        int user_number = 0;
        cout << "Enter a number: ";
        cin >> user_number;
        cout << "Number " << i+1 << " entered was: " << user_number << '\n';
    }
}

And when you're done trying the code, upgrade your compiler to some free, and up-to-date

Edited 7 Years Ago by Nick Evan: n/a

Comments
Good post.

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.

Comments
Nice post :)

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.

Edited 7 Years Ago by erialclaire_238: wrong use of code tags

cpp 4.5

#include<iostream.h>
int main()
{
int i=0, pass=0;
cout<<"Enter a number between 1 to 10:";
for(i=0; i<3; i++)
{
cin>>pass;
if (pass==3)
{
cout<<"The passcode you entered is correct!"; break;
}
else if (pass!=3)
cout<<"Enter a number between 1 to 10:";
else
cout<<"Program Over";  }
return 0;
}
This article has been dead for over six months. Start a new discussion instead.