Hi there! I'm trying to figure out how to loop the if statement if its conditions are not met.
This is just a simple password check system, where the password has been defined as string "123". If I type something else than 123, the msg of the 2nd if statement is being shown "Access denied". The thing I want is, that if I write something else than "123" and the 2nd if statement pops up, to return to the question what the password is. I'm not sure how to do it, maybe something with bool or do...while loop? I tried but I failed, could somebody help me please?
And the other question is, that if I make a calculation in the main(), to store the result and multiply it by 2. How can I do that?
1 2 3 4 5 6 7 8 9 10 11 12
if(userInput == password)
{
cout << "The password has been accepted!" << endl;
}
if(userInput != password)
{
cout << "Access denied" << endl;
}
Hmm, seems like if I overwrite my if statement codes with your bool code and run the program, it constantly loops "Access Denied" and does not stop no matter what I do. I better show the whole code:
I haven't tried your shown code but lets make an example:
lets say
we have a function above the main()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int multiply(int x)
{
return 2 * x;
}
int main()
{
int x;
cin >> x;
cout << "The result of x is: " << multiply << endl;
// how does it go on? How can I store the result of the by 2 multiplied x?
// lets say I managed to store it, I'd like to continue like this:
cout << "now lets multiply the stored result of the stored x by 2 again" << endl;
I want to multiply x by 2 again which already was multiplied by the function above, how is that possible? Lets say I enter "2"(x=2) and it is multiplied by 2, so the result would be 4, right? Then the main function goes on storing the 4 in an integer(maybe, or what else?) and then another new function comes which multiplies the stored 4 by 2 times and the endresult would be 8.
#include <iostream>
#include <string>
int multiply(int x)
{
return 2 * x;
}
int main()
{
usingnamespace std;
cout << "Enter the number you would like to multiply!" << endl;
int x;
cin >> x;
cout << "you have entered: " << x << endl;
cout << "The result of x is " << multiply(x) << endl;
int result = x;
cout << "so, lets try to multiply the result by 2" << endl;
cout << result * 2 << endl;
return 0;
}
Not sure what I'm doing wrong here, somene help please :).
Output:
"Enter the number you would like to multiply!
2
you have entered: 2
The result of x is 4
so, lets try to multiply the result
4"
I'm a completely new to programming and making mistakes is a huge part of learning programming.
I'm trying to get help by asking you guys, because I trust you.
So again, please don't be like "Are you serious?".
As an observation, it might be clearer to rename the function from multiply to something like multiply for program maintenance later on down the track.
Line 2 could be clearer too. "The result from the two function is" etc
:)