Jump to content

[HELP]How do I break between If else on C++?

rcarlos243
Go to solution Solved by MSVSora,
2 minutes ago, rcarlos243 said:

I have no idea how to do it :(

A switch case is written by doing the following

 

//enums also work here
switch(integer variable)
{
	case integer value:
        //code here
        break;
    case integer value2:
        //code here
        break;
    default:
        //code here
        break;
}

Though this wouldn't help you much, personally I would write the code something like this

 

const int maxNrOfTrys = 5;

for (int i = 0; i > 0; i = maxNrOfTrys; --i)
{
    cout << "Enter your password ";
    getline(cin, input);
    
    if ( input == "P@ssw0rd")
    {
        cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
        break;
    }
    else
    {
        cout << "incorrect, password! " << i <<" more try limit\n";
    }
}

 

Spoiler

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#include <cmath>

main()
{
    
    string input; //
    int i;
    i = 4;
    
    
    int years = 10;
    int D = 100;
    //output (calculated values)
    cout << fixed << setprecision(2);
    double p = 0.075 / 12; // 7.5% annual interest rate
    double T = years * 12;
    double S = D * ((pow(1 + p, T) -1) / p);
    
    while(true)
    {
        if (i == 0) break;
        i = i - 1;
            cout << "Enter your password " ;
        getline(cin, input);
        
        if ( input == "P@ssw0rd")
            cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
            
        
        else
            cout << "incorrect, password! " << i <<" more try limit\n";
    }
    cout << "You Have exceeded the limit... try later.";
}

The problem is I want to end the loop by using break after the if statement but if I put break in between if and else, it complains "error: ‘else’ without a previous ‘if’"

Spoiler

 while(true)
    {
        if (i == 0) break;
        i = i - 1;
            cout << "Enter your password " ;
        getline(cin, input);
        
        if ( input == "P@ssw0rd")
            cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
            break;
        
        else
            cout << "incorrect, password! " << i <<" more try limit\n";
    }

 

Also how do I get rid of "incorrect, password! 0 more try limit" If I got through the four attempt limit?

Spoiler

hqSW3BM.png

 

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, itsmyjobtoknow said:

Have you considered a switch case?

I have no idea how to do it :(

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

The problem you are seeing here is because of your scope. In C++ a scope is defined by the '{' and '}' brackets. When you use "if(bool) statement;" it basically just creates the scope for the single line, so when you are trying to use break, it creates it after that scope, so your code actually looks something like this

 

while(true)
{
    if (i == 0)
    {
        break;
    }
    i = i - 1;
    cout << "Enter your password " ;
    getline(cin, input);
    
    if ( input == "P@ssw0rd")
    {
        cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
    }
    break;
    else
    {
        cout << "incorrect, password! " << i <<" more try limit\n";
    }
}

while what you probably want is something more like this

 

while(true)
{
    if (i == 0)
    {
        break;
    }
    i = i - 1;
    cout << "Enter your password " ;
    getline(cin, input);
    
    if ( input == "P@ssw0rd")
    {
        cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
        break;
    }
    else
    {
        cout << "incorrect, password! " << i <<" more try limit\n";
    }
}

 

also rather than and a while you probably want to use a for loop

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, rcarlos243 said:

I have no idea how to do it :(

A switch case is written by doing the following

 

//enums also work here
switch(integer variable)
{
	case integer value:
        //code here
        break;
    case integer value2:
        //code here
        break;
    default:
        //code here
        break;
}

Though this wouldn't help you much, personally I would write the code something like this

 

const int maxNrOfTrys = 5;

for (int i = 0; i > 0; i = maxNrOfTrys; --i)
{
    cout << "Enter your password ";
    getline(cin, input);
    
    if ( input == "P@ssw0rd")
    {
        cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
        break;
    }
    else
    {
        cout << "incorrect, password! " << i <<" more try limit\n";
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, MSVSora said:

The problem you are seeing here is because of your scope. In C++ a scope is defined by the '{' and '}' brackets. When you use "if(bool) statement;" it basically just creates the scope for the single line, so when you are trying to use break, it creates it after that scope, so your code actually looks something like this

 


while(true)
{
    if (i == 0)
    {
        break;
    }
    i = i - 1;
    cout << "Enter your password " ;
    getline(cin, input);
    
    if ( input == "P@ssw0rd")
    {
        cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
    }
    break;
    else
    {
        cout << "incorrect, password! " << i <<" more try limit\n";
    }
}

while what you probably want is something more like this

 


while(true)
{
    if (i == 0)
    {
        break;
    }
    i = i - 1;
    cout << "Enter your password " ;
    getline(cin, input);
    
    if ( input == "P@ssw0rd")
    {
        cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S ;
        break;
    }
    else
    {
        cout << "incorrect, password! " << i <<" more try limit\n";
    }
}

 

also rather than and a while you probably want to use a for loop

It actually fixed the program, but I have another problem, I got the "Have exceeded limit... try later which is not supposed to output.


FJO8XGc.png

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, rcarlos243 said:

It actually fixed the program, but I have another problem, I got the "Have exceeded limit... try later which is not supposed to output.


FJO8XGc.png

This shouldn't really compile as far as I can tell, you have 1 too many brackets, though this may just be the way you have done your indentation.

 

Anyway I would recommend you define a bool above the while something like "bool usedCorrectPassword = false;" and then in your if after you output the in years ect but before the break you add "userCorrectPassword = true;" and then on the cout for "Have exceeded the limit... try later." you surround it with "if(!usedCorrectPassword)". That way you will check so if they use the correct password, and if they did you wont output the text

Link to comment
Share on other sites

Link to post
Share on other sites

No wonder it does not work, that break does not belong to the if block (which consists of a single line, because nothing is enclosed in curly brackets). So you're basically saying to the compiler: if this, do this (the single line), then break regardless of anything, then else (compiler is going "wat? where the f is it's if?" at that moment and gives you the error). Else has to go immediately after the if block, so put your brackets accordingly, or rethink the place entirely if this does not work logically.

 

EDIT:

46 minutes ago, rcarlos243 said:

It actually fixed the program, but I have another problem, I got the "Have exceeded limit... try later which is not supposed to output.


FJO8XGc.png

Why is it bad? It's the expected behavior by looking at this code. You break out of the infinite while cycle and immediately print "Have exceeded..." line. I guess you're probably confused by your own messy code, proper indentation is devised for a reason you know.

Link to comment
Share on other sites

Link to post
Share on other sites

@rcarlos243  I would say looking at the screenshot of your code line 44 should go inside the if statement (on line 25) before the break (on line 27). You only want the Have exceeded limit message to show if the user has tried to re enter the incorrect password 4 times in a row?

 

As your code currently is that will message will always be outputted no matter what you do inside the while loop.

Link to comment
Share on other sites

Link to post
Share on other sites

while(true)
{
    if (i == 0)
    {
        cout << "Have exceeded the limit... try later" << endl;
        break; // return -1 or handle the problem outside?
    }
    i = i - 1;
    cout << "Enter your password " ;
    getline(cin, input);
    
    if ( input == "P@ssw0rd")
    {
        cout << "In " << years << " years, $" << D << " deposited per month will grow to $" << S << endl;
        break;
    }
    else
    {
        cout << "incorrect, password! " << i <<" more try limit\n" << endl;
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, DevBlox said:

No wonder it does not work, that break does not belong to the if block (which consists of a single line, because nothing is enclosed in curly brackets). So you're basically saying to the compiler: if this, do this (the single line), then break regardless of anything, then else (compiler is going "wat? where the f is it's if?" at that moment and gives you the error). Else has to go immediately after the if block, so put your brackets accordingly, or rethink the place entirely if this does not work logically.

 

EDIT:

Why is it bad? It's the expected behavior by looking at this code. You break out of the infinite while cycle and immediately print "Have exceeded..." line. I guess you're probably confused by your own messy code, proper indentation is devised for a reason you know.

Im sorry I am a complete noob on programming and have no idea how to do it properly.

 

My idea is

 

-if the user gets input the correctly: it should say the if statement only.

 

-or else the user has 4 times to enter the input correctly and each time the user got it wrong it should say "Incorrect password",

 

-if that 4 times allowable try is exceeded. it should output have exceeded the limit... try later

 

I don't know how to properly do it :(

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, C2dan88 said:

@rcarlos243  I would say looking at the screenshot of your code line 44 should go inside the if statement (on line 25) before the break (on line 27). You only want the Have exceeded limit message to show if the user has tried to re enter the incorrect password 4 times in a row?

 

As your code currently is that will message will always be outputted no matter what you do inside the while loop.

THIS FIXED IT!!!.

Spoiler

Hq3MZHV.png

It is almost perfect, only thing left is I don't know how to fix(remove it from output) the "incorrect password, 0 try limit"

nS2Wnvv.png 

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

In your else statement add "if(i != 0){ your string thingy; }" <- this way only if you have tries remaining will it output the message

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, MSVSora said:

In your else statement add "if(i != 0){ your string thingy; }" <- this way only if you have tries remaining will it output the message

This also fixed it.

 

I am not sure what is going on, what is the if(i !=0) doing to the program?

Yeah, we're all just a bunch of idiots experiencing nothing more than the placebo effect.
Link to comment
Share on other sites

Link to post
Share on other sites

On 3/7/2016 at 9:07 PM, rcarlos243 said:

This also fixed it.

 

I am not sure what is going on, what is the if(i !=0) doing to the program?

It checks to see if you have any guesses left, if you do then it prints the text, if not then it doesnt

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×