Creating a simple crackme program in C++ problems with variable and input - c++

So I am trying to learn C++ so I can learn some reverse engineering that why I am trying to create this simple crack me program to get a foundation going and not take someone else's project as I choose my own path. However I am using CodeBlocks as the other IDEs were not being cooperative and am enjoying it and has given me some error and two lines. Below is the following code. So there errors are the following:
||=== Build: Debug in SimpleProgram (compiler: GNU GCC Compiler) ===|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp||In member function 'int checker::processing(int)':|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp|15|warning: no return statement in function returning non-void [-Wreturn-type]|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp||In function 'int main()':|
D:\Programming\C++ Projects\simple programs\SimpleProgram\main.cpp|22|error: 'x' was not declared in this scope|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
#include <iostream>
using namespace std;
class checker{
public:
int number;
processing(int x){
x = number;
if ( number == 10 ){
cout << "Well done!";
} else {
cout << "Keep trying!";
}
}
};
int main()
{
checker cracking;
cout << "Please enter in the correct number";
cin >> cracking.processing(x);
return 0;
}
Image of the project and error

A function always has a return type, even if your not attempting to return anything it will have the void signature. If your intent was to take input of a number passed from main and display it via a function in your class checker through an object of the same, this is how it would look like:
#include <iostream>
using namespace std;
class checker{
public:
int number;
void processing(int x)
{
if (x==10)
cout << "Well done!";
else
cout << "Keep trying!";
}
};
int main()
{
checker cracking;
cout << "Please enter in the correct number \n";
int n;
cin >> n;
cracking.processing(n);
return 0;
}

I've cleaned the code up and included comments that serve as notes:
#include <iostream>
using namespace std;
class checker{
public:
void setnumber(int i){ //it's a good habit to put variables in private and access them with a public function
this->number = i;
};
int processing(int x){ //x is just a placeholder value for whatever you put in here. You can't use it in the rest of the program
if ( x == 10 ){
cout << "Well done!" << endl;
return 1; //this condition indicates success
} else {
cout << "Keep trying!" << endl; //the endline just makes it so you aren't typing on the same line as the message
return 0; //this condition indicates success hasn't been reached yet
}
}
private:
int number;
};
int main()
{
checker cracking;
cracking.setnumber(10); //the number is set to 10
int i, l; //i is the guess number, l is a condition for the loop
cout << "Please enter in the correct number" << endl;
do{ //this loop sets it up so that you can have multiple tries
cin >> i;
l = cracking.processing(i);
}while(l!=1); //and this condition (the return value of processing(), breaks the loop on success
return 0;
}
The main issue that popped out at me was the use of x.
Trying to set x to number. In functions, the parameters are just placeholder values for arguments that will be passed into later. Then later on when you tried to use x as an input in the main() program. You were calling that function (using it) and needed an int as input.
Don't worry. It's confusing in the beginning for everyone (although to be fair, as you progress you'll just find new things to be confused about. It never really stops). Keep at it and it'll all make sense in time.

Related

Variables not declared in scope error/too few arguments to function

I am writing a simple code to take a number and a power based off user input and square the number to that power using functions. While attempting to compile the code, though, I get multiple errors. here is the code:
#include <iostream>
using namespace std;
double power(double& n1, sq)
{
for (int i = 0; i < sq; i++) {
n1* n1;
}
return n1;
}
int main()
{
double power(double&);
double num1, square;
cout << "Enter a number IMMEDIATLY: ";
cin >> num1;
cout << "\nEnter a power: ";
cin >> square;
power();
cout << num1 << endl;
return 0;
}
Here are the errors that I am receiving:
||=== Build: Debug in practice (compiler: GNU GCC Compiler) ===|
|5|error: 'sq' has not been declared|
In function 'double power(double&, int)':|
|6|error: 'sq' was not declared in this scope|
|7|warning: statement has no effect [-Wunused-value]|
In function 'int main()':|
|22|error: too few arguments to function 'double power(double&)'|
|15|note: declared here|
|17|warning: unused variable 'ans' [-Wunused-variable]|
||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
Any help or light shed on how to fix these errors would be much appreciated, for i have been stumped for quite sometime. Thank you!
edit: so I have parsed square as an int and initialized the variables into the power() (as you guys have said) but now the code produces the incorrect answer as the output (anything bigger than squaring the number produces incorrect output).
#include <iostream>
using namespace std;
double power(double& n1, int& sq) {
for (int i=2; i<=sq; i++) {
n1*=n1;
}
return n1;
}
int main()
{
double power(double& n1, int& sq);
double num1;
int square;
cout << "Enter a number IMMEDIATLY: ";
cin >> num1;
cout << "\nEnter a power: ";
cin >> square;
power(num1, square);
cout << num1 << endl;
return 0;
}
To answer your second question, simply trace through the code with an example:
Let's say the inputs are
n1 = 3
sq = 3
We know that 3^3 = 27, so lets see if we get that answer.
First, the operation n1 *= n1 is multiplying n1 by itself. For squaring alone, this is fine: 3*3 = 9. But then you loop through it again, and n1 is now 9, so the code will compute 9*9 = 81.
See if you can figure it out from here. Hint: you'll need another variable for storage.
Also, your return statement is outside of your brackets for the power() function. Though its working on its own because you passed &n1 in as a reference. Either remove the return statement altogether, or make a new variable in main that receives the value from power(). If you do the latter, remove the & symbol and place the return statement within the power brackets. To get a better understanding of passing by reference vs. passing by value, see this link. Good luck!

Incomplete Type Not Allowed in Counting Vowel program

#include "stdafx.h"
#include <iostream>
#include <string>
int isVowel(char &a, int &counter);
bool enterAnotherOne();
void outputResult(int &counter);
bool goAgain();
using namespace std;
int main() {
int counter = 0;
char a;
do
{
do
{
void enter(a);
int isVowel(counter);
void outputResult();
} while (enterAnotherOne());
} while (goAgain());
return 0;
}// Function main()
// ===================
void enter() {
char a;
cout << "Enter a letter. ";
cin >> a;
}
}// Function Letter()
// ===========================
int isVowel(char &a, int &counter) {
counter = 0;
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'y')
{
counter++;
}
return counter;
}// isVowel()
// ==============
bool enterAnotherOne() {
char a;
cout << "Would you like to enter another letter? ";
cin >> a;
if (a == 'y')
{
return true;
}
else
{
return false;
}
}
void outputResult(int &counter) {
cout << "The number of vowels that you entered are " << counter << endl;
}// outputResult()
// ===================
bool goAgain() {
char a;
cout << "Would you like to go again? ";
cin >> a;
if (a == 'y')
{
return true;
}
else
{
return false;
}
}
Hey Guys, I was making a program that would count the number of vowels that are entered when inputting random letters. The problem I am having is that, this line:
void enter(a);
it says incomplete type is not allowed and I can't figure out what is wrong with it.
void enter(a);
is likely being seen by the compiler as either
a declaration of enter as a variable of type void and passing a into the void constructor, and void's not a complete type because you can't make a void. void is nothing.
a declaration of function enter that returns void that expects a parameter of type a passed by value. a would be the incomplete type.
I think the first interpretation is more likely.
Anyway, you likely wanted to call
enter(a);
But that won't work because the enter function doesn't take any parameters. Let's look at enter for a moment.
void enter() {
char a;
cout << "Enter a letter. ";
cin >> a;
}
This function unfortunately doesn't do much. it reads a character from the user and promptly throws it away. We probably want to use that character, so
char enter() {
char a;
cout << "Enter a letter. ";
cin >> a;
return a;
}
Now we get a copy of the character returned to the caller. That means
enter(a);
Should look more like
a = enter();
You have similar problems with
int isVowel(counter);
void outputResult();
right below the call to enter.
Unfortunately enter is not visible to main because it is declared after main is declared. I recommend moving the function declaration to above main in the file. You could forward declare enter as you have done with the other functions, but why bother? A forward declaration means you may have two places to change the code if the function changes.
I recommend pulling out your programming textbook and reading the first few chapters to get a better grasp of functions. If you are learning on your own and have no textbook, or if your textbook sucks, The Definitive C++ Book Guide and List may be of use to you.
Side note: There are more than just guys running around here.
This has a few issues with it.
Firstly, you don't seem to prototype the enter function (you don't declare it with the rest of the functions near the top of the code).
Secondly, I think you're trying to call the functions using the code within the do loops, however it's not clear due to the fact that you've put a type before your reference to the functions. If you're trying to call them, you shouldn't mention any types beforehand, so void enter(a); should just become enter(a); (not really, due to the "a", more on that on the next point).
Thirdly, assuming that you were trying to call the functions in that bit of code, as I assumed above, the arguments you're passing in aren't consistent with the way the functions are defined. For example, enter requires no arguments in the function you've created, however when you attempt to call it in the do loop, you're trying to pass in a variable "a" which it isn't prepared for. Checking the other function calls in that block, you'll find it to be inconsistent with those, too.
Fourthly, and building off of the last point, I believe you made the mistake of leaving out the "a" variable being passed into enter, as you reference it whilst within the function however it is never passed in.
Don't be discouraged by this! Keep learning and prosper!
There's quite a few issues with your code, I've put some meaningful comments in my code to help you. It sounds harsh, but I'm must in a rush, so I'm not trying to be. This will fully compile and run like I think you'd like.
#include "stdafx.h"
#include <iostream>
#include <string>
void enter(char&); /*You forget to put the function prototype here.*/
void isVowel(char &, int &); /*Since you are passing by reference, and NOT using the return value, you don't need have have it return int.*/
bool enterAnotherOne();
void outputResult(int &);
bool goAgain();
using namespace std;
int main() {
int counter = 0;
char a;
do
{
do
{
enter(a); //you don't need to declare the function type when calling a function :(
isVowel(a, counter); /*You didn't properly pass the argument here*/
outputResult(counter); //This needs an argument.
} while (enterAnotherOne());
//Did you want to reset the counter? if so, do it here.
counter = 0;
} while (goAgain());
return 0;
}// END OF Function main() /*Make sure you clarify it is the END OF function main().*/
// ===================
void enter(char &letter) { /*This requires an argument to be useful.*/
cout << "Enter a letter. ";
cin >> letter;
}
// END OF Function Letter() also, you had an extra bracket, which means this wouldn't compile.
// ===========================
void isVowel(char &a, int &num) {
//counter = 0; //if your're coing to pass counter as a argument, why doe this?
//counter = 0 resets counter back to 0.
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'y')
{
num++;
}
}// END OF isVowel()
// ==============
bool enterAnotherOne() {
char choice; //give meaningful variable names!
cout << "Would you like to enter another letter? ";
cin >> choice;
if (choice == 'y')
{
return true;
}
else
{
return false;
}
}//If you're going ot comment END OF on every function, might as well do it here.
void outputResult(int &num) {
cout << "The number of vowels that you entered are " << num << endl;
}// END OF outputResult()
// ===================
bool goAgain() {
char choice;
cout << "Would you like to go again? ";
cin >> choice;
if (choice == 'y')
{
return true;
}
else
{
return false;
}
}//where's END OF for this function?

How do I fix this compiler error

Been working on this for the past 5 hrs or so. I have done the research and tried to see what was wrong but I couldn't grasp what was missing. If anyone can help me solve this minor issue I would be grateful
Here's the code
#include <iostream>
using namespace std;
bool isPrime (int num);
int main()
{
int num=0;
cout << "Enter a number and I'll tell you whether it is prime: ";
cin >> num;
if (isPrime(num)==true)
cout << num << " is prime.";
else
cout << num << " is NOT prime.";
return 0;
}
bool isPrime(int input)
{
if(input<1)
return false;
else if (input == 1||input ==2 ||input==3)
{
return true;
}
else
{
for(int i=2; i<input; i++)
{
if(input%i==0)
return false;
}
return true;
}
}
It gives me this compiler error
./main.cpp: In function ‘int main()’:
./main.cpp:31:6: error: redefinition of ‘int main()’
int main()
^
./main.cpp:10:5: error: ‘int main()’ previously defined here
int main() {
^
I'm not sure what it means, but from where I researched, it means there are two mains but I have only one.
The following code compiles well with TurboC++ on windows and gives the appropriate result according to as required:
#include <iostream.h>
enum bool {true, false};
bool isPrime (int num);
int main()
{
int num=0;
cout << "Enter a number and I'll tell you whether it is prime: ";
cin >> num;
if (isPrime(num)==true)
cout << num << " is prime.";
else
cout << num << " is NOT prime.";
return 0;
}
bool isPrime(int input)
{
if(input<1) {
cout<<"Negatives not allowed"<<endl;
return false;
}
else if(input == 1) {
return false;
}
else if (input ==2 ||input==3) {
return true;
}
else {
for(int i=2; i<input; i++) {
if(input%i==0)
return false;
}
return true;
}
}
Make sure you're not including any additional files that also define main()
If you're working in visual studio, you should check the solution explorer for other .cpp files, especially if you've moved this over from an old solution, where you could've forgotten to get rid of old references. If that fails, you could just copy and paste the code that you do have into a new project, since what you have shouldn't be producing the error that you're getting.
Are you including this file from multiple others without an include guard? Or you could possibly have another main written in another cpp file in your project that you forgot about, in which case make sure you do not build that one with the project by excluding, or creating an entirely new project and adding each file you need individually.
I have found that with most IDE's, and even with command-line compiling, when you get really weird errors and cannot fix them creating a new project can solve many of them.
Your exact code runs perfectly on my compiler. So there is no problem on your code. This kind of problem always happens for you compiler. I think you are using an IDE in which you have created a project and added another code, that has a main() function too. So you can easily resolve this by creating a new project from scratch and copy paste this code.

I am having troubles with creating and executing prototype functions in c++ programming

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;
}
Well, the first thing I saw was that you are passing arguments to your main function. This is a no-no as the main function is the one that gets called first, and thus there is no way for the program to pass arguments to it. so I'd suggest you change it to simple int main(){...}
Next thing would be that you have defined the getPassword function to take in an argument nPassword, but when you are calling the function, you are not passing any argument, so I'd assume your functiongetPassword doesn't get called. There are two fixes to this :
a) Make getPassword accept no arguements.
b) Pass an arguement to getPassword. This however, given the way your code is, is not recommend. It seems that nPassoword is the variable in which you want to store the user entered password; if you pass it as an arguement, that means you already have the inputted password, and thus it makes no sense to call the function getPassword .
So following fix 'a', I'd suggest doing something like :
int getPassword(){
int nPassword;
std::cout << "Please enter password..." << std::endl;
std::cin >> nPassword;
return nPassword;
}
and then calling the function by
int nPassword;
nPassword = getPassword();
The above things in a single code would be like :
#include<iostream>
//protoype declaration
int getPassword();
//combo to check for
const int nCombo = 3141;
int main(){
int nPassword;
nPassword = getPassword();
if (nPassword == nCombo)
{
std::cout << "Correct password!" << std::endl;
}
else
{
std::cout << "Incorrect password!" << std::endl;
}
return 0;
}
int getPassword(){
int nPassword;
std::cout << "Please enter password..." << std::endl;
std::cin >> nPassword;
return nPassword;
}
Ofcourse, if you want the user to keep entering the password till it is correct, you can just make the contents of the main function in a while loop which continues till the password entered is correct. Hope this helped.

I think I'm trying to pass data wrong in my variables? Novice coder inside

I started learning some basics of C++ and I wanted to write some code to practices what I've learned. I wanted to make a class and some functions. It's supposed to be a title screen to start a text game, except there is no game...yet :P
Whenever I enter 1 to start so it displays "Good Work" it just does nothing after I hit enter.
Any point in the right direction would be great. I've been watching videos and reading tutorials on functions, it doesn't seem to cover the problem I'm having...
#include <iostream>
#include <string>
using namespace std;
//Function Protos
void keyError();
int userInput(int x);
//class library
class Title
{
bool nSelect;
int x;
public:
void titleScreen()
{
while(nSelect)
{
cout << "Welcome to Biggs RPG!" << endl << "1. Play 2. Exit" << endl;
userInput(x);
if (userInput(1))
nSelect = 0;
else if (userInput(2))
{
cout << "Closing program..." <<endl;
nSelect = 0;
}
else
keyError();
}
}
};
int main()
{
Title displayTitle;
displayTitle.titleScreen();
cout << "Good work";
return 0;
}
void keyError()
{
cout << "Meow? Wrong input try again." << endl;
}
int userInput(int x)
{
x = 0;
cin >> x;
return x;
}
There are numerous stylistic and technical problems. Try learning from resources recommended in The Definitive C++ Book Guide and List.
Here is a start…
#include <iostream>
#include <string>
// "using namespace std;" is poor practice. Better to write out std::
/* Unless you will have two title screens at the same time,
this should probably be a namespace, not a "singleton" class. */
namespace Title
{
int nSelect;
void titleScreen()
{
do {
// prompt for input
std::cout << "Welcome to Biggs RPG!\n" "1. Play 2. Exit\n";
// get ready to accept input, even if there was an error before
if ( ! std::cin ) {
std::cin.clear(); // tell iostream we're recovering from an error
std::cin.ignore( 1000, '\n' ); // ignore error-causing input
}
// repeat if invalid input
} while( ! std::cin >> nSelect || ! handleInput( nSelect ) );
The difference is that you want to ask for input, then handle it. The code you posted asks for input again each time it checks what the input was.
This is a do … while loop, so it executes at least once and then repeats as long as the condition at the end is true. If the user gives an invalid input, then ! std::cin evaluates to true. Then the policy of C++ is to stop returning any input until you call std::cin.clear(), which signals that you are going to try again. ignore then gets rid of the invalid input. Then ! std::cin >> nSelect tries to read a number, and if that operation is successful, call handleInput (which you must write) which should return false if the input was invalid. So if reading a number fails, or the wrong number was entered, the loop goes again.
You should compare the return value of userInput with 1 or 2, like this:
int userInput(void);
//class library
class Title
{
bool nSelect;
int x;
public:
void titleScreen()
{
nSelect = true;
while(nSelect)
{
cout << "Welcome to Biggs RPG!" << endl << "1. Play 2. Exit" << endl;
x = userInput();
if (x == 1)
nSelect = false;
else if (x == 2)
{
cout << "Closing program..." <<endl;
nSelect = false;
}
else
keyError();
}
}
};
and define userInput as:
int userInput(void)
{
int x = 0;
cin >> x;
return x;
}
I sense confusion about the difference between parameters and return values. When you define a function as
int userInput(int x) {
...
You pass a value to the function (x) and return a value with the return statement. In your case you don't need to pass a parameter to your function; you need to return a value. You access this value by assigning it to another variable:
theResult = userInput(123);
But it doesn't matter what value you pass to the function; you might as well use
int userInput(void) {
...
In which case you can use
theResult = userInput();
Now just to confuse you, it is possible to pass the address of a variable as a parameter to a function. You can use that either to access data (usually a "larger" block of data like an array or struct) but it can also be used to provide a place where a return value is stored. Thus
void squareMe(int *x){
*x*=*x;
}
Would return the square of the number pointed to in that location. You could then do
int x=4;
squareMe(&x);
cout << x;
Would print out 16 (!). This is because the function looks at the contents of the address (&x is the address of the variable x), and multiplies it by itself in- place.
I hope this explanation helps.