I have started learing c++ programming and am just messing around with different commands and seem to have a hard time using prototype functions and executing them. I am using CodeBlocks for everything. My code is to simply set a password, and have the user enter the password to proceed. Seems simple enough, right? As far as I am right now, this is what I have. (I have not gotten any further in this because I found the error early on)
#include<cstdlib>
#include<cstdio>
#include<iostream>
using namespace std;
// prototype declaration
int getPassword(int nPassword);
// declare combo to be matched
const int nCombo = 3141
int main(int nNumberofArgs, char* pszArgs[])
{
getPassword();
return 0;
}
// fetch password from user to compare to nCombo
int getPassword(int nPassword)
{
cout << "Please enter password..." << endl;
cin >> nPassword;
return nPassword;
}
When I run this program as is, the cout line does not appear on the screen, the program terminates, and nothing is done. Please help me with this. very frusturating.
Okay, so there are some issues with your piece of code besides the missing semicolon after you declaration of nCombi
.
In your declaration and definition of int getPassword(int)
you state that the function should take an integer, but you don't provide one when you call the function. Also, the function returns an integer that you don't capture.
I've submitted some fixes below.
#include<cstdlib>
#include<cstdio>
#include<iostream>
using namespace std;
// prototype declaration
int getPassword();
// declare combo to be matched
const int nCombo = 3141;
int main(int nNumberofArgs, char* pszArgs[])
{
// The main control loop. We continue this forever or until the user submits the correct password
while (true)
{
// Capture input from the user. Save the result in pw
int pw = getPassword();
// Here we test if the user has submitted the correct password
if (nCombo == pw)
{
cout << "You got it!" << endl;
// Break out of the loop, and exit the program
break;
}
}
return 0;
}
// Fetch password from user to compare to nCombo
int getPassword()
{
int nPassword = 0;
cout << "Please enter password..." << endl;
cin >> nPassword;
return nPassword;
}