2 questions about c++

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;
    	}
I would use a Boolean to loop until the user gets it correct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

bool bWhileLooping = true;

while( bWhileLooping )
    {
    // Get user's input:

    // Check user's input:

    if( userInput == password )
        {
        cout << "The password has been accepted!" << endl;

        bWhileLooping = false;

        }
else
        {
        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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>


using namespace std;

int main()
{
	string password = "123";

	cout << "Please enter your password: " << endl;

	string userInput;
	cin >> userInput;

	cout << "You have entered: " << userInput << endl;


	bool bWhileLooping = true;

	while( bWhileLooping )
	    {
	    // Get user's input:

	    // Check user's input:

	    if( userInput == password )
	        {
	        cout << "The password has been accepted!" << endl;

	        bWhileLooping = false;

	        }
	else
	        {
	        cout << "Access denied!" << endl;

	        }

	    }

	return 0;
}


Where is the mistake? I just copied your code.
closed account (z05DSL3A)
MurVanSam wrote:
Where is the mistake? I just copied your code.
yep, that would be it.



Move the part of your code that gets input from the user to the part where there is a comment about // Get user's input:
So you mean like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
#include <string>


using namespace std;

int main()
{
	string password = "123";

	cout << "Please enter your password: " << endl;
	string userInput;

	bool bWhileLooping = true;

	while( bWhileLooping )
	    {
			cin >> userInput;

			cout << "You have entered: " << userInput << endl;

	    if( userInput == password )
	        {
	        cout << "The password has been accepted!" << endl;

	        bWhileLooping = false;

	        }
	else
	        {
	        cout << "Access denied!" << endl;

	        }

	    }

	return 0;
}


I keep getting this error:

c:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file 1.exe: Permission denied
collect2.exe: error: ld returned 1 exit status
make: *** [1] Error 1
makefile:45: recipe for target '1' failed

17:38:24 Build Finished (took 199ms)

What does this mean?
The linker does not like the name of your executable file. Try giving it a legal file name. Something other than 1.exe

Its working, thanks guys!!

But could you give me the answer for the other question I asked?

The question was "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
  int some_num = 21;
  int result; 
...
  result = some_num * 2;


Is that that you mean? If not, you will have to clarify what you mean.

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.
Last edited on
You need to pass x to your function:

1
2
3

multiply( x );


Of course, save this result.
Oh yea, I'm sorry. just a little mistake.

But how exactly do I store a result?

What do I have to write after

"cout << "The result of x is: " << multiply(x) << endl;"

to store the result?

just "int result = x" or something like that?
Yep!
Hmm, seems like I must have something wrong, because the output keeps telling me that the result is 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <iostream>
#include <string>

int multiply(int x)
{
    return 2 * x;
}


int main()
{
    using namespace 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"
Last edited on
Are you serious?

int result = multiply( x );
No need to be like that mate.

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?".

Thanks anyways, it worked.
Last edited on
closed account (48T7M4Gy)
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
:)
Last edited on
Sounds good to me, thanks for the advice, kemort :).
Sorry for the snarky reply, MurVanSam: I apologize. Please mark this as solved (if it is.)
No Problem mate, I'd like to be one of your kind and looking forward to learning more from you guys :).
Thanks! There are a lot of very good programmers on this site! Good luck!
Topic archived. No new replies allowed.