Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am having a fairly frustrating issue where I attempt to pass the contents of a pointer array that is filled with objects to another class so that I may print the contents of the Array but no matter what I have tried to do in changing the way I pass the array I consistently get a Segmentation Fault. I have looked all around for people with similar problems and I did not manage find anyone with the same issue but still I apologize if this is a duplicate question!
anyways, my code for these two classes is
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//Time Class
class Time{
public:
Time();
void setHour(int sHour){hour = sHour;}
void setMinute(int sMinute){minute = sMinute;}
void setAmPm(int sAmPm){ampm = sAmPm;}
int getHour(){return hour;}
int getMinute(){return minute;}
int getAmPm(){return ampm;}
private:
int hour;
int minute;
int ampm;
};
//Time Constructor implementation
Time::Time(){
hour = 0;
minute = 0;
ampm = 0;
}
//Event Class
class Event{
public:
Event(string x, Time y);
void setDesc(string sDesc){description = sDesc;}
string getDesc(){return description;}
void setTime(Time t, int h, int m, int ampm){t.setHour(h); t.setMinute(m); t.setAmPm(ampm);}
Time getTime(){return eventTime;}
printEvent(Event e){
Time t = e.getTime();
cout << description << endl;
string ampm ="";
if(t.getAmPm() == 0)
ampm = "AM";
else
ampm = "PM";
cout << t.getHour() << ":" << t.getMinute() << " in the " << ampm;
}
private:
string description;
Time eventTime;
};
//Event Constructor implementation
Event::Event(string cDesc, Time t){
description = cDesc;
eventTime = t;
}
//Month Class
class Month{
public:
Month(string x, int y);
void addEvent(string desc, int h, int m, int ampm, int day){
Time t;
t.setHour(h);
t.setMinute(m);
t.setAmPm(ampm);
Event e(desc, t);
this -> event[day] = &e;
}
void deleteEvent(int day){delete event[day];}
Event getEvent(int day){
return *event[day];
}
void displayEvent(int day){
Event e = getEvent(day);
e.printEvent(e);
}
void displayAll(int days){
// Time t;
// Event e("StupidSolution", t);
// for(int i = 0; i < days; i++)
// e.printEvent(*event[i]);
}
private:
int days;
string month;
Event* event[31];
};
//Month Constructor implementation
Month::Month(string cMonth, int cDays){
days = cDays;
month = cMonth;
}
//both num days and find month are used to determine the Month the user wants to generate a calendar for and the amount of days in that month
string findMonth(int numMonth){
if(numMonth == 1)
return "January";
if(numMonth == 2)
return "February";
if(numMonth == 3)
return "March";
if(numMonth == 4)
return "April";
if(numMonth == 5)
return "May";
if(numMonth == 6)
return "June";
if(numMonth == 7)
return "July";
if(numMonth == 8)
return "August";
if(numMonth == 9)
return "September";
if(numMonth == 10)
return "October";
if(numMonth == 11)
return "November";
if(numMonth == 12)
return "December";
}
int numDays(int numMonth){
if(numMonth == 1)
return 31;
if(numMonth == 2)
return 28;
if(numMonth == 3)
return 31;
if(numMonth == 4)
return 30;
if(numMonth == 5)
return 31;
if(numMonth == 6)
return 30;
if(numMonth == 7)
return 31;
if(numMonth == 8)
return 31;
if(numMonth == 9)
return 30;
if(numMonth == 10)
return 31;
if(numMonth == 11)
return 30;
if(numMonth == 12)
return 31;
}
//gets all necessary info from user for creating a new event
void newEvent(int day, Month month){
string desc = "";
int h = 0;
int m = 0;
int ampm = 0;
cin.get();
cout << "Please enter a Brief Description of the Event!: " << endl;
getline(cin, desc);
cout << "Please enter the Hour of event(1-12): " << endl;
cin >> h;
cout << "Please enter the Minute of event (0-59): " << endl;
cin >> m;
cout << "Please enter 0 if it is in the AM and 1 if it is in the PM: " << endl;
cin >> ampm;
month.addEvent(desc, h, m, ampm, day);
}
int main(void){
string month = "";
int numMonth = 0;
int menuChoice = 0;
int menuLoop = 0;
int days = 0;
int eDay = 0;
cout << "please enter the month you would like to track(1-12): ";
cin >> numMonth;
days = numDays(numMonth);
month = findMonth(numMonth);
Month m(month,days);
while(menuLoop == 0){
cout << "Event Calendar for the month of " << month << endl;
cout << "1. Create a new Event" << endl;
cout << "2. Delete an existing Event" << endl;
cout << "3. Display Event for a particular day" << endl;
cout << "4. Display All Events" << endl;
cout << "5. Exit Program" << endl;
cout << "Enter Choice: " << endl;
cin >> menuChoice;
switch(menuChoice){
case 1:
cout << "What day would you like this event to be on?" << endl;
cin >> eDay;
if(eDay > 0 && eDay <= days)
newEvent(eDay, m);
else cout << "Invalid Day";
break;
case 2:
cout << "What day would you like to clear of events?" << endl;
cin >> eDay;
if(eDay > 0 && eDay <= days)
m.deleteEvent(eDay);
break;
case 3:
cout << "What day would you like to View?" << endl;
cin >> eDay;
if(eDay > 0 && eDay <= days)
m.displayEvent(eDay);
break;
case 4:
cout << "Displaying all Events.";
m.displayAll(days);
break;
case 5:
cout << "Goodbye!" << endl;
menuLoop++;
break;
default:
cout << "incorrect input";
break;
}
}
}
The important bits are the Event* event[31]; array, the displayEvent and the PrintEvent functions. I have tried all forms of passing by reference and de-referencing the array as I pass it but nothing seems to fix the issue...
Thank you so much!
Edit: added the remainder of the program, the segmentation fault occurred after creating a new event (option 1 on the menu) then either trying to delete it (option 2) or display it (option 3)
This function:
void addEvent(string desc, int h, int m, int ampm, int day){
Time t;
t.setHour(h);
t.setMinute(m);
t.setAmPm(ampm);
Event e(desc, t);
this -> event[day] = &e;
}
Creates a new Event e on the stack. Then it saves a pointer to that local variable. As soon as your function returns, that pointer is no longer valid and using it will result in undefined behavior (such as a seg fault, but it could do many other things).
To fix it, work out whether those arrays should be arrays of pointers, or just arrays of objects. If you definitely want to use pointers, then you will need to work out how you control their life time. Alternatively, you could use a smart pointer that will look after the object's lifetime for you.
Note: this is just the first thing that jumped out at me, there may be other problems.
Related
So im working on a project for school and i just cant get this to work. everything else seems to run but when i input the users choice for q, s, z10, z25,z5,z1 it loops indefinetly. i cant figure out why it keeps running the function over and over. im not quite sure if im just typing something wrong or what but this is the only thing im really stuck on currently. Here is the code
#include<iostream>
#include<string>
using namespace std;
struct Inventory
{
int Pack_25oz;
int Pack_10oz;
int Pack_5oz;
int Pack_1oz;
};
void show_Menu();
void initialize (Inventory& I);
void add_25OZ (Inventory& I, int P25);
void add_10OZ (Inventory& I, int P10);
void add_5OZ (Inventory& I, int P5);
void add_1OZ (Inventory& I, int P1);
void Place_Order(Inventory& I, int amount);
void show (Inventory& I);
int main(){
Inventory I;
string choice;
int nop= 0;
initialize(I);
show_Menu();
cout << "Dear User! please make a choice from above menu : " << endl;
getline(cin,choice);
while(choice!="q" or choice != "Q"){
if(choice == "s"|| choice == "S"){
show(I);
}
else if(choice == "z25" || choice == "Z25"){
cout << "Enter the number of packages : \n";
cin >> nop;
add_25OZ(I, nop);
}
else if(choice == "z10" || choice == "Z10"){
cout << "Enter the number of packages : \n";
cin >> nop;
add_10OZ(I, nop);
}
else if(choice == "z5"||choice == "Z5"){
cout << "Enter the number of packages : \n";
cin >> nop;
add_5OZ(I, nop);
}
else if(choice == "z1"|| choice == "Z1"){ // choice equal to "z1" or "Z1"
cout << "Enter the number of packages : \n" ; // prompt for number of packages
cin >> nop ; // accept user input
add_1OZ( I, nop);// call add_1OZ function
}
else if(choice == "o"||choice == "O") {
cout << "Enter the number of ounces : ";// prompt for number of ounces
int noun; // noun stands for number of ounces
cin >> noun;// accept user input
Place_Order( I, noun);
}
else if (choice == "q" || choice == "Q"){ // Output final message that the program has ended
cout << "The program has ended.";
break;
}
else
cout << "invalid comand" << endl;
};
return 0;
}
void show_Menu(){
cout << "S - Show Inventory" << endl;
cout << "Z25 - Add 25 oz packages"<< endl;
cout << "Z10 - Add 10 oz packages"<< endl;
cout << "Z5 - Add 5 oz packages"<< endl;
cout << "Z1 - Add 1 oz packages "<< endl;
cout << "O - Place order"<< endl;
cout << "Q - End"<< endl;
}
void show (Inventory& I)
{
cout << "The Inventory comprises of the following:\n 1. Pack_25oz : " << I.Pack_25oz << "\n 2.
Pack_10oz : " << I.Pack_10oz << "\n 3. Pack_5oz : " << I.Pack_5oz << "\n 4. Pack_1oz : " <<
I.Pack_1oz <<endl ;
}
void initialize (Inventory& I) {
int var = 0 ;
I.Pack_25oz = var;
I.Pack_10oz = var;
I.Pack_5oz = var;
I.Pack_1oz = var;
}
void add_25OZ (Inventory& I, int P25) {
I.Pack_25oz += P25;
}
void add_10OZ (Inventory& I, int P10) {
I.Pack_10oz += P10;
}
void add_5OZ (Inventory& I, int P5) {
I.Pack_5oz += P5;
}
void add_1OZ (Inventory& I, int P1) {
I.Pack_1oz += P1;
}
void Place_Order(Inventory& I, int amount) {
int subtract;
subtract = amount / 25;
if (I.Pack_25oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_25oz -= subtract; }
amount = amount % 25;
subtract = amount / 10;
if (I.Pack_10oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_10oz -= subtract; }
amount = amount % 10;
subtract = amount / 5;
if (I.Pack_5oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_5oz -= subtract; }
amount = amount % 5;
subtract = amount / 1;
if (I.Pack_1oz <= 0) {
cout << "Not enough packs" << endl;
} else { I.Pack_1oz -= subtract;
cout << "Order Fufilled" << endl;
}
}
It seems you call getline outside of the loop. so choice is never updated.
Hello StackOverflow community.
I cant seem to change a value on a class.
I am new to C++, and I have been looking for a solution but I cant seem to figure it out.
This is my Account.cpp
#include "Account.h"
#include <iostream>
using namespace std;
Account::Account(string accName, int accBalance)
{
name = accName;
balance = accBalance;
std::vector<std::string> Report();
}
bool Account::Deposit(Account a,int amt) {
if (amt >= 0) {
a.balance += amt;
return 1;
}
else
return 0;
}
bool Account::Withdraw(Account a, int amt) {
if (amt >= 0 && (a.balance - amt > 0)) {
a.balance -= amt;
return 1;
}
else
return 0;
}
int equal(string a, string b) {
if (a == b) return 1;
else return 0;
}
Account::~Account()
{
}
This is my main.cpp
int main() {
int aux_bal;
string aux_name;
int choice;
std::string InputName;
std::list< Account > arr;
std::list<Account>::iterator result;
do
{
cout << endl
<< " 1 - Create New Account.\n"
<< " 2 - View Balance.\n"
<< " 3 - Make a Deposit\n"
<< " 4 - Make a Withdraw\n"
<< " 5 - Check log\n"
<< " 6 - Exit.\n"
<< " Enter your choice and press return: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Input holder's account name: \n";
cin >> InputName;
arr.push_back(Account(InputName, 0));
break;
case 2:
cout << "Enter Account Name:\n";
cin >> InputName;
for (result = arr.begin(); result != arr.end(); result++) {
int aux_bal = result->balance;
std::string aux_name = result->name;
if (aux_name == InputName) {
cout <<"Account Balance:"<< aux_bal << endl;
cout << "Account Holder:" << aux_name << endl;
}
}
break;
case 3:
cout << "Enter Account Name:\n";
cin >> InputName;
for (result = arr.begin(); result != arr.end(); result++) {
int aux_bal = result->balance;
std::string aux_name = result->name;
if (aux_name == InputName) {
cout << "Enter ammount to Deposit:\n";
cin >> aux_bal;
(*result).Deposit(*result,aux_bal);
}
}
break;
case 4:
//code to help the user like give him
//extra information about the mode and the controller
break;
case 5:
break;
case 6:cout << "End of Program.\n";
break;
default:
cout << "Not a Valid Choice. \n"
<< "Choose again.\n";
break;
}
} while (choice != 6);
return 0;
}
For some reason when I output the account info after making a Deposit, the Balance value remains unaltered at 0.
Any help please?
The deposit function should act on the account object that it belongs to, you don't need to pass it as an argument. Also, integers are not booleans. Lastly, why is the deposit amount an int? What if I want to deposit $20.25 ?
bool Account::Deposit(int depositAmt)
{
if (depositAmt >= 0) {
this->balance += depositAmt; // "this->" is optional
return true;
}
else
return false;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am having to write a program for a class I'm taking. I have to take a birth date in numerical form (mm-dd-yyyy) and put it into a different format (Month day, year). I feel like my code is correct, but whenever my do while loop gets to the hyphen, it crashes. It compiles correctly, and Visual Studio isn't popping up any error until it runs, so I really don't know what's wrong. I've included the entire code. Also, use of the try/catch block is required for this assignment. Can anyone tell me why this loop doesn't like to check for hyphens? Thanks in advance.
#include <iostream>
#include <string>
using namespace std;
int get_digit(char c) {return c - '0';}
int main() {
string entry = "";
short month = 0;
int day = 0;
int year = 0;
bool error = false;
int temp = 0;
try {
cout << "Enter your birthdate in the form M-D-YYYY: ";
cin >> entry;
int x = 0;
do {
month *= 10;
temp = get_digit(entry[x]);
month += temp;
x += 1;
} while (entry[x] != '-');
if (month > 12) {
throw month;
}
x += 1;
do {
day *= 10;
temp = get_digit(entry[x]);
day += temp;
x += 1;
} while (entry[x] != '-');
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day > 31) {
throw day;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
throw day;
}
} else {
if (day > 29) {
throw day;
}
}
x += 1;
do {
year *= 10;
temp = get_digit(entry[x]);
year += temp;
x += 1;
} while (entry[x] != '\n');
if ((year % 4) != 0) {
if (month == 2 && day > 28) {
throw day;
}
}
switch (month) {
case 1:
cout << "January ";
case 2:
cout << "February ";
case 3:
cout << "March ";
case 4:
cout << "April ";
case 5:
cout << "May ";
case 6:
cout << "June ";
case 7:
cout << "July ";
case 8:
cout << "August ";
case 9:
cout << "September ";
case 10:
cout << "October ";
case 11:
cout << "November ";
case 12:
cout << "December ";
}
cout << day << ", " << year << endl;
} catch (short) {
cout << "Invalid Month!" << endl;
} catch (int) {
cout << "Invalid Day!" << endl;
}
system("pause");
return 0;
}
There is no newline character in your entry string, so x keeps getting incremented and causes buffer overrun for entry[x]. You need to look for the end of the string instead:
do {
year *= 10;
temp = get_digit(entry[x]);
year += temp;
x += 1;
} while (entry[x] != '\0');
I am writing a simple program to simulate a watch. Class Date, Class Time, Class Watch. Watch is derived from Date and Time. Trying to call a date member function from Time member function. It says I cant do it without an object. I am learning and I am sure most of this is crap haha. Any help would be appriciated. Basically at the end of Time::Tick, I am trying to call Class Date member function inDay. Calling one member function from another class member function. I dont know how to do a friend function from another class. I tried to make my functions static but it leads me down a long road of errors. Any advice?
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;
class Date
{
public:
Date();
void inDay();;// increments day, tests if month increments, if true, increment
void inMonth(); // increments month, tests if year increments, if true, increment
void inYear(); // increment year
void displayWatch(); // calls daysInMonthSwith, calls inDay, Display date
int daysInMonthSwitch(); // determines the number of days in the month based on which month using a switch
//void testPrint(); //Testing purposes only
void setDay();
void setMonth();
void setYear();
private:
int day;
int month;
int year;
int daysInMonth;
};
class Time{
public:
Time();
void setTime(int, int, int ); // set hours, minutes, seconds
void printStandard(); // print time in standard time format 12h
void setHour();
void setMin();
void setSec();
void tick();
private:
int hour;
int minute;
int second;
};
class Watch: public Time, public Date {
public:
Watch();
int getDate();
void setTime(int, int, int);
int getTime();
private:
};
//class Time::
Date::Date() : // Class Constructor
day(27),
month(2),
year(2013),
daysInMonth(31)
{
} // End Constructor
void Date::setDay()
{
int tempDay;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
while (tempDay < 1 || tempDay > daysInMonth)
{
cout << "You entered a day outside the range." << endl;
cout << "There are " << daysInMonth << " in the current month. Please enter a day between 1 and " << daysInMonth << ": ";
cin >> tempDay;
}
day = tempDay;
}
void Date::setMonth()
{
int tempMonth;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
while (tempMonth < 1 || tempMonth > 12)
{
cout << "You entered a month outside the range." << endl;
cout << "There are 12 in the year. Please enter a day between 1 and 12: ";
cin >> tempMonth;
}
month = tempMonth;
}
void Date::setYear()
{
int tempYear;
cout << "Please enter a year in the full number(Correct: 2005, Incorrect: 05): ";
cin >> tempYear;
year = tempYear;
}
void Date::displayWatch()
{
daysInMonthSwitch();
cout << "Date: " << month << "/" << day << "/" << year << endl;
//testPrint(); // Prints number of days in current month for testing only
//cout << "Month: " << month << endl; // Prints month for testing purposes
//inDay();
}
////////////// inDay function increments day and if conditions are met, increments month and year
void Date::inDay() //increments month and if day is more greater than number of days in month, call inMonth function
{
day++;
if (day > daysInMonth)
{
inMonth();
day = 1;
}
}// end function
void Date::inMonth() // increment month and if month is more greater than or equal to 12, call inYear function
{
month++;
if (month >= 12)
{
Date::inYear();
}
} // end function
void Date::inYear() // increment year
{
year++;
month = 1;
} // end function
//void Date::testPrint()
//{
// cout << "Days in month: " << daysInMonth << endl;
//} // for testing purposes only
int Date::daysInMonthSwitch() // Function contains switch that determines the number of days in the current month
{
//int month = m;
int result;
switch(month)
{
case 1:
result = 31;
break;
case 2:
result = 28;
break;
case 3:
result = 31;
break;
case 4:
result = 30;
break;
case 5:
result = 31;
break;
case 6:
result = 30;
break;
case 7:
result = 31;
break;
case 8:
result = 31;
break;
case 9:
result = 30;
break;
case 10:
result = 31;
break;
case 11:
result = 30;
break;
case 12:
result = 31;
break;
default :
cout << "Invalid Month" << endl;
}
daysInMonth = result;
return daysInMonth;
} // end daysInMonthSwitch function
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Time::Time()
{
const time_t currentTime = time(0);
const tm *localTime = localtime( ¤tTime );
setTime(localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
}
void Time::setHour()
{
int h;
cout << "Please enter the hour: ";
cin >> h;
hour = ( h >= 0 && h <= 24 ) ? h : 0;
}
void Time::setMin()
{
int m;
cout << "Please enter the minute: ";
cin >> m;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
}
void Time::setSec()
{
int s;
cout << "Please enter the second: ";
cin >> s;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
void Time::setTime( int h, int m, int s )
{ // validating time time, if incorrent, set to 0
hour = ( h >= 0 && h <= 24 ) ? h : 0;
minute = ( m >= 0 && m <= 60 ) ? m : 0;
second = ( s >= 0 && s <= 60 ) ? s : 0;
}
///void Time::tick();
void Time::printStandard()
{
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" <<
setfill('0') << setw(2) << minute << ":" << setw(2) << second
<< ( hour < 12 ? " AM" : " PM");
}
void Time::tick()
{
while (second >=0 && second <=60)
{
if (second < 59)
{
Sleep(1000);
second++;
}
else
{
Sleep(1000);
minute++;
second = 0;
if (minute >= 60)
{
hour++;
minute = 0;
if (hour >= 24)
hour = 0;
inDay();
}
}
}
};
///////////////////////////////////////////////////////////////////
Watch::Watch()
{}
////////////////////////////////////////////////////////////////// End Member Functions
int main()
{
Watch watch1;
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.setDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
watch1.inDay();
watch1.displayWatch();
watch1.printStandard();
system("pause");
return 0;
} // end main
Your class Time does not descend from Date. It can't call methods on classes that might also happen to be parents of child classes. Imagine if you instantiated a Time alone. What Date would it operate on?
A method that increments Time until it rolls over and then increments Date would need to be in Watch where it can see both.
\a3.cpp(75): error C2563: mismatch in formal parameter list
I'm certain I'm passing the function checkout with 3 doubles, I don't know why I'm getting the error I am. Please help.
#include <iostream>
#include <cstdlib>
using namespace std;
const double peanut_PRICE = 1.80;
const double peanut_SHIP = 0.50;
const double BOOK_PRICE = 9;
const double BOOK_SHIP = 1.06;
const double MOVIE_PRICE = 13.99;
const double MOVIE_SHIP = 0.05;
double checkout (double myamountofbooks, double myamountofmovies, double mypoundsofpeanuts)
{
myamountofbooks = myamountofbooks * (BOOK_PRICE + BOOK_SHIP);
myamountofmovies = myamountofmovies * MOVIE_PRICE * (1 + MOVIE_SHIP);
mypoundsofpeanuts = mypoundsofpeanuts * (peanut_PRICE + peanut_SHIP);
return (myamountofbooks + myamountofmovies + mypoundsofpeanuts);
}
bool validUserImput (int whereUserWantsToGoNext)
{
if (whereUserWantsToGoNext > 50 || whereUserWantsToGoNext < 0)
return false;
else return true;
}
bool validUserImput (double whereUserWantsToGoNext)
{
if (whereUserWantsToGoNext > 50 || whereUserWantsToGoNext < 0)
return false;
else return true;
}
int main()
{
//===========================Declaration Statements==================================
double amountofbooks = 0;
double amountofmovies = 0;
double poundsofpeanuts = 0;
int whereUserWantsToGoNext = 0;
while (! (whereUserWantsToGoNext == 4) )
{
cout << "1. Books\n2. Peanuts\n3. Movies\n4. Checkout\n" << endl;
cin >> whereUserWantsToGoNext;
if (!validUserImput(whereUserWantsToGoNext)) cout << "INVALID IMPUT" << endl;
if (whereUserWantsToGoNext == 1){
cout << "Please enter your number of books";
cin >> amountofbooks;
if (!validUserImput(amountofbooks)) cout << "INVALID IMPUT" << endl;
}
if (whereUserWantsToGoNext == 3){
cout << "Now please enter the number of movies you've selected";
cin >> amountofmovies;
if (!validUserImput(amountofmovies)) cout << "INVALID IMPUT" << endl;
}
if (whereUserWantsToGoNext == 2) {
cout << "Please enter the weight(in pounds) of your peanuts";
cin >> poundsofpeanuts;
if (!validUserImput(poundsofpeanuts)) cout << "INVALID IMPUT" << endl;
}
if (validUserImput == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts);
}
cin >> amountofbooks;
}
The problem is here:
if (validUserImput == 4) ...
validUserImput is a function, but you are not calling that function, you are trying to compare it to 4.
If you wanted to keep track of the number of valid inputs you received, you could instead add a new variable that you manually increment on every valid input.
The last if - you are comparing function pointer to an integer. try this:
if (validUserImput(3) == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts);
I assume you want to display the result of the checkout function if the user selects 4. So you probably wanted to write:
if (whereUserWantsToGoNext == 4) cout << "Total Cost is..." << checkout(amountofbooks, amountofmovies, poundsofpeanuts) << endl;