Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ Problem


ghost's Avatar
0 0

I have created a small program, just to get a feel for C++, but every time that I run it keeps printing the first line of the program until stopped with Ctrl + C. Does any one have any solutions as to why. Thanks.


fuser's Avatar
Member
0 -1

wow, I need to be psychic just to help you is it?


ghost's Avatar
0 0

You should post your code.


ghost's Avatar
0 0

#include <cstdlib> #include <iostream>

using namespace std;

int main(void) { ;int iPassword; ;int iPassword2; ;int iPassword3; while(true) (

cout &lt;&lt; &quot;Please enter the password: &quot; ) &lt;&lt; endl;
cin &gt;&gt; iPassword
;if(iPassword = 255)
( 
              cout &lt;&lt; &quot;That is correct.&quot; &lt;&lt; endl);

              
if (iPassword &lt; 255 | iPassword &gt; 255)
(
              cout &lt;&lt; &quot;That is incorrect, please try again.&quot; &lt;&lt; endl);
cout &lt;&lt; &quot;Please enter the second password: &quot; &lt;&lt; endl;
cin &gt;&gt; iPassword2
;if(iPassword2 = 916)
(
               cout &lt;&lt; &quot;That is correct.&quot;) &lt;&lt; endl;
if (iPassword2 &lt; 916 | iPassword2 &gt; 916)
(
               cout &lt;&lt; &quot;That is incorrect, please try again.&quot; &lt;&lt; endl &lt;&lt; endl);
;if(iPassword3 = 5131)
(
               cout &lt;&lt; &quot;ACCESS GRANTED&quot; &lt;&lt; endl &lt;&lt; endl);
if (iPassword3 &lt; 5131 | iPassword3 &gt; 5131)
(
               cout &lt;&lt; &quot;That is incorrect, please try again.&quot; &lt;&lt; endl &lt;&lt; endl);
system(&quot;PAUSE&quot;);
return 0;

}


ghost's Avatar
0 0

Correct me if I'm wrong but it looks like there's a lot of mistakes in this program.


yours31f's Avatar
Retired
10 0

Yes, At a glance I found four.

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

int main(void)
{
    int iPassword;  // also just noticed that you had ; at the beginning, don&#39;t put then there.
    int iPassword2;
    int iPassword3;
    while(true)
    (
               
    cout &lt;&lt; &quot;Please enter the password: &quot;  &lt;&lt; endl; // the parentheses
    cin &gt;&gt; iPassword;
if(iPassword == 255)  
  // you had to set this as == not = or it will set it as  255
    ( 
                  cout &lt;&lt; &quot;That is correct.&quot; &lt;&lt; endl);

                  
    if (iPassword &lt; 255 | iPassword &gt; 255)
    (
                  cout &lt;&lt; &quot;That is incorrect, please try again.&quot; &lt;&lt; endl);
    cout &lt;&lt; &quot;Please enter the second password: &quot; &lt;&lt; endl;
    cin &gt;&gt; iPassword2;
if(iPassword2 == 916) //same here
    (
                   cout &lt;&lt; &quot;That is correct.&quot;) &lt;&lt; endl;
    if (iPassword2 &lt; 916 | iPassword2 &gt; 916)
    (
                   cout &lt;&lt; &quot;That is incorrect, please try again.&quot; &lt;&lt; endl &lt;&lt; endl);
    if(iPassword3 == 5131) // and here and an extra ; at the begining
    (
                   cout &lt;&lt; &quot;ACCESS GRANTED&quot; &lt;&lt; endl &lt;&lt; endl);
    if (iPassword3 &lt; 5131 | iPassword3 &gt; 5131)
    (
                   cout &lt;&lt; &quot;That is incorrect, please try again.&quot; &lt;&lt; endl &lt;&lt; endl);
    system(&quot;PAUSE&quot;);
    return 0;
}```

ghost's Avatar
0 0

skathgh420 wrote: Correct me if I'm wrong but it looks like there's a lot of mistakes in this program. Indeed there are, I couldn't quite get my head around it all. SpeedyJ250, if you're just getting the feel of C++ I'd suggest you'd start off by doing something simpler just to get the hang of the individual functions you're trying to use. Also what yours31f said about using two = signs when comparing, same goes for most other things as well, such as |, which could in your attempt be easily replaced with an "else" statement.


ghost's Avatar
0 0

Also you can surely use more efficient coding practices, like if else statments. Pseudocode:

if password = 619: print "That is correct" else: print "That is incorrect"


yours31f's Avatar
Retired
10 0

I also added that you don't need to put semicolons in the beginning of a command.