I am struggling to understand how to properly use pass by value and reference in this program. It keeps saying identifier is undefined when I put the Int in the main function. But when it's out of the main function it works fine. I can't use it outside of the main function otherwise it's an automatic 0. Am I just missing code that identifies it?
I didn't explain the program very well. It's essentially the Monty Hall problem but with boxes. The user then tries to guess the right box. The biggest issue I had was getting the non prize box to appear to the user in the output.
I am very new to coding so I am probably overlooking something.
#include <stdlib.h>
#include <iomanip>
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
/*******************************************************************************
* Function Name: main()
* Parameters: None
* Return Value: int
* Purpose: To execute the main function of the program. Performs loops and takes user input to execute the game.
*****************************************************************************/
void BoxCheck(int);
void PrizeBox(int&);
//int UserGuess = 0;
int main()
{
int UserChoice;
int UserGuess;
int BoxReveal;
int PrizeB;
// int Prize;
//char Switch;
cout << "Wellcome to Pandora's Prize!" << endl;
cout << "Infront of you there are three doors, and behind one of them is the grand prize!" << endl;
cout << "But in the the other two they contain a stink bomb." << endl;
cout << "It's your job to guess which one has the prize behind it." << endl;
cout << "Door 1?" << endl;
cout << "Door 2?" << endl;
cout << "Door 3?" << endl;
PrizeBox(PrizeB);
cin >> UserGuess;
BoxReveal != (PrizeB || UserGuess);
// cout << "The host revealed box number " << BoxReveal << " ." << endl;
//if (true)
//{
if (UserGuess == 1)
{
cout << "You picked box 1." << endl;
cout << "The host revealed box number " << BoxReveal << " ." << endl;
cout << "Would you like to stay with box number 1 or switch." << endl;
//cin >> UserChoice;
}
else if (UserGuess == 2)
{
cout << "You picked box 2." << endl;
cout << "The host revealed box number " << BoxReveal << " ." << endl;
cout << "Would you like to stay with box number 2 or switch." << endl;
//cin >> UserChoice;
}
else if (UserGuess == 3)
{
cout << "You picked box 3." << endl;
cout << "The host revealed box number " << BoxReveal << " ." << endl;
cout << "It contains a red snapper!" << endl;
cout << "Would you like to stay with box number 3 or switch." << endl;
//cin >> UserChoice;
}
else
{
cout << "This isn't a number associated with a box. Try again." << endl;
}
//}
/* if (true)
{
} */
//PrizeBox(Prize);
if (UserChoice == UserGuess)
{
cout << "You chose to stay with your original box." << endl;
BoxCheck(UserGuess);
}
else if (UserChoice != UserGuess) //|| UserChoice != BoxReveal)
{
cout << "You decided to switch." << endl;
BoxCheck(UserGuess);
}
/*else if (UserChoice != UserGuess || BoxReveal)
{
cout << "You decided to switch." << endl;
BoxCheck(UserGuess);
} */
else
{
cout << "Your answer was out of the parameters." << endl;
BoxCheck(UserGuess);
}
//BoxCheck(UserGuess);
system("pause");
return 0;
}
void PrizeBox(int& PrizeB)
{
srand(time(NULL));
PrizeB = rand() % 3 + 1;
//Prize = rand() % 3 + 1;
//cin >> PrizeB;
/* BoxReveal = !(PrizeBox || boxChoice);
cout << "Here is one of the box's opened! " << boxReveal << " ." << endl;
BSwitch = (boxChoice || boxReveal); */
}
void BoxCheck(int UserChoice)
{
if (UserChoice == PrizeB)
{
cout << "WOW YOU WON!!!!" << endl;
}
else if (UserChoice != PrizeB)
{
cout << "Sorry you got a red snapper" << endl;
}
else
{
cout << "Sorry you got a red snapper" << endl;
}
}
Related
My class is currently studying Classes and I'm attempting to complete my assignment due tomorrow. I have most of it done, however, for some reason the address input won't let me input any letters only numbers.
I can't figure out what I'm doing wrong here and would appreciate help in figuring out if I need to change something.
#include <iostream>
using namespace std;
class personalData
{
private:
string name;
string address;
int age;
string phNumber;
public:
void setName()
{
cout << "Enter name: ";
cin >> name;
}
string getName()
{
return name;
}
void setAddress()
{
cout << "Enter address: ";
cin >> address;
}
string getAddress()
{
return address;
}
void setAge()
{
cout << "Enter age: ";
cin >> age;
}
int getAge()
{
return age;
}
void setPhone()
{
cout << "Enter phone number: ";
cin >> phNumber;
}
string getPhone()
{
return phNumber;
}
void displayData()
{
cout << "\nName: " << getName() << endl;
cout << "Address " << getAddress() << endl;
cout << "Age: " << getAge() << endl;
cout << "Phone: " << getPhone() << endl;
}
};
int main()
{
personalData person1;
personalData person3;
personalData person2;
person1.setName();
person1.setAddress();
person1.setAge();
person1.setPhone();
person1.getAddress();
cout << "\n";
person2.setName();
person2.setAddress();
person2.setAge();
person2.setPhone();
person2.getAddress();
cout << "\n";
person3.setName();
person3.setAddress();
person3.setAge();
person3.setPhone();
person3.getAddress();
cout << "\n";
cout << "Person1 details: ";
person1.displayData();
cout << "Person2 details: ";
person2.displayData();
cout << "Person3 details: ";
person3.displayData();
return 0;
}
Here is what it looks like if you input both numbers and letters:
And here is what happens if you only input numbers:
I'm not sure if my syntax is incorrect. I am getting "no 'void Pair T1, T2::display()' member function Pair declared in class" as well as "no matching function for call to ‘Pair std::basic_string char, std::char_traits char" Here is the header file:
#ifndef PAIR_H
#define PAIR_H
#include <iostream>
#include <string>
using namespace std;
template <typename T1, typename T2 >
class Pair
{
private:
T1 t1;
T2 t2;
public:
Pair(const T1 & t1,const T2 & t2) : t1(t1), t2(t2) {};
T1 getFirst() const { return t1; };
T2 getSecond() const { return t2; };
//setters
void setFirst(const T1 & value) { t1 = value; };
void setSecond(const T2 & value) { t2 = value; };
};
template <typename T1, typename T2>
void Pair<T1,T2> :: display()
{
cout << t1 << " - " << t2 << endl;
}
#endif // PAIR_H
and this is the driver file.
#include <iostream>
#include <string>
using namespace std;
#include "pair.h"
int main()
{
string first;
cout << "Please enter a first name: ";
cin >> first;
string last;
cout << "Please enter a last name: ";
cin >> last;
Pair<string, string> fullName;
fullName.setFirst(first);
fullName.setSecond(last);
cout << "The first name is: " << fullName.getFirst() << endl;
cout << "The last name is: " << fullName.getSecond() << endl;
cout << "The complete pair is: ";
fullName.display();
cout << endl << endl;
int num1;
cout << "Please enter a number: ";
cin >> num1;
int num2;
cout << "Please enter another number: ";
cin >> num2;
Pair<int, int> numbers;
numbers.setFirst(num1);
numbers.setSecond(num2);
cout << "The first number is: " << numbers.getFirst() << endl;
cout << "The second number is: " << numbers.getSecond() << endl;
cout << "The complete pair is: ";
numbers.display();
cout << endl << endl;
string name;
cout << "Please enter a name: ";
cin >> name;
int score;
cout << "Please enter a score: ";
cin >> score;
Pair<string, int> grade;
grade.setFirst(name);
grade.setSecond(score);
cout << "The name is: " << grade.getFirst() << endl;
cout << "The score is: " << grade.getSecond() << endl;
cout << "The complete pair is: ";
grade.display();
cout << endl << endl;
return 0;
}
Yes, your syntax is incorrect.
1) display() is a member of Pair, so you must declare it inside the class.
You can add this declaration line in the public section
void display ();
or you can define display() inside the class (deleting the definition outside) [I added the const modifier]
void display () const { cout << t1 << " - " << t2 << endl; }
1 bis)
I suggest you to define the operator<< (output operator) for class Pair; something like, inside public, this
friend ostream & operator<< (ostream & os, Pair const & p)
{ return os << '(' << p.t1 << ',' << p.t2 << ')'; }
so you can simply output a pair like this
cout << fullName << endl;
2) You don't define a default constructor (without arguments), so you should define you pairs in this way
Pair<string, string> fullName("", "");
Pair<int, int> numbers(0, 0);
Pair<string, int> grade("", 0);
or, avoiding the following setFirst() and setSecond()
Pair<string, string> fullName(first , last);
Pair<int, int> numbers(num1, num2);
Pair<string, int> grade(name, score);
3) I don't know if you know it but there is a standard std::pair class (#include <utility>).
4) You should use che c++ tag for a question like this
5) Sorry for my bad English
I've been trying to work with function pointers for quite a bit now, and to no avail. I've been working with a few friends to create a C++ 11 library to make creating ASCII games easier, and I've personally been working on creating a menu class. The beef of the class is complete, but one issue - I can't get the buttons to call functions. I always get the error:
terminate called after throwing an instance of 'std::bad_function_call'
what(): bad_function_call
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Obviously the error lies somewhere in the pointers, but I can't solve it for the life of me. Thanks for the help in advance.
Menu.h
#ifndef MENU_H
#define MENU_H
using namespace std;
#include <functional>
#include <string>
#include <map>
class Menu {
public:
int numberOfOptions;
map<int, string> options;
int currentSelection;
string title;
Menu();
Menu(int initialNumberOfOptions, map<int, string> initialOptions, int initialSelection);
void display();
void waitForInput();
void attachOptionAction(int option, void (*function)());
private:
map<int, void (*std::function<void()>)> optionActions;
void executeOptionAction(int option);
};
#endif
Menu.cpp
#include "Menu.h"
#include <windows.h>
#include <iostream>
#include <conio.h>
Menu::Menu(int initialNumberOfOptions, map<int, string> initialOptions, int initialSelection) {
title = "";
numberOfOptions = initialNumberOfOptions;
options = initialOptions;
currentSelection = initialSelection;
}
void Menu::display() {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
for(int i = 0; i < 10; i++) {
cout << " " << endl;
}
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
if(title != "") {
cout << title << endl << endl;
}
for(int i = 0; i < numberOfOptions; i++) {
if(i == currentSelection - 1) {
cout << "[ " << options[i] << " ]" << endl;
} else {
cout << options[i] << endl;
}
}
waitForInput();
}
void Menu::waitForInput() {
char input;
while(!kbhit());
input = getch();
if(input == 72 && currentSelection > 1) {
currentSelection--;
} else if (input == 80 && currentSelection < numberOfOptions) {
currentSelection++;
} else if (input == 13) {
if(currentSelection == 1) {
executeOptionAction(1);
}
return;
}
display();
}
void Menu::attachOptionAction(int option, std::function<void()> function) {
optionActions[option] = function;
}
void Menu::executeOptionAction(int option) {
(optionActions[option])();
}
test.cpp
#include "Menu.h"
#include <unistd.h>
#include <iostream>
#include <iomanip>
#include <map>
void test() {
cout << "Hello, World!";
}
int main() {
map<int, string> options;
options[0] = "Play";
options[1] = "Help";
options[2] = "Quit";
Menu menu(3, options, 1);
menu.title = "ASCII Game Library 2015";
menu.display();
void (*actionPointer)() = NULL;
menu.attachOptionAction(1, (*actionPointer));
return 0;
}
This is wrong. I'm not even sure what's that supposed to be.
map<int, void (*std::function<void()>)> optionActions;
It should be:
map<int, std::function<void()>> optionActions;
This here is also wrong. It would be correct if you hadn't imported std into your current namespace.
void attachOptionAction(int option, void (*function)());
It should be:
void attachOptionAction(int option, const std::function<void()> & action);
This here is also wrong. You can't name your argument function after you imported std into your namespace.
void Menu::attachOptionAction(int option, std::function<void()> function)
It should be:
void Menu::attachOptionAction(int option, const std::function<void()> & action)
This here is also wrong. You don't check if the function exists or that a valid function pointer was assigned to it. Which you haven't.
(optionActions[option])();
It should be:
// Attempt to find the action!
map<int, std::function<void()>>::iterator action = optionActions.find(option);
// Did we find anything?
if (action == optionActions.end())
{
return;
}
// Is the function assigned to this action valid?
if (action->second)
{
action->second();
}
You are attaching a null function pointer to an action and try to call it. And since there's no validation it does exactly that. Which is why you get that exception:
void (*actionPointer)() = NULL;
menu.attachOptionAction(1, (*actionPointer));
But I'm not even sure how even managed to compile it :/
EDIT:
I hope you can find this example to be informative on what you're looking for.
#include <map>
#include <string>
#include <cstdlib>
#include <iostream>
#include <functional>
class Menu
{
protected:
typedef std::function<int (Menu &)> Callback;
typedef std::map<int, Menu> SubMenus;
public:
Menu()
: m_Id(0), m_Name(""), m_Handler(nullptr)
{
}
Menu(int id, const std::string & name, Callback clbk)
: m_Id(id), m_Name(name), m_Handler(clbk)
{
}
Menu & operator [] (int id)
{
return m_Childrens.at(id);
}
int Enter()
{
int result = 0;
if (m_Handler)
result = m_Handler(*this);
return result;
}
void Insert(const Menu & menu)
{
m_Childrens[menu.m_Id] = menu;
}
void Insert(int id, const std::string & name, Callback clbk)
{
m_Childrens[id] = Menu(id, name, clbk);
}
void Remove(int id)
{
m_Childrens.erase(id);
}
int GetId() const
{
return m_Id;
}
const std::string & GetName() const
{
return m_Name;
}
const Callback & GetHandler() const
{
return m_Handler;
}
bool IsChildren(int id) const
{
return (m_Childrens.find(id) != m_Childrens.cend());
}
Menu & GetChildren(int id)
{
return m_Childrens.at(id);
}
const SubMenus & GetChildrens() const
{
return m_Childrens;
}
private:
int m_Id;
std::string m_Name;
Callback m_Handler;
SubMenus m_Childrens;
};
void ClearScreen()
{
system("cls");
}
int SharedMenuDisplay(const Menu & menu)
{
ClearScreen();
std::cout << "Welcome to " << menu.GetName() << std::endl;
for (const auto & m : menu.GetChildrens())
{
std::cout << "> " << m.second.GetId() << " - " << m.second.GetName() << std::endl;
}
std::cout << "> 0 - Go Back" << std::endl;
std::cout << "Please select a sub menu: ";
int choice;
std::cin >> choice;
return choice;
}
int Menu_Home(Menu & menu)
{
int choice;
int result = 0;
do {
ClearScreen();
std::cout << "Welcome to " << menu.GetName() << std::endl;
for (const auto & m : menu.GetChildrens())
{
std::cout << "> " << m.second.GetId() << " - " << m.second.GetName() << std::endl;
}
std::cout << "> 0 - To Leave" << std::endl;
std::cout << "Please select a sub menu: ";
std::cin >> choice;
if (choice != 0 && menu.IsChildren(choice))
result = menu.GetChildren(choice).Enter();
} while (choice != 0);
return result;
}
int Menu_A(Menu & menu)
{
int choice;
int result = 0;
do {
choice = SharedMenuDisplay(menu);
if (choice != 0 && menu.IsChildren(choice))
result = menu.GetChildren(choice).Enter();
} while (choice != 0);
return result;
}
int Menu_A_SubMenu_X(Menu & menu)
{
ClearScreen();
std::cout << "You have selected " << menu.GetName() << std::endl;
std::cout << "> Type something and press Enter to go back..." << std::endl;
std::string str;
std::cin >> str;
return 0;
}
int Menu_A_SubMenu_Y(Menu & menu)
{
ClearScreen();
std::cout << "You have selected " << menu.GetName() << std::endl;
std::cout << "> Type something and press Enter to go back..." << std::endl;
std::string str;
std::cin >> str;
return 0;
}
int Menu_A_SubMenu_Z(Menu & menu)
{
ClearScreen();
std::cout << "You have selected " << menu.GetName() << std::endl;
std::cout << "> Type something and press Enter to go back..." << std::endl;
std::string str;
std::cin >> str;
return 0;
}
int Menu_B(Menu & menu)
{
int choice;
int result = 0;
do {
choice = SharedMenuDisplay(menu);
if (choice != 0 && menu.IsChildren(choice))
result = menu.GetChildren(choice).Enter();
} while (choice != 0);
return result;
}
int Menu_B_SubMenu_X(Menu & menu)
{
ClearScreen();
std::cout << "You have selected " << menu.GetName() << std::endl;
std::cout << "> Type something and press Enter to go back..." << std::endl;
std::string str;
std::cin >> str;
return 0;
}
int Menu_B_SubMenu_Y(Menu & menu)
{
ClearScreen();
std::cout << "You have selected " << menu.GetName() << std::endl;
std::cout << "> Type something and press Enter to go back..." << std::endl;
std::string str;
std::cin >> str;
return 0;
}
int Menu_B_SubMenu_Z(Menu & menu)
{
ClearScreen();
std::cout << "You have selected " << menu.GetName() << std::endl;
std::cout << "> Type something and press Enter to go back..." << std::endl;
std::string str;
std::cin >> str;
return 0;
}
int Menu_C(Menu & menu)
{
ClearScreen();
std::cout << "You have selected " << menu.GetName() << std::endl;
std::cout << "> Type something and press Enter to go back..." << std::endl;
std::string str;
std::cin >> str;
return 0;
}
int main(int argc, char **argv)
{
Menu home(-1, "Home", &Menu_Home);
home.Insert(1, "Menu Item A", &Menu_A);
home.Insert(2, "Menu Item B", &Menu_B);
home.Insert(3, "Menu Item C", &Menu_C);
home.GetChildren(1).Insert(1, "Sub Menu Item X", &Menu_A_SubMenu_X);
home.GetChildren(1).Insert(2, "Sub Menu Item Y", &Menu_A_SubMenu_Y);
home.GetChildren(1).Insert(3, "Sub Menu Item Z", &Menu_A_SubMenu_Z);
home[2].Insert(1, "Sub Menu Item X", &Menu_B_SubMenu_X);
home[2].Insert(2, "Sub Menu Item Y", &Menu_B_SubMenu_Y);
home[2].Insert(3, "Sub Menu Item Z", &Menu_B_SubMenu_Z);
return home.Enter();
}
I am trying to get OpenGL-ES working on a Raspberry Pi, but so far no luck. I compiled SDL 2.0.3 from source with this, as the version in Rasbian is missing Raspberry Pi support:
./configure --prefix=/home/pi/run/SDL2-2.0.3/ \
--disable-video-x11 \
--disable-pulseaudio \
--disable-esd \
--disable-video-opengl
The code below should create a OpenGL context and clear the screen to red. When I run the code, the Raspberry Pi is switching video modes, but the screen is turning black instead of red and the calls to glGetString(GL_VERSION) and Co. return NULL which would indicate that something is wrong with the GL context creation.
#include <SDL.h>
#include <SDL_opengles2.h>
#include <iostream>
void print_gl_string(GLenum name)
{
const GLubyte* ret = glGetString(name);
if (ret == 0)
{
std::cerr << "error getting string: " << name << std::endl;
}
else
{
std::cerr << name << ": " << ret << std::endl;
}
}
void set_gl_attribute(SDL_GLattr attr, int value)
{
if (SDL_GL_SetAttribute(attr, value) != 0)
{
std::cerr << "SDL_GL_SetAttribute(" << attr << ", " << value << ") failed: " << SDL_GetError() << std::endl;
}
}
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
SDL_DisplayMode videomode;
if (SDL_GetCurrentDisplayMode (0, &videomode) != 0)
{
std::cerr << "Error getting current display mode: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Current screen mode: " << videomode.w << "x" << videomode.h << std::endl;
set_gl_attribute(SDL_GL_RED_SIZE, 5);
set_gl_attribute(SDL_GL_GREEN_SIZE, 6);
set_gl_attribute(SDL_GL_BLUE_SIZE, 5);
//set_gl_attribute(SDL_GL_DEPTH_SIZE, 8);
set_gl_attribute(SDL_GL_DOUBLEBUFFER, 1);
set_gl_attribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
set_gl_attribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
set_gl_attribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_Window* window = SDL_CreateWindow("Minimal SDL2 Example",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
720, 576,
SDL_WINDOW_OPENGL);
if (!window)
{
std::cerr << "Could not create window: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
print_gl_string(GL_RENDERER);
print_gl_string(GL_SHADING_LANGUAGE_VERSION);
print_gl_string(GL_VERSION);
print_gl_string(GL_EXTENSIONS);
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
SDL_Delay(5000);
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
The problem turned out to not be in the code, but in the library path. A simple -L/opt/vc/lib/ added to the compile command line fixed it. Without that the compiler would pick:
/usr/lib/arm-linux-gnueabihf/libGLESv2.so.2
While the right one would be (use ldd to check):
/opt/vc/lib/libGLESv2.so
I'm using visual c++ 2010 and I'm having trouble with class constructors. I've written it exactly as my instructor described and I can't seem to figure out why it wont compile.
#include <iostream>;
using namespace std;
class Account
{
public:
Insert other functions here...
Account(float b)
{
Balance = b;
}
private:
float &Balance;
}
Int main()
{
float withdraw,deposit;
Account myAccount(100.00);
cout << "Enter the amount you would like to withdraw:" << endl;
cin >> withdraw;
MyAccount.debt(withdraw);
cout << "Your balance is now "<< endl;
MyAccount.showAccountInfo();
cout << endl;
cout << "Enter the amount you would like to deposit: " << endl;
cin >> deposit;
myAccount.credit(deposit);
cout << "Your balance now is " << endl;
MyAccount.showAccountInfo();
cout << endl;
return 0;
}
It is possible that you have declared the member Balance as a reference and you are calling the constructor with a constant (i.e. 100.00), you must pass a variable name to the contructor or declare the member without the reference operator.
If you need the balance in different parts of the process you could try it with dynamic memory.
For instance:
class Account
{
public:
Insert other functions here...
Account(float* b)
{
Balance = b;
}
private:
float* Balance;
}
Int main()
{
float withdraw,deposit;
Account myAccount(new float(100.0));
cout << "Enter the amount you would like to withdraw:" << endl;
cin >> withdraw;
MyAccount.debt(withdraw);
cout << "Your balance is now "<< endl;
MyAccount.showAccountInfo();
cout << endl;
cout << "Enter the amount you would like to deposit: " << endl;
cin >> deposit;
myAccount.credit(deposit);
cout << "Your balance now is " << endl;
MyAccount.showAccountInfo();
cout << endl;
return 0;
In the updating balance method, you should unreference the pointer:
For instance...
void debt(float withdraw){
*balance -= withdraw;
}
Thanks, Regards.